Characteristics
Duplicate elements are not allowed in set, according to what is the principle? The answer is: this is distinguished by the Equals () method. Then, if you want the custom class object instance to not recur in set, you need to override the Equals method:
Here we assume that the custom class is person:
Packagepublic; Public class person { Private intAgePrivateString name; Public Person() {Super(); } Public Person(intAge, String name) {Super(); This. Age = Age; This. name = name; }//The same object returns True, and vice versa returns false @Override Public Boolean equals(Object obj) {//If the address is the same, it must be the same object if( This= = obj) {return true; }//To compare objects that are not custom class objects (here is person), certainly not the same object //Think about why not write as this.equals (obj)??? if(! (objinstanceofperson)) {return false; }//Use polymorphism for transformationPerson P = (person) obj;//Compare the values of each member variable for equality if(P.age = = This. Age && P.name.equals ( This. Name)) {return true; }Else{return false; } }@Override PublicStringtoString() {return "person [age="+ Age +", name="+ name +"]"; }}
There's no need to be busy testing, set is an interface, it is this: public interface Set extends Collection, for which to instantiate the class to use to implement it, usually with treeset (with sort function), HashSet (stored according to the hash value).
Instantiate using TreeSet:
It says TreeSet has a sort function, which is based on what is implemented? The answer is: the CompareTo () method, which means that our custom classes implement the comparable interface, overwrite the CompareTo () method:
Let's build the person class:
Package public;//Focus here Public classPerson implements comparable<person>{Private intAgePrivateString name; Public Person() {super (); } Public Person(intAge, String name) {super (); This. Age = Age; This. name = name; }//Focus here@Override Public int CompareTo(Person O) {if( This. Age > O.age) {return 1; }Else if( This. Age < O.age) {return-1; }Else{return This. Name.compareto (O.name); }} @Override PublicBooleanequals(Object obj) {if( This= = obj) {return true; }if(! (obj instanceof person)) {return false; } person P = (person) obj;if(P.age = = This. Age && P.name.equals ( This. Name)) {return true; }Else{return false; }} @Override PublicStringtoString() {return "person [age="+ Age +", name="+ name +"]"; }}
Under test:
PackageSet;ImportJava.util.Set;ImportJava.util.TreeSet;ImportPublic.person; Public class treesetdemo { Public Static void Main(string[] args) {set<person> s =NewTreeset<person> (); S.add (NewPerson (3,"x"));//Add elementS.add (NewPerson (1,"x"));//Add elementS.add (NewPerson (1,"x"));//Add repeating elementsS.add (NewPerson (1,"Y"));//Add elementS.add (NewPerson (2,"x"));//Add elementSystem.out.println (s); }}/* Results: [person [Age=1, name=x], person [age=1, name=y], person [age=2, name=x] , person [age=3, name=x]]*/]
The result shows that the added elements are not duplicated and the output is orderly.
Instantiating using HashSet
HashSet does not have a sort, so can not implement the comparable interface, but to overwrite the hashcode () method, after all, people have their own things to mark it?
Simple modification of the person class:
Packagepublic; Public class person { Private intAgePrivateString name; Public Person() {Super(); } Public Person(intAge, String name) {Super(); This. Age = Age; This. name = name; }@Override Public int hashcode() {return This. Name.hashcode () * This. Age; }@Override Public Boolean equals(Object obj) {if( This= = obj) {return true; }if(! (objinstanceofperson)) {return false; } person P = (person) obj;if(P.age = = This. Age && P.name.equals ( This. Name)) {return true; }Else{return false; } }@Override PublicStringtoString() {return "person [age="+ Age +", name="+ name +"]"; }}
Test it:
PackageSet;ImportJava.util.HashSet;ImportJava.util.Set;ImportPublic.person; Public class hashsetdemo { Public Static void Main(string[] args) {set<person> s =NewHashset<person> (); S.add (NewPerson (1,"x")); S.add (NewPerson (1,"x")); S.add (NewPerson (1,"Z")); System.out.println (s); }}/* Results: [person [Age=1, name=z], person [age=1, name=x]]*/
All right, let's talk about it!
Java Basic Knowledge Set