java-Collection (iii)

Source: Internet
Author: User

Set set: Elements are ordered, element values are unique, and duplicates are not allowed.
HashSet: The underlying data structure hash table, is the element's hashcode and equals to ensure the uniqueness of the element.
If the hashcode value of the element is the same, it will determine if the equals is true;
If the value of the element's hashcode is different, equals is not called.

For operations that determine whether an element exists, and delete it, the dependent method is the hashcode of the element and the Equals method.

TreeSet: The elements in the set collection can be naturally sorted. The underlying data is a two-fork tree

Sort by: To make a custom object implement the comparable interface, force the object to be comparative. , when the main condition is the same, be sure to judge the next condition.
Then rewrite the CompareTo () method.
If you want to output in the same order, let the CompareTo () method return 1.

Sort by two: when the element itself is not comparable, or the comparative is not required, then it is necessary to make the collection itself comparative.
When the collection is initialized, let the collection be comparative.
Defines a comparer that passes the comparer as a parameter to the constructor of the TreeSet collection.

HashSet using:
Use:
HashSet hs=new HashSet ();
Hs.add ();

Iterator It=hs.iterator ();
while (It.hasnext) {
It.next ();
}

Save a custom object to the HashSet collection, with the same name and age as the same person.

Replication Equals and Hashcode methods.

public boolean equals (Object obj) {
if (! ( obj.instanceof person))
return false;
Person p= (person) obj;
Return This.getname (). Equals (P.getname ()) &&this.getage () ==p.getage ();
}

Replication Hashcode Method
public int hashcode () {
return name.hashcode+age*12;
}

TreeSet using:

Use:
TreeSet ts=new TreeSet ();
Ts.add ();

Iterator It=ts.iterator ();
while (It.hasnext) {
It.next ();
}

TreeSet stores Custom objects: stores custom objects in the TreeSet collection and sorts them according to their age.

Sort by: Make elements comparable
To make a custom object implement the comparable interface, force the object to be comparative. , when the main condition is the same, be sure to judge the next condition.
Then rewrite the CompareTo () method.
Cases:

public int compareTo (Object obj)
{
if (!obj instanceof Student)
throw new RuntimeException ("not student");
Student s= (Student) obj;

if (this.age>s.age)
return 1;
if (this.age==s.age)
Return This.name.compareTo (S.name);
return-1;
}


Sort by two: Define a class to implement the comparator interface, overriding the Compare method. Make collections comparable

TreeSet ts=new TreeSet (New Mycompare ());

Class Mycompare implements Comparator

public int Compare (Object o1,object O2)
{
Student s1= (Student) O1;
Student s2= (Student) O2;

int Num=s1.getname (). CompareTo (S2.getname ());
if (num==0)
return new Integer (S1.getage ()). CompareTo (New Integer (S2.getage ()));

}






java-Collection (iii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.