public static void main(String[] args) {
// HashSet
HashSet hs = new HashSet();
System.out.print("集合中是否不含元素:");
System.out.println(hs.isEmpty());
hs.add(112);
hs.add(118);
hs.add(123);
hs.add(456);
hs.add(231);
System.out.print("集合中是否存在123:");
System.out.println(hs.contains(123));
System.out.println("----for...each循环遍历set集合----");
for (Integer str : hs) {
System.out.println(str);
}
System.out.println("----删除123----");
hs.remove(123);
System.out.println("----迭代器遍历HashSet集合----");
Iterator it = hs.iterator();
while (it.hasNext()) {
int str = it.next();
System.out.println(str);
}
// TreeSet
TreeSet ts = new TreeSet<>();
ts.add(112);
ts.add(118);
ts.add(456);
ts.add(123);
ts.add(231);
System.out.println("----迭代器遍历TreeSet集合----");
Iterator ti = ts.iterator();
while (ti.hasNext()) {
int str = ti.next();
System.out.println(str);
}
System.out.println("----TreeSet集合已经自动排序----");
}