1. Overview of Set Interface
Set does not store duplicate elements (how do you judge that the elements are the same?). If you try to add multiple instances of the same object to the Set, it prevents this duplication. The most common use of Sets is to test attributes, and you can easily ask whether an object is in a Set. Because of this, lookup becomes the most important operation in Set, so you usually choose a HashSet implementation that optimizes fast lookup.
Set has exactly the same interface as Collection, so there is no additional functionality, unlike the previous List. In fact, a Set is a Collection, but it behaves differently. This is a typical application of inheritance and polymorphism: manifesting different behaviors. Set determines attributes based on the value of the object.
java.util Interface Set<E> |
Parametric type E: The type of elements maintained by this collection All superclass interfaces:Collection <E>, Iterable <E> All subclass interfaces:NavigableSet <E>, SortedSet <E> All known implementation classes: AbstractSet , ConcurrentHashMap.KeySetView , ConcurrentSkipListSet , CopyOnWriteArraySet , EnumSet , HashSet , JobStateReasons ,LinkedHashSet,TreeSet |
2.Set Common Implementation Classes
(1)HashSet
HashSet inherits the AbstractSet class and implements Set, Cloneable, Serializable interfaces. Supported by hash tables (actually a HashMap instance, based on
HashMap is implemented, and the underlying layer uses HashMap to save elements.) It's natural to improve search efficiency.
![]() |
![]() |
Construction method |
![]() |
All methods |
![]() |
(2)TreeSet
TreeSet inherits the AbstractSet class and implements Navigable Set, Cloneable, Serializable interfaces. As HashSet is implemented based on HashMap, TreeSet is also implemented based on TreeMap. Because of Tree's support, TreeSet's greatest feature is sorting, and its function is to provide ordered set of Sets.
![]() |
Parameter type E - Types of elements maintained by this collection All Implemented Interfaces (Implementation Interfaces): Serializable ,Cloneable ,Iterable <E>,Collection <E>,NavigableSet <E> Set <E> SortedSet <E> |
Construction method |
![]() |
common method |
(3)LinkedHashSet
LinkedHashSet integrates linked list + hash table, determines the storage location of elements according to the hashCode value of elements, and uses linked list to maintain the order of elements.
When traversing the collection, LinkedHashSet accesses the elements of the collection in the order in which they are added.
For LinkedHashSet, it inherits from HashSet and is based on LinkedHashMap.
![]() |
public class LinkedHashSet<E> extends
HashSet<E>Parametric type E -- The type of elements maintained by this collection All Implemented Interfaces (Implementation Interfaces): Serializable , Cloneable , Iterable <E>, Collection <E>, Set <E> |
Construction method |
![]() |
Method (basically inheritance method) |
![]() |
3.set demo
(1)HashSet
1 public class HashSetDemo { 2 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 8 //1. Create a Set container object. 9 Set set = new HashSet();//If it should be LinkedHashSet, order can be achieved. 10 11 //2. Add elements. 12 set.add("abc"); 13 set.add("nba"); 14 set.add("heihei"); 15 set.add("haha"); 16 set.add("heihei"); 17 18 //3. It can only be taken out by iterator. 19 for (Iterator it = set.iterator(); it.hasNext();) { 20 System.out.println(it.next()); 21 } 22 23 } 24 25 } 26
(2)TreeSet
1 public class TreeSetDemo2 { 2 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 8 //Initialize the TreeSet collection to specify a comparator. 9 Set set = new TreeSet(new ComparatorByName()); 10 11 set.add(new Student("xiaoqiang",20)); 12 set.add(new Student("daniu",24)); 13 set.add(new Student("xiaoming",22)); 14 set.add(new Student("tudou",18)); 15 set.add(new Student("daming",22)); 16 set.add(new Student("dahuang",19)); 17 18 for (Iterator it = set.iterator(); it.hasNext();) { 19 Student stu = (Student)it.next(); 20 System.out.println(stu.getName()+"::"+stu.getAge()); 21 } 22 } 23 24 } 25 /**///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 26 * Customize a comparator to sort student objects by name. 27 * @author Administrator 28 * 29 */ 30 public class ComparatorByName extends Object implements Comparator { 31 32 @Override 33 public int compare(Object o1, Object o2) { 34 35 Student s1 = (Student)o1; 36 Student s2 = (Student)o2; 37 38 int temp = s1.getName().compareTo(s2.getName()); 39 40 return temp==0?s1.getAge()-s2.getAge():temp; 41 } 42 43 } 44 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 45 public class Student implements Comparable { 46 47 private String name; 48 private int age; 49 50 public Student() { 51 super(); 52 53 } 54 public Student(String name, int age) { 55 super(); 56 this.name = name; 57 this.age = age; 58 } 59 60 61 62 /** 63 * Override the hashCode method. Hash values are defined according to the characteristics of the object itself. 64 */ 65 public int hashCode(){ 66 final int NUMBER = 37; 67 return name.hashCode() + age*NUMBER; 68 } 69 70 /** 71 * It is necessary to define the basis on which the object itself judges the same content. Cover equals method. 72 * 73 */ 74 public boolean equals(Object obj){ 75 76 if(this == obj){ 77 return true; 78 } 79 80 if(!(obj instanceof Student)){ 81 throw new ClassCastException("error in type"); 82 } 83 84 Student stu = (Student)obj; 85 86 return this.name.equals(stu.name)&& this.age == stu.age; 87 } 88 89 public String getName() { 90 return name; 91 } 92 public void setName(String name) { 93 this.name = name; 94 } 95 public int getAge() { 96 return age; 97 } 98 public void setAge(int age) { 99 this.age = age; 100 } 101 @Override 102 public String toString() { 103 return "Student [name=" + name + ", age=" + age + "]"; 104 } 105 /** 106 * Students have a comparative function. This function is a natural sort method. 107 * The natural order is mainly ascending order of age. 108 */ 109 @Override 110 public int compareTo(Object o) { 111 112 Student stu = (Student)o; 113 // System.out.println(this.name+":"+this.age+"........"+stu.name+":"+stu.age); 114 /* 115 if(this.age>stu.age) 116 return 1; 117 if(this.age<stu.age) 118 return -1; 119 return 0; 120 */ 121 122 /* 123 * Since the same name and age are the same person, there are two elements to be judged as repetitive elements. 124 * Since it's sorted by age. So first judge your age. In judging names. 125 */ 126 int temp = this.age - stu.age; 127 128 return temp==0?this.name.compareTo(stu.name):temp; 129 130 } 131 132 133 } 134
2018-01-05
Contents from API 1.8, Java Programming Thoughts, Intelligence Podcast Course