Collection Frame
Collection:
|--list are ordered and repeatable, indexed, in the same order as they are taken out.
|--arraylist--array structure, fast query speed, different steps
|--linkedlist--chain list structure, adding and deleting speed, different steps
|--set unordered and non-repeating
|--hashset--Hash table structure, element unique, fast query speed, different steps
|--treeset--two fork tree structure, you can sort the elements in the set set, out of sync
Treeset Sort two ways, natural sort: element implements Comaprable interface, overrides CompareTo ()
Comparator sort: container passed in to implement comparator interface comparator, overwrite compare ()
Set : elements cannot be duplicated, without order.
Set the methods in and Collection in the same.
HashSet
@Testpublic void Test7 () {//Create a HashSet collection. HashSet hs = new HashSet () Hs.add ("CBA"), Hs.add ("NBA"), Hs.add ("NBA"), Hs.add ("NBA"), Hs.add ("haha"), Hs.add ("ABCD"); Hs.add ("Haha3"),//system.out.println ("AB"), Equals ("AB")),//system.out.println ("AB"), hashcode ()); for (Iterator it = Hs.iterator (); It.hasnext (); ) {System.out.println (It.next ());}} Print Result: unordered, non-repeating haha3 CBA NBA haha ABCD
HashSet Storing Custom objects
HashSet Sets determine whether the elements are the same, depending on the hashcode of the element and the equals method.
@Testpublic void test07 () {HashSet hs = new HashSet (); Person p = new person ("Hahahha"), Hs.add (p), Hs.add (New person ("Hahahha"), Hs.add (New person ("Hahahha", 33)); Hs.add (New person ("Hahahha")); for (Iterator it = Hs.iterator (); It.hasnext ();) {System.out.println (It.next ());}} Test result: [email protected]
public class person {private string name;private int age; public Person (string name,int age) { this.name = name; this.age = age; // System.out.println (name+ " " +age); } @Overridepublic int hashcode () { final int prime = 31;int result = 1;result = prime * result + age;result = prime * result + (name == null) 0 : name.hashcode ()); return result;} @Overridepublic boolean equals (object obj) {if (this == obj) return true;if (obj == null) return false;if (GetClass () != obj.getclass ()) return false; person other = (person) obj;if (age != other.age) return false;if (name == null) {if (other.name ! = null) Return false;} else if (!name.equals (other.name)) return false;return true;} }
TreeSet
TreeSet set judgment element is the same, according to the element of the CompareTo method or the Compare method of return 0;
There are two ways to sort TreeSet:
The first: make the elements themselves comparable. Let the element object implement the comparable port, overriding the compareTo method.
The second type: Make the container comparative. In fact, is to define a comparator, is to implement the comparator interface, covering the compare method. The comparer is passed into the construction method as a parameter.
the second ranking method is more priority than the first sort Method!!
Example Child
The first sort method, the element itself is comparative
the meaning of the return value of the Conpareto method
return=1;//to achieve order. The order of deposit is consistent with the order of extraction. Generally not.
The order in which the return=-1;//is deposited is reversed from the order taken
return=0;//can only deposit a number into the TreeSet, and the other numbers will not be stored in it .
Public class person implements comparable{private string name;private int age;public person (string name,int age) { this.name = name; this.age = age; } @Overridepublic int compareto (Object o) {if (!) ( O instanceof person)) {throw new classcastexception ("type Error");} person p = (person) o; //is compared by age. If the age is the same, the secondary condition name must be compared again. Return this.age-p.age==0?this.name.compareto (p.name): This.age-p.age;} @Overridepublic String ToString () {return "person [name=" + name + " age=" + age + "]";} } test @testpublic void test08 () {TreeseT ts = new treeset (), Ts.add (New person ("Zhang"), Ts.add (New person (" New person ("Zhang"), Ts.add ("Zhang", "Ts.add"), Ts.add (New person ("New person"); ASD ","); for (Iterator it = ts.iterator (); It.hasnext ();) {System.out.println (It.next ());}} Test Result: person [name=zhang, age=31] person [name= Zhang, age=32] person [name=asd, age=33] person  [NAME=ZHANG, AGE=33]
Second sort: make the container compare
Test @testpublic void test09 () {Treeset ts = new treeset (New Comparator () {@Overridepublic int compare (OBJECT O1, OBJECT O2) {// Compare by person's name, and if the name is the same, compare the age again. person p1 = (person) O1; person p2 = (person) o2;int temp = p1.getname (). CompareTo (P2.getname ()); return temp==0?p1.getage ()-p2.getage (): temp;}); Ts.add ("des", New person), Ts.add (New person ("Zhang"), Ts.add (New person ("Zhang", 33)); Ts.add (New person ("Zhang")), Ts.add (New person ("ASD"); for (iterator it = Ts.iterator (); It.hasnext ();) {System.out.println (It.next ());}} Test Result: person [name=asd, age=33] person [name=des, age=36] Person [name=zhang, age=31] Person [Name=zhang, age=33] General javabean public class Person {private string name;private int age; public person (String name , Int age) { this.name = name; this.age = age; } public String getname () {return name;} Public void setname (String name) {this.name = name;} Public int getage () {return age;} Public void setage (Int age) {this.age = age;} @Overridepublic string tostring () {return "Person [name=" + name + ", age=" + age + "]";} }
/*
* Exercise: Sort the string in length.
* Ideas:
* The string itself has a natural sort, but is not required by the requirements.
* only comparators can be used at this time.
*/
@Testpublic void test10 () { //does not pass in the treeset of the comparer, it is sorted by the order of the elements themselves treeset ts = new treeset (New comparator () {@Overridepublic int compare (object o1,  OBJECT O2) {String s1 = (String) O1; string s2 = (String) o2;int temp = s1.length ()-s2.length (); return temp==0? S1.compareto (S2): temp;}}); ts.add ("NBA"); ts.add ("adaddd"); ts.add ("AA"); ts.add ("AA"); ts.add ("zz"); Ts.add ("abc"), ts.add ("haha"); for (Iterator it = ts.iterator (); it.hasnext ();) {system.out.println (It.next ()); }} Test Result: element unique and sorted by length, length consistent by dictionary aa zz abc nba haha    ADADDD
Set Frame -linkedhashset
On the basis of the hash table is guaranteed to have Shun Order , the only .
Test @Testpublic void test11 () {HashSet hs = new Linkedhashset ();//element unique, ordered. Hs.add ("ABCD"), Hs.add ("NBA"), Hs.add ("haha"), Hs.add ("CBA"), Hs.add ("haha"); for (Iterator it = Hs.iterator (); It.hasnext (); ) {System.out.println (It.next ());}} Test result: ABCD NBA haha CBA
Set Frame-set