Set 表示唯一對象的集合。集合中元素的排序是不相關(guān)的。
集合框架提供三種類型的集合:
Set
接口對數(shù)學(xué)中的一組進(jìn)行建模。集合是唯一元素的集合。
Java最多允許一個(gè)Set中的一個(gè)空元素。 Set
中元素的排序并不重要。
Java不保證 Set
中元素的排序。
當(dāng)循環(huán)遍歷 Set
的所有元素時(shí),你得到 Set
中的每個(gè)元素一次。
集合框架提供 HashSet
類作為實(shí)現(xiàn)為設(shè)置
接口。
以下代碼顯示了如何創(chuàng)建一個(gè)Set并向其添加元素。 當(dāng)向集合添加重復(fù)元素時(shí),它們將被忽略。
如果比較它們,則在集合中的兩個(gè)元素被認(rèn)為是相等的使用 equals()
方法返回true。
import java.util.HashSet; import java.util.Set; public class Main { public static void main(String[] args) { Set<String> s1 = new HashSet<>(); // Add a few elements s1.add("HTML"); s1.add("CSS"); s1.add("XML"); s1.add("XML"); // Duplicate // Create another set by copying s1 Set<String> s2 = new HashSet<>(s1); // Add a few more elements s2.add("Java"); s2.add("SQL"); s2.add(null); // one null is fine s2.add(null); // Duplicate System.out.println("s1: " + s1); System.out.println("s1.size(): " + s1.size()); System.out.println("s2: " + s2); System.out.println("s2.size(): " + s2.size()); } }
上面的代碼生成以下結(jié)果。
集合框架提供 LinkedHashSet
類作為 Set
接口的另一個(gè)實(shí)現(xiàn)類。
HashSet
不保證順序元素。 LinkedHashSet
在插入元素時(shí)保持元素順序。
import java.util.LinkedHashSet; import java.util.Set; public class Main { public static void main(String[] args) { Set<String> s1 = new LinkedHashSet<>(); s1.add("A"); s1.add("B"); s1.add("C"); s1.add("D"); System.out.println("LinkedHashSet: " + s1); } }
上面的代碼生成以下結(jié)果。
我們可以對集合執(zhí)行并集,交集和差分運(yùn)算。
// Union of s1 and s2 will be stored in s1 s1.add(s2); // Intersection of s1 and s2 will be stored in s1 s1.retainAll(s2); // Difference of s1 and s2 will be stored in s1 s1.removeAll(s2);
在集合操作期間,修改s1。要保持原始設(shè)置不變,請?jiān)诓僮髦皬?fù)制:
Set s1Unions2 = new HashSet(s1); // Make a copy of s1 s1Unions2.addAll(s2);
要測試集合s1是否是另一個(gè)集合s2的子集,請使用s2.containsAll(s1)方法。
import java.util.HashSet; import java.util.Set; public class Main { public static void main(String[] args) { Set<String> s1 = new HashSet<>(); s1.add("HTML"); s1.add("CSS"); s1.add("XML"); Set<String> s2 = new HashSet<>(); s2.add("Java"); s2.add("Javascript"); s2.add("CSS"); System.out.println("s1: " + s1); System.out.println("s2: " + s2); performUnion(s1, s2); performIntersection(s1, s2); performDifference(s1, s2); testForSubset(s1, s2); } public static void performUnion(Set<String> s1, Set<String> s2) { Set<String> s1Unions2 = new HashSet<>(s1); s1Unions2.addAll(s2); System.out.println("s1 union s2: " + s1Unions2); } public static void performIntersection(Set<String> s1, Set<String> s2) { Set<String> s1Intersections2 = new HashSet<>(s1); s1Intersections2.retainAll(s2); System.out.println("s1 intersection s2: " + s1Intersections2); } public static void performDifference(Set<String> s1, Set<String> s2) { Set<String> s1Differences2 = new HashSet<>(s1); s1Differences2.removeAll(s2); Set<String> s2Differences1 = new HashSet<>(s2); s2Differences1.removeAll(s1); System.out.println("s1 difference s2: " + s1Differences2); System.out.println("s2 difference s1: " + s2Differences1); } public static void testForSubset(Set<String> s1, Set<String> s2) { System.out.println("s2 is subset s1: " + s1.containsAll(s2)); System.out.println("s1 is subset s2: " + s2.containsAll(s1)); } }
上面的代碼生成以下結(jié)果。
更多建議: