TreeSet identifies repeated element parsing and code examples. treeset example
The underlying layer of TreeSet is TreeMap's keySet (), while TreeMap is implemented based on the red and black trees, and the red and black trees are balanced binary search trees, it ensures that the height difference between the left and right subtree of any node is no more than twice that of the shorter one.
TreeMap is sorted by key, so the elements in the TreeSet are sorted in order. Obviously, the compareTo () method is called when an element is inserted into a TreeSet. Therefore, the elements in the TreeSet must implement the Comparable interface. TreeSet is a Set that does not allow repeated elements. TreeSet uses compareTo () to judge duplicate elements, rather than equals (). Let's look at the following code.
Import java. util. treeSet; import org. junit. test; public class TestTreeSet {class Combine implements Comparable <Combine> {private int p1; private int p2; public Combine (int p1, int p2) {this. p1 = p1; this. p2 = p2 ;}@ Override public int hashCode () {return p1 * 31 + p2 ;}@ Override public Boolean equals (Object obj) {System. out. print ("whether equal" + this + "and" + obj); Boolean rect = false; if (obj instanc Eof Combine) {System. out. println ("whether equal" + this + "and" + obj); Combine other = (Combine) obj; rect = (this. p1 = other. getP1 () & this. p2 = other. getP2 ();} System. out. println (":" + rect); return rect ;}@ Override public int compareTo (Combine o) {System. out. print ("compare" + this + "and" + o); // only p1if (this. p1 <o. p1) {System. out. println (", return-1"); return-1;} else if (this. p1> O. p1) {System. out. println (", return 1"); return 1;} else {System. out. println (", return 0"); return 0 ;}@ Override public String toString () {return "(" + p1 + "," + p2 + ")";} public int getP1 () {return p1;} public void setP1 (int p1) {this. p1 = p1;} public int getP2 () {return p2;} public void setP2 (int p2) {this. p2 = p2 ;}@ Test public void test () {Combine c1 = new Combine (1, 2); Combine c2 = new Combine (1, 2); Combine c3 = new Combine (1, 3); Combine c4 = new Combine (5, 2 ); treeSet <Combine> set = new TreeSet <Combine> (); set. add (c1); set. add (c2); set. add (c3); set. add (c4); while (! Set. isEmpty () {// output the element Combine combine = set in the TreeSet in sequence. pollFirst (); System. out. println (combine. getP1 () + "\ t" + combine. getP2 ());}}}
Output:
Compare (1, 2) and (1, 2), return 0
Compare (1, 2) and (1, 2), return 0
Compare (1, 3) and (1, 2), return 0
Compare (5, 2) and (1, 2), return 1
1 2
5 2
We can see that the equals () method is not called no matter whether compareTo () returns equal or not.
Summary
The above is all the content of this article on TreeSet's determination of repeated element parsing and code examples. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!