21 booleanblnRemoved = hSet.remove(newInteger("2"));
22 System.out.println(hSet.contains(newInteger("2")));
23 System.out.println(blnRemoved);
24 System.out.println(hSet);
25 hSet.clear();
26 System.out.println(hSet);
27 System.out.println(hSet.isEmpty());
28 }
29
30 publicstaticvoidshowToArrayListAndArray() {
31 HashSet
32 hs.add("B");
33 hs.add("A");
34 hs.add("D");
35 System.out.println(hs);
36 List
37 Object[] objects = list.toArray();
38 for(Object obj : objects)
39 System.out.println("Object = " + obj);
40 String[] strs = list.toArray(newString[0]);
41 for(String str : strs)
42 System.out.println("String = " + str);
43 }
44
45 publicstaticvoidshowToHashSetAndRemoveDuplicate() {
46 Integer[] numbers = { 7, 7, 8, 9, 10, 8, 8, 9, 6, 5, 4 };
47 List
48 Set
49 for(Integer o : set)
50 System.out.print(o + ", ");
51 }
52
53 publicstaticvoidshowMinMax() {
54 HashSet
55 hashSet.add(newLong("9"));
57 hashSet.add(newLong("2"));
58 hashSet.add(newLong("2"));
59 hashSet.add(newLong("3"));
60 Object objMin = Collections.min(hashSet);
61 System.out.println("Minimum Element of Java HashSet is : " + objMin);
62 Object objMax = Collections.max(hashSet);
63 System.out.println("Maximum Element of Java HashSet is : "+ objMax);
64 }
65
66 publicstaticvoidshowSynchronizedSet() {
67 HashSet hashSet = newHashSet();
68 Set set = Collections.synchronizedSet(hashSet);
69 //set之后的并发操作将不再需要synchronized关键字来进行同步了。
70 }
7. TreeSet:该集合为有序集合,数据在插入集合后便按照自定义的排序规则将插入的对象进行排序,该集合主要是通过两种方式排序插入对象的,一种是要求集合元素类必须是Comparable
1 publicclassItem implementsComparable
2 publicintcompareTo(Item other) {
3 returnpartNumber - other.partNumber;
4 }
5 }
6 publicstaticvoidmain(String[] args) {
7 SortedSet
8 parts.add(newItem("Toaster",1234));
9 parts.add(newItem("Widget",4562));
10 parts.add(newItem("Modem",9912));
11 System.out.println(parts);
12
13 SortedSet
14 publicintcompare(Item a,Item b) {
15 String descA = a.getDescription();
16 String descB = b.getDescription();
17 returndescA.compareTo(descB);
18 }
19 });
20 sortByDescription.addAll(parts);
21 System.out.println(sortByDescription);
22 }
8. PriorityQueue(优先级对象): 该容器也是有序集合,和TreeSet不同的是,该集合只是保证当从集合的头部取出数据