Java HashSet and TreeSet

Source: Internet
Author: User
Tags comparable set set

The set interface does not extend the collection interface, but is more stringent than the collection interface, does not add duplicate elements, and uses a hash of the storage method, so there is no order

ImportJava.util.Set;ImportJava.util.HashSet;ImportJava.util.TreeSet; Public classTest { Public Static voidMain (string[] args) {//Set allset = new HashSet ();//@ Result 1Set Allset =NewTreeSet ();//@ result 2 java.lang.ClassCastExceptionAllset.add (NewPerson ("Zhao Yiqian", 21)); Allset.add (NewPerson ("Money two", 22)); Allset.add (NewPerson ("Magozo", 23)); Allset.add (NewPerson ("John Doe", 24)); Allset.add (NewPerson ("Friday", 25)); Allset.add (NewPerson ("Friday", 25)); Allset.add ("a"); Allset.add ("a"););            System.out.println (Allset); }}classPerson {String name; intAge ; Person (String name,intAge ) {         This. Name =name;  This. Age =Age ; }         PublicString toString () {return  This. Name + This. Age; }}/*1.Set adds duplicate objects, which can be passed during compilation, but not duplicate objects, which are added only once for repeating elements. The order in which elements are added to the collection when the 2.Set set class program runs is not the order in which the collection is saved. Elements in the 3.TreeSet are stored in an orderly fashion, treeset each object's class must implement the comparable interface to use*/

The TreeSet class is considered to be the same object if a property in the comparer is not compared.

ImportJava.util.Set;ImportJava.util.HashSet;ImportJava.util.TreeSet; Public classTest { Public Static voidMain (string[] args) {Set Allset=NewTreeSet (); Allset.add (NewPerson ("Zhao Yiqian", 21)); Allset.add (NewPerson ("Money two", 22)); Allset.add (NewPerson ("Magozo", 23)); Allset.add (NewPerson ("John Doe", 24)); Allset.add (NewPerson ("Friday", 25)); Allset.add (NewPerson ("Friday", 25)); Allset.add (NewPerson ("Zheng", 25));            System.out.println (Allset); }}classPersonImplementscomparable<person> {//? Why must you write comparable<person>String name; intAge ; Person (String name,intAge ) {         This. Name =name;  This. Age =Age ; }         PublicString toString () {return  This. Name + This. Age; }       Public intcompareTo (person p) {//ObjectCompare        if( This. Age >p.age) {return100; }        if( This. Age <p.age) {return-100; }        //return 0;//It is not possible to add (Zheng, 15)//Comparison of string names        return  This. Name.compareto (P.name);                    }    }/*The elements in 1.TreeSet are stored in an orderly fashion, treeset each object's class must implement the comparable interface in order to use 2. If a property in the comparer is not compared, it is considered to be the same object.
3. The Equals () and Hashcode () methods are not written in this program. All is done by the CompareTo () method.
*/

A complete class should override the Equals (), toString (), Hashcode () method

ImportJava.util.Set;ImportJava.util.HashSet;ImportJava.util.TreeSet; Public classTest { Public Static voidMain (string[] args) {Set Allset=NewTreeSet (); Allset.add (NewPerson ("Zhao Yiqian", 21)); Allset.add (NewPerson ("Money two", 22)); Allset.add (NewPerson ("Magozo", 23)); Allset.add (NewPerson ("John Doe", 24)); Allset.add (NewPerson ("Friday", 25)); Allset.add (NewPerson ("Friday", 25)); Allset.add (NewPerson ("Zheng", 25));            System.out.println (Allset); }}classPersonImplementsComparable<person> {//class person{String name; intAge ; Person (String name,intAge ) {         This. Name =name;  This. Age =Age ; }         PublicString toString () {return  This. Name + This. Age; }             Public Booleanequals (Object obj) {if(objinstanceofPerson ) {Person P=(person) obj; if( This. Name.equals (P.name) && This. Age = =p.age) {return true; }        }        if( This==obj) {            return true; }        return false; }         Public inthashcode () {return  This. Name.hashcode () * This. Age; }       Public intcompareTo (person p) {//Age Comparison        if( This. Age >p.age) {return100; }        if( This. Age <p.age) {return-100; }        //return 0;//It is not possible to add (Zheng, 15)        return  This. Name.compareto (P.name);    }}/*1. A complete class should override Equals (), toString (), hashcode ();*/

Java HashSet and TreeSet

Related Article

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.