The set interface does not extend the collection interface, but is more stringent than the collection interface, does not add duplicate elements, and uses a hash of the storage method, so there is no order
ImportJava.util.Set;ImportJava.util.HashSet;ImportJava.util.TreeSet; Public classTest { Public Static voidMain (string[] args) {//Set allset = new HashSet ();//@ Result 1Set Allset =NewTreeSet ();//@ result 2 java.lang.ClassCastExceptionAllset.add (NewPerson ("Zhao Yiqian", 21)); Allset.add (NewPerson ("Money two", 22)); Allset.add (NewPerson ("Magozo", 23)); Allset.add (NewPerson ("John Doe", 24)); Allset.add (NewPerson ("Friday", 25)); Allset.add (NewPerson ("Friday", 25)); Allset.add ("a"); Allset.add ("a");); System.out.println (Allset); }}classPerson {String name; intAge ; Person (String name,intAge ) { This. Name =name; This. Age =Age ; } PublicString toString () {return This. Name + This. Age; }}/*1.Set adds duplicate objects, which can be passed during compilation, but not duplicate objects, which are added only once for repeating elements. The order in which elements are added to the collection when the 2.Set set class program runs is not the order in which the collection is saved. Elements in the 3.TreeSet are stored in an orderly fashion, treeset each object's class must implement the comparable interface to use*/
The TreeSet class is considered to be the same object if a property in the comparer is not compared.
ImportJava.util.Set;ImportJava.util.HashSet;ImportJava.util.TreeSet; Public classTest { Public Static voidMain (string[] args) {Set Allset=NewTreeSet (); Allset.add (NewPerson ("Zhao Yiqian", 21)); Allset.add (NewPerson ("Money two", 22)); Allset.add (NewPerson ("Magozo", 23)); Allset.add (NewPerson ("John Doe", 24)); Allset.add (NewPerson ("Friday", 25)); Allset.add (NewPerson ("Friday", 25)); Allset.add (NewPerson ("Zheng", 25)); System.out.println (Allset); }}classPersonImplementscomparable<person> {//? Why must you write comparable<person>String name; intAge ; Person (String name,intAge ) { This. Name =name; This. Age =Age ; } PublicString toString () {return This. Name + This. Age; } Public intcompareTo (person p) {//ObjectCompare if( This. Age >p.age) {return100; } if( This. Age <p.age) {return-100; } //return 0;//It is not possible to add (Zheng, 15)//Comparison of string names return This. Name.compareto (P.name); } }/*The elements in 1.TreeSet are stored in an orderly fashion, treeset each object's class must implement the comparable interface in order to use 2. If a property in the comparer is not compared, it is considered to be the same object.
3. The Equals () and Hashcode () methods are not written in this program. All is done by the CompareTo () method.
*/
A complete class should override the Equals (), toString (), Hashcode () method
ImportJava.util.Set;ImportJava.util.HashSet;ImportJava.util.TreeSet; Public classTest { Public Static voidMain (string[] args) {Set Allset=NewTreeSet (); Allset.add (NewPerson ("Zhao Yiqian", 21)); Allset.add (NewPerson ("Money two", 22)); Allset.add (NewPerson ("Magozo", 23)); Allset.add (NewPerson ("John Doe", 24)); Allset.add (NewPerson ("Friday", 25)); Allset.add (NewPerson ("Friday", 25)); Allset.add (NewPerson ("Zheng", 25)); System.out.println (Allset); }}classPersonImplementsComparable<person> {//class person{String name; intAge ; Person (String name,intAge ) { This. Name =name; This. Age =Age ; } PublicString toString () {return This. Name + This. Age; } Public Booleanequals (Object obj) {if(objinstanceofPerson ) {Person P=(person) obj; if( This. Name.equals (P.name) && This. Age = =p.age) {return true; } } if( This==obj) { return true; } return false; } Public inthashcode () {return This. Name.hashcode () * This. Age; } Public intcompareTo (person p) {//Age Comparison if( This. Age >p.age) {return100; } if( This. Age <p.age) {return-100; } //return 0;//It is not possible to add (Zheng, 15) return This. Name.compareto (P.name); }}/*1. A complete class should override Equals (), toString (), hashcode ();*/
Java HashSet and TreeSet