Set interface is Collection Interface Sub-interface, Set duplicate elements cannot be inserted in an interface
Set Common subclasses of interfaces:
HashSet is a Set a subclass of the interface, features: it can not store duplicate elements, and the use of hash storage, so there is no order.
Treeset is also Set a subclass of an interface, characterized by the inability to store repeating elements and to store them in an orderly manner.
TreeSet is ordered, so you need to set up the collation, TreeSet the class in which each object resides must implement the compatable The interface can be used normally;
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) {//judgment is not the same object
return true;
}
if (! ( Obj instanceofpersonal)) {//Judging 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);
Using a custom class object 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);
Using the custom class personal as an element to implement the HashSet function
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