Set of Basic Java knowledge
Features
Duplicate elements are not allowed in Set. What is the principle? The answer is: it is differentiated by the equals () method. If you want to create a custom class object instance that does not appear in the Set, you need to override the equals method:
Here we assume that the custom class is Person:
Package Public; public class Person {private int age; private String name; public Person () {super ();} public Person (int age, String name) {super (); this. age = age; this. name = name;} // returns true for the same Object; otherwise, false @ Override public boolean equals (Object obj) {// if the address is the same, it must be the same object if (this = obj) {return true;} // if the object to be compared is not a custom Class Object (here it is Person ), certainly not the same object // think about why not write it as this. what about equals (obj ??? If (! (Obj instanceof Person) {return false;} // transformation Person p = (Person) obj using polymorphism; // compare the values of each member variable if (p. age = this. age & p. name. equals (this. name) {return true;} else {return false; }}@ Override public String toString () {return "Person [age =" + age + ", name = "+ name +"] ";}}
So far, do not be busy testing. Set is an interface. It is like this: public interface Set extends Collection, and the class that implements it should be used for its instantiation, generally, TreeSet and HashSet are used (stored Based on hash values ).
Instantiate with TreeSet:
As mentioned above, TreeSet has the sorting function. What is the implementation of this function? The answer is: The compareTo () method, that is, we need to implement the Comparable interface for the custom class and overwrite the compareTo () method:
Let's create the Person class:
Package Public; // public class Person implements Comparable
{Private int age; private String name; public Person () {super ();} public Person (int age, String name) {super (); this. age = age; this. name = name;} // The key 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 public boolean equals (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 public String toString () {return "Person [age =" + age + ", name = "+ name +"] ";}}
Test:
Package Set; import java. util. Set; import java. util. TreeSet; import Public. Person; public class TreeSetDemo {public static void main (String [] args) {Set
S = new TreeSet
(); S. add (new Person (3, "x"); // add element s. add (new Person (1, "x"); // add element s. add (new Person (1, "x"); // add duplicate element s. add (new Person (1, "y"); // add element s. add (new Person (2, "x"); // add the System element. out. println (s) ;}}/* result: [Person [age = 1, name = x], Person [age = 1, name = y], Person [age = 2, name = x], Person [age = 3, name = x] */
The results show that the added elements are not repeated and the output is ordered;
Use HashSet for instantiation
HashSet does not have sorting, so it does not implement the Comparable interface, but it must overwrite the hashCode () method. After all, people must have their own things to mark it;
Simply modify the Person class:
package Public;public class Person{ private int age; private String name; public Person() { super(); } public Person(int age, 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(!(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 public String toString() { return "Person [age=" + age + ", name=" + name + "]"; }}
Test:
Package Set; import java. util. HashSet; import java. util. Set; import Public. Person; public class HashSetDemo {public static void main (String [] args) {Set
S = new HashSet
(); S. add (new Person (1, "x"); s. add (new Person (1, "x"); s. add (new Person (1, "z"); System. out. println (s) ;}}/* result: [Person [age = 1, name = z], Person [age = 1, name = x] */
Now, let's talk about it!