Java Collection Framework set set HashSet TreeSet

Source: Internet
Author: User
Tags comparable set set

Set:

Features: unordered, variable in length, non-repeatable.

the realization of HashSet

For HashSet, it is based on HASHMAP implementation, HashSet the bottom of the HashMap to save all elements, so HashSet implementation is relatively simple. the underlying data structure is a hash table.

HashSet The uniqueness of the element is ensured by the two methods of the element, hashcode and equals.

-----Determines if the equals is true if the hashcode value of the element is the same.

----- If the hashcode value of an element is different, equals is not called.

----- Note : For actions that determine whether an element exists, and delete it, the method of dependency is the hashcode and equals methods.

Import Java.util.hashset;import java.util.Iterator;
public class Hashsetdemo {
  public static void Main (string[] args) {//TODO auto-generated method stub HashSet hs = new HashSet ();  Hs.add (New Students ("V7", "Java"));  Hs.add (New Students ("v7s", "Java"));  System.out.println (Hs.contains (New Students ("V7", "Java"));  Iterator Iterator = Hs.iterator ();   while (Iterator.hasnext ()) {Students SS = (Students) iterator.next ();  System.out.println (Ss.getname () + "----" + ss.getbook ()); } }
}
Class Students {public string name;
@Override public boolean equals (Object o) {if (this = = O) {return true; }
if (o.getclass () = = Students.class) {Students n = (Students) o; Return n.book.equals (book);  Only judge the book Field//return n.book.equals (book) && n.name.equals (name); } return false;
}
@Override public int hashcode () {//TODO auto-generated Method stub return This.book.hashCode ();}
Public Students (string name, string book) {//TODO auto-generated method Stub this.name = name; This.book = Book; }
Public String GetName () {return name;}
public void SetName (String name) {this.name = name;}
Public String GetBook () {return book;}
public void Setbook (String book) {this.book = Book;}
}


TreeSetis to rely onTreeMapTo achieve.
TreeSetis an ordered set, the elements in the TreeSet are arranged in ascending order, by default, by natural sorting,means theElements in the TreeSet to beImplementcomparableInterface. Or there is a custom comparer.
We can pass the implementation when constructing the TreeSet objectComparatorinterface ofComparatorObject.

/**
*
*treeset: You can sort the elements in the set collection.
* Ground levelData StructureIs Two fork Tree
* The basis for ensuring the uniqueness of the element.
* CompareTo method return 0
*
* The first way to sort the TreeSet: to make the elements themselves comparable
* Elements need to implement Compareable interface, overwrite CompareTo method.
*
*
* TreeSet's second sorting method:
* When the elements themselves are not comparable, or have a comparative is not required.
* Then you need to make the collection itself comparable.
*
*/

TreeSet the first way to sort:
Import Java.util.comparator;import Java.util.iterator;import Java.util.treeset;public class TreeSetDemo {public static void Main (string[] args) {//TODO auto-generated method Stubtreeset ts = new TreeSet (); Ts.add (New Student ("v1", 10) ), Ts.add (New Student ("V2", one)), Ts.add (New Student ("V3"), Ts.add (New Student ("V4")), Ts.add (New Student ("V1", ); Iterator ite = Ts.iterator (); while (Ite.hasnext ()) {Student St = (Student) ite.next (); System.out.println (St.getname () + "<-->" +st.getage ());}}} Class Student implements comparable{//implements the comparable interface, forcing the element to have a comparative private String name;private int age;public Student (stri Ng Name,int Age) {//TODO auto-generated constructor stubthis.name = Name;this.age = age;} @Overridepublic int compareTo (Object o) {//TODO auto-generated method stubif (! o instanceof Student)) throw new RuntimeException ("Not a student class object! "); Student stu = (Student) o; System.out.println (stu.age+ "=========compareto () ====>" +stu.name);//main factor if (This.age > Stu.age) {return 1;} else if (this.Age = = Stu.age) {//Minor factor return This.name.compareTo (Stu.name);} return-1;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}}

The second sort of TreeSet: The second sort of TreeSet:
Import Java.util.comparator;import Java.util.iterator;import Java.util.treeset;public class TreeSetDemo {public static void Main (string[] args) {//TODO auto-generated method Stubtreeset ts = new TreeSet (new Mycompa ()); Ts.add (New Stud ENT ("v1"), Ts.add (New Student ("V2", one)), Ts.add (New Student ("V3"), Ts.add (New Student ("V4"); Ts.add (new Student ("v1"), Iterator ite = Ts.iterator (); while (Ite.hasnext ()) {Student St = (Student) ite.next (); System.out.println (St.getname () + "<-->" +st.getage ());}}} Class Mycompa implements comparator{@Overridepublic int Compare (object O1, Object O2) {Student st1 = (Student) O1; Student st2 = (Student) o2;int num = St1.getname (). CompareTo (St2.getname ()); if (num = = 0) {return new Integer (St1.getage ()) . CompareTo (New Integer (St2.getage ()));} return num;}} Class Student{private string name;private int age;public Student (string Name,int Age) {//TODO auto-generated constructor Stubthis.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.) {this.age = age;}}


Java Collection Framework set set HashSet TreeSet

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.