Set set--hashset, TreeSet, Linkedhashset (July 06, 2015)

Source: Internet
Author: User
Tags set set

The set set is different from the list: set does not allow duplicate set is unordered set set does not have subscript index, so the traversal of set to pass iterator iterator two, HashSet1, hashset by a hash table to support, inside is actually a hashmap, The HashMap key is the value of HashSet, and the value of HashMap is a fixed constant, which is why the value is not allowed in HashSet because the HashMap key does not allow repetition. HashSet allows null values and only allows a null value to exist, it is also non-thread-safe, but it provides a way to construct a thread-safe hashSet Set hashSet = Collections.synchronizedset (new HashSet (...)); 2, the value of hashset is not allowed to repeat, then how does it guarantee that the element is not duplicated? You must implement and define your own equals () method for elements that are added to HashSet, but for a good design style, it is best to overwrite the Equals () method, and also overwrite the Hashcode () method, when you insert a new object into HashSet. First, the object's Hashcode () is compared with the hashcode () of the object that already exists, and if it is equal, it cannot be inserted, and if not, the Equals () method is called, and if the equals result is true, the description already exists and cannot be inserted if False , which can be inserted. Eguser user = new User (), User.setuserid (1), User.setusername ("Dreyer"), User.setpassword ("123456"); User.setbirthday ( New Date ()); User User2 = New user (), User2.setuserid (1), User2.setusername ("Dreyer"), User2.setpassword ("123456"); User2.setbirthday (New Date ()); HashSet HashSet = new HashSet (); Hashset.add (user); Hashset.add (User2); System.out.println ("Collection Size:" +hashset.size ()); For the above code, if the user class does not implement the Equals (), Hashcode () method, Output: Collection Size: 2 we add equals (), Ha for the user classShcode () method @overridepublic Boolean equals (Object o) {if (this = = O) return true;if (o = = NULL | | getclass ()! = O.getclass () ) return false; User user = (user) o;if (userId! = User.userid) return false;if (userName! = null?!username.equals (user.username): User . UserName = null) return False;return true;} @Overridepublic int hashcode () {int result = Userid;result = $ * result + (UserName! = null? Username.hashcode (): 0); re turn result;} The above code will output: Collection size: 1 (actually judging by hashcode) Three, TreeSet1, TreeSet is based on red-black tree, the interior is actually a treemap, similar to HashSet, TreeSet also does not allow duplicate elements, which are also non-thread-safe, as well as providing a TreeSet method for constructing thread safety Set TreeSet = Collections.synchronizedset (new TreeSet (...)); The difference is that TreeSet does not allow null values, and if you try to add a null value, it throws Nullpointexceptin. 2, the bottom of the TreeSet implementation is the red-Haishi data structure, the use of this structure can get ordered sequence from set, but the precondition is: the element must implement the comparable interface, the interface is only one method, is the CompareTo () method. When a new element is inserted into a set, the element that already exists in the set is traversed first, and the CompareTo () method is called, and the insertion position is determined based on the returned result. In turn, the order of the elements is ensured. If you put an object that does not implement comparable into TreeSet, it throws classcastexception.
LinkedHashSet1, Linkedhashset is implemented based on hash tables and link lists, which differs from HashSet in that the latter maintains a double-link list that runs on all items. This list of links defines the order of iterations, that is, the order in which the elements are inserted into the set (insert order). Note that the insertion order is not affected by the elements that are reinserted in the set. (If S.add (e) is called immediately after S.contains (e) Returns True, the element e is reinserted into set S. 2. Linkedhashset inherits from HashSet, so it also allows null values, does not allow duplicates, Linkedhashset maintains the linked table of the collection in the order in which the elements are inserted, allowing iterations in the collection in the order in which they are inserted (the order in which they are inserted, What is the order of iteration? V. Usage Summary hashset is based on hash table, its performance is generally better than TreeSet, So when it comes to efficiency, using hashsettreeset is generally used in situations where ordering is required, Linkedhashset can be used in cases where we need to maintain the order of the inserted elements.

Set set--hashset, TreeSet, Linkedhashset (July 06, 2015)

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.