A. Set set
Set set, which resembles a jar, "throw in" set, and there is no obvious order between objects in the collection. The set set is basically exactly the same as collection, and it does not provide an additional method. The set is actually collection, but the behavior is slightly different (set does not allow repeating elements).
The set collection does not allow the inclusion of the same element, and if an attempt is made to add two identical elements to the same set set, the addition fails, the Adding method returns False, and the new element is not added.
The set determines that two objects are the same, not using the = = operator, but according to the Equals method. That is, as long as two objects are returned true,set with the equals method , the two objects will not be accepted, and conversely, as long as two objects are compared with the equals method to return false. Set is a good place to accept these two objects (even those two objects are the same object, set can also treat them as two objects).
The following uses a simple set demonstration program:
Import java.util.*; Public classsettest{ Public Static voidMain (string[] args) {Set books=NewHashSet (); //Add a String objectBooks.add (NewString ("Dark Horse Programmer")); //add a String object again,//because two string objects compare equality through the Equals method,//so add failed, return falseBoolean result = Books.add (NewString ("Dark Horse Programmer")); //The following output sees only one element of the collectionSystem. out. println (Result +" -"+books); }}
Operation Result:
false-->[Black Horse Programmer]
Java Foundation--java Collection (iii)