Java class set-set

Source: Internet
Author: User

Set interface is Collection Interface Sub-interface, Set cannot insert repeating element in interface

Set frequently used subclasses of interfaces:

HashSet is a Set a subclass of the interface. Features: The inside can not store repeated elements, and the use of a hash of the storage mode. So there is no order.

Treeset is also Set a subclass of the interface. Features: The inside can not store repeated elements, and is ordered to store

TreeSet is stored in order. So it is necessary to set up a good collation,TreeSet Each object in the class must be implemented compatable interface talent enough normal use;

Packageleiji;

Publicclass Person implements comparable<person> {

private String name;

private int age;

Public person (String Name,int age) {

This.name=name;

This.age=age;

}

Public String ToString () {//Overwrite toString method

Return "Name:" +name+ "; Age:" +age;

}

public int CompareTo (person per) {//Overwrite CompareTo method

if (this.age>per.age) {

return 1;

}

else if (this.age<per.age) {

return-1;

}

else{

Return This.name.compareTo (Per.name);

}

}

}

Packageleiji;

Publicclass Personal {

private String name;

private int age;

Public Personal (String Name,int age) {

This.name=name;

This.age=age;

}

Public String ToString () {//Overwrite toString method

Return "Name:" +name+ "; Age:" +age;

}

public boolean equals (Object obj) {//Overwrite Equals method

if (this==obj) {//inference is not the same object

return true;

}

if (! ( Obj instanceofpersonal)) {//inference is not the same class

return false;

}

personalp= (Personal) obj; Make a downward transition

if (This.name.equals (p.name) && this.age==p.age) {//

return true;

}else{

return false;

}

}

public int hashcode () {//overwrite hashcode function

Returnthis.name.hashCode () *this.age; Specify encoding format

}

}

Packageleiji;

Importjava.util.Set;

Importjava.util.HashSet;

Importjava.util.TreeSet;

Publicclass Sett {

public static void Main (String args[]) {

Set<string> allset=new hashset<string> ();

Allset.add ("A");

Allset.add ("M");

Allset.add ("D");

Allset.add ("F");

Allset.add ("F");

System.out.println (Allset);

Set<string> sortset=new treeset<string> ();

Sortset.add ("F");

Sortset.add ("M");

Sortset.add ("A");

Sortset.add ("D");

System.out.println (Sortset);

Use a class object of your own definition as an element

Set<person> alls=new treeset<person> ();

Alls.add (New person ("Zhang San", 30));

Alls.add (New person ("Zhang San", 32));

Alls.add (New person ("Zhang San", 30));

Alls.add (New person ("John Doe", 30));

System.out.println (alls);

Implement HashSet function by defining class personal as an element

Set<personal> allse=new hashset<personal> ();

Allse.add (New Personal ("Zhang San", 30));

Allse.add (New Personal ("John Doe", 30));

Allse.add (New Personal ("Zhang San", 30));

Allse.add (New Personal ("Zhao Liu", 30));

Allse.add (New Personal ("Xu Zheng Does", 30));

System.out.println (Allse);

}

}

// a good Object class Best Overwrite Object Class of hashcode () equals () toString () Three methods

Java class set-set

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.