Java Collection framework
Collection framework Collection | -- List set. elements are ordered (stored and retrieved) and can have repeated elements because the Collection system has an index. | -- ArrayList set. The underlying data is an array structure. Features: Fast query and slow addition/deletion. The thread is not synchronized. | -- Sort list set. The underlying data is a linked list structure. Features: Fast addition, deletion, and slow query. Threads are not synchronized | -- Vector set. The underlying data is an array structure. The thread is synchronous. It has been replaced by the ArrayList set. | -- Set. elements are unordered (stored and retrieved) and cannot have repeated elements. | -- HashSet set. The underlying data is the hash table structure. The uniqueness of elements in a HashSet set is determined by the hashCode () and equals () methods. If the hash value of an element is different, it is determined whether equals is true. | -- TreeSet Set. The underlying data is a binary tree table structure. Elements in the Set can be sorted in a certain order. To ensure the uniqueness of elements, the compareTo () method returns 0. The first method of TreeSet sorting is to make the objects in the TreeSet set have a comparison. Custom objects must implement the comparable interface to overwrite the compareTo () method. In sorting, when the main conditions are the same, you must determine the secondary conditions. The second method of TreeSet sorting: When the elements do not have the comparison, the TreeSet set must have the comparison. When the set is initialized, a comparison method is available. Define a class to implement the comparator interface, overwrite the compare () method, and use return 0 to ensure the same. If both sorting types exist, 2nd sorting types are dominant.
Import java. util. *; class TreeSetDemo2 {public static void main (String [] args) {TreeSet ts = new TreeSet (new MyCompare (); // The add method is unique to the Collection framework, add an element to the container. The element is the new object. // to add three elements, you need to add three new elements. You should write ts as follows. add (new Student ("Li Si 1", 21); ts. add (new Student ("Li Si 4", 26); ts. add (new Student ("Li Si 2", 22); ts. add (new Student ("Li Si 4", 25); ts. add (new Student ("Li Si 3", 23); Iterator it = ts. iterator (); while (it. hasNext () {Student su = (Student) it. next (); System. out. println (su. getName () + "," + su. getAge () ;}} class Student implements Comparable // This interface forces the implementation of the subclass local comparison {private int age; private String name; Student (String name, int age) {this. name = name; this. age = age;} // rewrite the compareTo () method public int compareTo (Object obj) in the Comarapble interface {// determine whether the class is a student class. If not, an exception is thrown. // Return 1; if (! (Obj instanceof Student) throw new RuntimeException ("not a Student object! "); Student su = (Student) obj; // sort by age if (this. age-su. age> 0) return 1; else if (this. age-su. age <0) return-1; else {// System. out. println ("asd:" + this. name. compareTo (su. name); return this. name. compareTo (su. name) ;}} public String getName () {return name;} public int getAge () {return age ;}} // customize a Comparator class MyCompare implements Comparator {public int compare (Object o1, Object o2) {// determine if (! (O1 instanceof Student & o2 instanceof Student) throw new RuntimeException ("compared to students! "); Student s1 = (Student) o1; Student s2 = (Student) o2; int num = s1.getName (). compareTo (s2.getName (); // when the name is equal, determine the age if (num = 0) {return s1.getAge ()-s2.getAge ();} else return num ;}}