The elements in set are unordered (the order in which they are deposited and taken out is not necessarily consistent), and the elements cannot be duplicated.
The methods in set are the same as those in collection.
Common sub-categories: HashSet TreeSet
HashSet the underlying data structure is a hash table
TreeSet the underlying data structure is a two-fork tree
Import java.util.hashset;import java.util.iterator;class person{private String name;private int age;public person ( String name, int age) {super (); this.name = Name;this.age = age;} Set and get Methodspublic String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}} public class Setdemo {public static void main (string[] args) {//TODO auto-generated method Stub//method1 (); Method2 ();} public static void Method1 () {HashSet hs = new HashSet () Hs.add ("n"); Hs.add (""); Hs.add ("04");//disordered SOP ( HS), Hs.add ("02"),//Unique SOP (HS), SOP ("Whether to add in:" + hs.add ("02"));} /** * Deposit The custom object, if the name and age are the same as the same person, repeat the element */public static void Method2 () {HashSet hs = new HashSet (); Hs.add (New Person ("01", 10)); Hs.add (New person ("one")), Hs.add (New person (""), Hs.add (New person ("04", 13));/* Print Iterator it = Hs.iterator (); while (It.hasnext ()) {Person person = (person) it.next (); SOP (Person.getname () + "::" +person.getage());} *///adds an identical element hs.add (new person ("n")), Iterator it = Hs.iterator (); while (It.hasnext ()) {Person person = (person) It.next (); SOP (Person.getname () + "::" +person.getage ()); No problem. Two 01::10}public static void Sop (Object object) {System.out.println (object);}}
We require the same name and age to be considered as repeating elements, which should not be preserved according to the set's characteristics, and the result is saved ...
Workaround: Override the Hashcode method of the element to establish its own hash value while overriding the Equals method.
The Hashcode method for overriding elements is because, in the set, the element is first judged to be the hash value of the element, and the element is deposited only if the hash value of the element is not present.
Overriding the Equals method is because the hash value of the two elements is the same, but the two elements are not necessarily the same, so make a decision and determine, based on the return value, whether the element will eventually be deposited.
Package Ssssssss;import Java.util.hashset;import java.util.iterator;class person {private String name;private int age; Public person (String name, int.) {super (); this.name = Name;this.age = age;} public boolean equals (Object obj) {//TODO auto-generated method stubif (! ( obj instanceof person) return false; Person person = (person) obj;//print indicates that this method is called SYSTEM.OUT.PRINTLN (this.name + "******equals*****" + person.name); return th Is.name.equals (person.name) && this.age = = person.age;} Override the Hashcode method to establish the person's own hash value public int hashcode () {//TODO auto-generated method StubSystem.out.println (THIS.name +): ... hashcode "); return 13;//set the hash value//return Name.hashcode () + age, according to the condition;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}} public class Setdemo {public static void main (string[] args) {//TODO auto-generated method Stubmethod2 ();} /** * Deposit A custom object if the name and age are the same as the same person, repeating the element */puBlic static void Method2 () {HashSet hs = new HashSet (), Hs.add (New person (""), Hs.add (New person ("one")), Hs.add ( New Person ("Hs.add"); * * (New person ("04", 13));/* Print Iterator it = Hs.iterator (); while (It.hasnext ()) {Person person = ( person) It.next (); SOP (Person.getname () + "::" +person.getage ());} *///adds an identical element hs.add (new person ("n")), Iterator it = Hs.iterator (); while (It.hasnext ()) {Person person = (person) It.next (); SOP (Person.getname () + "::" +person.getage ()); No problem. Two 01::10//overwrite hashcode method, establish own hash value}public static void Sop (Object object) {System.out.println (object);}}
Printing results:
01..........hashcode02..........hashcode02******equals*****0103..........hashcode03******equals*****0103****** equals*****0204..........hashcode04******equals*****0104******equals*****0204******equals*****0301 ..... Hashcode01******equals*****0101::1002::1103::1204::13
Returns an identical hash value, which is not difficult to guess by printing the process of running this program:
01 Mister into a hash value because there are only 11 elements at this time, so deposit
02 is regenerated into an identical hash value, and then compared with 01, not the same element, deposited
03 is regenerated to the same hash value, then compared with 01 and 02 to determine that there are no identical elements, deposited
04 is also the same, with 01 02 03 After the comparison, deposited
Then the repetition of element 01, the comparison found with the first deposit of 01 is the same,
Returns false, not deposited.
Conclusion:
How does HashSet guarantee the uniqueness of the elements?
Two ways to pass elements: Hashcode and equals
The Equals method is only judged when the hash value of the element is equal
If you generate a different hash value, you can significantly reduce the number of times the program runs.
Package Ssssssss;import Java.util.hashset;import java.util.iterator;class person {private String name;private int age; Public person (String name, int.) {super (); this.name = Name;this.age = age;} public boolean equals (Object obj) {//TODO auto-generated method stubif (! ( obj instanceof person) return false; Person person = (person) obj;//print indicates that this method is called SYSTEM.OUT.PRINTLN (this.name + "******equals*****" + person.name); return th Is.name.equals (person.name) && this.age = = person.age;} Override the Hashcode method to establish the person's own hash value public int hashcode () {//TODO auto-generated method StubSystem.out.println (THIS.name +): ... hashcode ");//return 13;//set hash value by condition return Name.hashcode () + age;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}} public class Setdemo {public static void main (string[] args) {//TODO auto-generated method Stubmethod2 ();} /** * Deposit A custom object if the name and age are the same as the same person, repeating the element */puBlic static void Method2 () {HashSet hs = new HashSet (), Hs.add (New person (""), Hs.add (New person ("one")), Hs.add ( New Person ("Hs.add"); * * (New person ("04", 13));/* Print Iterator it = Hs.iterator (); while (It.hasnext ()) {Person person = ( person) It.next (); SOP (Person.getname () + "::" +person.getage ());} *///adds an identical element hs.add (new person ("n")), Iterator it = Hs.iterator (); while (It.hasnext ()) {Person person = (person) It.next (); SOP (Person.getname () + "::" +person.getage ()); No problem. Two 01::10//overwrite hashcode method, establish own hash value}public static void Sop (Object object) {System.out.println (object);}}
Java------Set set and its subclasses HashSet