"Understanding Java Collection (ii)"--Set set

Source: Internet
Author: User
Tags comparable object object set set sorts

The previous article describes the general knowledge of set sets. The Set collection contains three more important implementation classes: HashSet, TreeSet, and Enumset. This article will focus on these three classes.

  

I. Introduction of HashSet class HashSet

HashSet is a typical implementation of the set interface, implements all the methods in the set interface, and does not add additional methods, most of the time using the set set is the implementation class. HashSet stores the elements in the collection by the hash algorithm. Therefore, it has good access and lookup performance.

HashSet Features

1. The order of elements cannot be guaranteed, the order may differ from the order of addition, and the order may vary.
2.HashSet is not synchronous, and if more than one thread accesses a hashset at the same time, it must be synchronized by code.
3. The collection element value can be null.
In addition, hashset to determine whether the two elements of equality is also a major feature of the standard. HashSet set the criterion for judging the equality of two elements is that two objects are compared by the Equals () method, and the return value of the Hashcode () method of two objects is also equal.

Writing here, we're going to introduce the Equals () and Hashcode () methods.

Equals () and Hashcode () equals ()

The purpose of equals () is to determine whether two objects are equal. **

Equals () is defined in the Object.java of the JDK. Distinguishes between two objects by determining whether their addresses are equal (that is, whether they are the same object). The source code is as follows:

1  Public Boolean equals (Object obj) {2        return ( this = = obj); 3    }
Since the Equals () method is defined in Object.java, this means that all Java classes implement the Equals () method, and all classes can compare the equality of two objects with equals (). However, using the default Equals () method is equivalent to the "= =" method. We can also override this method in a subclass of object, customize the Equals () method, define your own judgment logic in it, and return true if satisfied, or false if it is not satisfied. Here we customize a class person and think that the Equals () method compares the results equal to two person objects of the same age and height.
1  Public classPerson {2      Public intAge ;3      Public intheight;4 @Override5      Public Booleanequals (Object obj) {6         if( This==obj)7             return true;8         if(obj = =NULL)9             return false;Ten         if(GetClass ()! =Obj.getclass ()) One             return false; Aperson other =(person) obj; -         if(Age! =other.age) -             return false; the         if(Height! =other.height) -             return false; -         return true; -     } + }

  1  public  class     equaltest {  2  public  static  void   main (string[] args) {  3  person P1 = new   person ();   4  person P2 =new   person ();   5   System.out.println (P1.equals (p2));   6  }   7   8  }  

Output Result:

True

  

The following is divided into 2 classes, depending on whether the class overrides the Equals () method.
(01) If a class does not overwrite the Equals () method, when it compares two objects by equals (), it is actually comparing two objects that are not the same object. This is equivalent to comparing the two objects by "= =", that is, whether the memory address of the two objects is the same.
(02) We can override the Equals () method of the class to let equals () compare two objects in other ways for equality. The usual practice is that if the contents of two objects are equal, the Equals () method returns true; otherwise, the Fasle is returned.

Hashcode ()

The function of Hashcode () is to obtain a hash code, also known as a hash code; it actually returns an int integer. The purpose of this hash code is to determine the index position of the object in the hash table.

Hashcode () is defined in the Object.java of the JDK, which means that any class in Java contains a hashcode () function. Although, each Java class contains the hashcode () function. However, the hashcode () of the class is only useful when creating a hash table for a "class". More generally, hashcode () is useful when creating a Hashmap,hashtable,hashset collection that contains the class. Because Hashmap,hashtable,hashset is a collection of hash lists.
In the hash table, the hashcode () function is to determine the position of each object of the class in the hash table , and in other cases the hashcode () of the class has no effect. The function of Hashcode () in a hash table is to get the hash code of the object, and to determine the position of the object in the hash table.

Hashcode () can also be divided into two different situations. One is the default method in the object class, and the other is a method that is overridden in a subclass.
(01) If a class does not overwrite the Hashcode () method, when it compares two objects by Hashcode (), it is actually a comparison of two objects that are not the same object. This is equivalent to comparing the two objects by "= =", that is, whether the memory address of the two objects is the same.
(02) We can override the Hashcode () method of a class to allow hashcode () to compare two objects in other ways. As a general rule, the Hashcode () method returns True if the contents of the two objects are equal, otherwise the Fasle is returned.

Through the above two methods of understanding. We can then learn how to determine whether two elements are equal in the HashSet collection.

HashSet the elements of a set are equal

The comparison of two objects is divided into the following four cases:
1. If there are two elements that return false through the equal () method, but their hashcode () methods return unequal, HashSet will store them in a different location.

2. If there are two elements that return true through the equal () method, but their hashcode () methods return unequal, HashSet will store them in a different location.

3. If two objects compare unequal by the Equals () method, the Hashcode () method compares equal, and hashset stores them in the same location, where they are stored in a linked-list structure to hold multiple objects. This is because when an element is deposited into the HashSet collection, HashSet invokes the Hashcode () method of the object to get the Hashcode value of the object, and then, based on that hashcode value, determines where the object is stored in HashSet.

4. If there are two elements that return true by comparison with the equal () method, their hashcode () method returns True,hashset will not be added.

HashSet determines the criteria for equality of two elements: two objects are compared by the Equals () method and the Hashcode () method return values for two objects are equal.

Note: HashSet is quickly positioned based on the hashcode value of the element, and if more than two elements in HashSet have the same hashcode value, the performance will degrade. Therefore, if you override the Equals () method and the Hashcode () method of a class, you should try to ensure that two objects return true by using the Equals () method when the value is equal by the Hashcode () method.

Linkedhashset class

Linkedhashset is a subclass of hashset pairs, and also determines where elements are stored based on their hashcode values, while maintaining the order of elements using linked lists, so that elements are saved in the order in which they are inserted. When iterating through the elements in the Linkedhashset collection, Linkedhashset will access the elements in the collection in the order in which they are added. However, due to maintaining the insertion order of the elements, the performance is slightly lower and hashset, but there is good performance in iterating through all the elements of the set.
Note: Linkedhashset still does not allow elements to repeat, judging the repetition criteria to be consistent with HashSet.

Add: The essence of HashSet is a hashmap. All of the collection elements of the HashSet form the HashMap key, whose value is a static object object. So all the properties of HashSet, the set of HashMap key, are available. You can refer to the relevant content of HashMap in the following article for comparison.

Ii. introduction of TreeSet class TreeSet

TreeSet is an implementation class for the SortedSet interface, as the SortedSet name implies, TreeSet can ensure that the collection element is in the sorted state. In addition, TreeSet provides several additional methods.

Methods of TreeSet
Comparator (): Returns a comparer that sorts the elements in this set and returns NULL if this set uses the natural order of its elements. First (): Returns the current (lowest) element in this set. Last (): Returns the current (highest) element in this set. Lower (e E): Returns the largest element in this set that is strictly less than the given element, or null if no such element exists. Higher (e E): Returns the smallest element in this set that is strictly greater than the given element, or null if no such element exists. Subset (e fromelement, E toelement): Returns a partial view of this set whose elements are from fromelement (including) to Toelement (not included). HeadSet (E toelement): Returns a partial view of this set whose elements are less than toelement. Tailset (E fromelement): Returns a partial view of this set whose elements are greater than or equal to fromelement.

  

How to sort TreeSet

The so-called order in TreeSet, unlike the previous insertion order, is achieved by ordering the attributes of the elements in the collection.
TreeSet supports two sorts of sorting methods: natural sorting and custom sorting. By default, TreeSet is sorted by nature.

1. Natural sorting

Before you tell the natural sort, talk about the comparable interface first.

Java provides a comparable interface that defines a CompareTo (object obj) method that returns an integer value that the class implementing the interface must implement and the object of the class that implements the interface can compare size. When an object calls the method to compare with another object, such as Obj1.compareto (OBJ2), if the method returns 0, two objects are equal, and if the method returns an integer, the obj1 is greater than obj2, or if the method returns a negative integer, Indicates that oj1 is less than obj2.

TreeSet calls the CompareTo (object obj) method of the class that the element belongs to in the collection to compare the size relationship between the elements, and then arranges the collection elements in ascending order, which is the larger backward row compared with the CompareTo (Object obj) method. This is a natural sort of way.

Some of the common classes in Java have implemented the comparable interface and provided a comparison size standard. For example, string is compared by the Unicode value of a string, and the wrapper classes for all numeric types, such as Integer, are compared by their numeric size.
In addition to these already implemented comparable interface classes, if you attempt to add an object to TreeSet, the object's class must implement the comparable interface, or an exception will occur.
Note: You can only add objects of the same type in TreeSet, otherwise you cannot compare and an exception occurs.

TreeSet the elements of a set are equal

For TreeSet collections, the only criterion for judging whether two objects are equal is: two objects are compared by CompareTo (object obj) method to return a 0--if 0 is returned by the CompareTo (object obj) method. TreeSet think they are equal and are not added to the set, otherwise they are considered unequal and added to the collection.
TreeSet is the storage location of the collection elements found based on the red-black tree structure.

2. Custom sorting

The natural ordering of TreeSet is in ascending order based on the size of the CompareTo (Object obj) comparison in the collection element. The custom ordering is helped by the comparator interface. The interface contains an int compare (T o1,t O2) method, which is used to compare the size of the O1,o2: If the method returns a positive integer, the O1 is greater than O2, or if the method returns 0, O1 equals O2, or if the method returns a negative integer, O1 is less than O2.
If you want to implement a custom sort, you need to call a parameter constructor to pass in the comparator object when you create the TreeSet. The comparator object is responsible for the sorting logic of the collection element, and the collection element does not have to implement the comparable interface. Here's a concrete demonstration of this usage:

1  Public Static voidMain (string[] args) {2person P1 =NewPerson ();3P1.age =20;4person P2 =NewPerson ();5P2.age = 30;6Comparator<person> Comparator =NewComparator<person>() {7 8 @Override9              Public intCompare (person O1, person O2) {Ten                 //the Younger, the younger, the more behind. One                 if(o1.age<o2.age) { A                     return1; -}Else if(o1.age>o2.age) { -                     return-1; the}Else{ -                     return0; -                 } -                  +             } -         }; +Treeset<person> set =NewTreeset<person>(comparator); A Set.add (p1); at Set.add (p2); - System.out.println (set); -     }
[person[age=30], person[age=20]]

  

Summary: Whether you use natural sorting or custom sorting, you can implement a variety of sorting methods through custom comparison logic.

Note: If you add a Mutable object to TreeSet, and the subsequent program modifies the variable object's instance variable, this will cause it to change in size order with other objects, but TreeSet will not adjust them again. The following procedure demonstrates this behavior:

1Treeset<person> set =NewTreeset<person>();2person P1 =NewPerson ();3P1.setage (10);4person P2 =NewPerson ();5P2.setage (30);6Person P3 =NewPerson ();7P3.setage (40);8 Set.add (p1);9 Set.add (p2);Ten Set.add (p3); OneSystem.out.println ("Initial Age order"); A System.out.println (set); -         //P1 age changed to 50 Max -P1.age = 60; theSystem.out.println ("Modify P1 age after collection sorting"); - System.out.println (set); -P2.age = 40; -SYSTEM.OUT.PRINTLN ("Modify P2 Age after collection sorting"); + System.out.println (set); -Person P4 =NewPerson ();

Where person implements the comparable interface, the person object is arranged in ascending order of age from small to large.
Output Result:

Initial age sequencing [person[age=10], person[age=30], person[age=40]] Modify P1 Age Collection sort [person[age=60], person[age=30], person[age=40] Modify P2 Age Collection sort [person[age=60], person[age=40], person[age=40]

  

It can be seen that there is no change, and that if you modify the element deletion operation may be unsuccessful, it is more complex. In Summary, it is recommended that you do not modify the key instance variables of the elements placed in the TreeSet collection.
Add: TreeSet is also non-thread safe.

Three, Enumset class Enumset Introduction

Enumset is a collection class designed for enumeration classes, and all elements in Enumset must be an enumeration value of the specified enumeration type, which is either displayed or implicitly specified when the Enumset is created. The collection elements of the Enumset are also ordered, and Enumset determines the order of the collection elements in the order in which the enumeration values are defined within the Enumset class.

Enumset Features

The 1.EnumSet collection does not allow the addition of NULL elements. All elements in the Enumset must be enumerated values of the specified enumeration type.
The 2.EnumSet class does not expose any constructors to create an instance of the class, and the program should create the Enumset object from the class method it provides.

Enumset There are no additional methods to add, just add some methods to create Enumset objects.

Enumset How to create objects


add: Enumset is also non-thread safe.

Iv. performance comparisons of HashSet, TreeSet and Enumset

Enumset Performance >hashset Performance >linkedhashset>treeset performance

The Enumset interior is stored as a bit vector, is compact, efficient, and only stores enumeration values of enumerated classes, so it is the most efficient. HashSet is a hash algorithm for location storage, which is especially suitable for adding and querying operations. Linkedhashset because to maintain the list, performance than HashSet almost, but with a linked list, linkedhashset more suitable for inserting, deleting and traversing operations. The TreeSet requires an additional red-black tree algorithm to maintain the order of the collection, the performance of the most time.

However, specific usage scenarios should be considered.
Use the TreeSet collection when a specific sorted collection is required.
Use the Enumset collection when you need to save an enumeration value for an enumeration class.
Use HashSet when adding and querying operations are frequently used.
Use Linkedhashset when you frequently insert a sort or use delete, insert, and traverse operations.

Subsequent articles will take a closer look at the specific implementation classes in the Java collection. If you are interested, you can watch the following content to learn more about the Java collection.

Understanding Java Collection (i)--set frame collction, Map
Understanding Java Collection (iii)--collection List
Understanding Java Collection (iv)--collection Queue
Understanding Java Collection (v)--set Map
Understanding Java Collection (vi)--collection and deletion of the details of the change, performance and selection recommendations (to be updated)

Reprint Link: https://www.jianshu.com/p/9081017a2d67

"Understanding Java Collection (ii)"--Set set

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.