Java集合教程 – Java Set集合
Set 表示唯一对象的集合。集合中元素的排序是不相关的。
集合框架提供三种类型的集合:
- 数学集
- 排序集
- 导航集
数学集
Set 接口对数学中的一组进行建模。集合是唯一元素的集合。
Java最多允许一个Set中的一个空元素。 Set 中元素的排序并不重要。
Java不保证 Set 中元素的排序。
当循环遍历 Set 的所有元素时,你得到 Set 中的每个元素一次。
集合框架提供 HashSet 类作为实现为设置接口。
以下代码显示了如何创建一个Set并向其添加元素。 当向集合添加重复元素时,它们将被忽略。
如果比较它们,则在集合中的两个元素被认为是相等的使用 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());
}
}
上面的代码生成以下结果。

LinkedHashSet
集合框架提供 LinkedHashSet 类作为 Set 接口的另一个实现类。
HashSet 不保证顺序元素。 LinkedHashSet 在插入元素时保持元素顺序。
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);
}
}
上面的代码生成以下结果。
集合操作
我们可以对集合执行并集,交集和差分运算。
// 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。要保持原始设置不变,请在操作之前复制:
Set s1Unions2 = new HashSet(s1); // Make a copy of s1 s1Unions2.addAll(s2);
要测试集合s1是否是另一个集合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));
}
}
上面的代码生成以下结果。


国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码













