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