Java--set Collection

Source: Internet
Author: User
Tags set set

Set set, which resembles a jar in which a program can "throw" multiple objects into a set set in turn, while set collections do not usually remember the order in which elements are added, meaning that set sets are unordered. The set set is basically the same as colleaction, no additional method is provided, in fact set is Collection, but the behavior is slightly different (Set does not allow repeating elements).

"HashSet

  The HashSet class is a typical implementation class for a set interface, which is used most of the time when a set collection is used. HashSet stores the elements in the collection by the Hash algorithm, so it has good access and lookup performance.

HashSet has the following characteristics:

"Cannot guarantee the order of the elements, the order may be different from the order in which they are added, and the order may vary."

"HASHSETF is not synchronous, and if multiple threads are accessing a HashSet at the same time, assuming that there are two or more threads that modify the HashSet collection at the same time, the code must be synchronized.

The value of the collection element can be null.

 Public Static voidMain (string[] args) {Dog Ououdog=NewDog ("Ouau","Snow Schnauzer"); Dog Yayadog=NewDog ("Asia and Asia","Labrador"); Dog Meimeidog=NewDog ("Meimei","Snow Schnauzer"); Dog Feifeidog=NewDog ("Feifei","Labrador"); Set Dogs=NewHashSet (); //adding elementsDogs.add (Ououdog);        Dogs.add (Yayadog);        Dogs.add (Meimeidog);                   Dogs.add (Feifeidog); //iterating through the elements in the collection         for(Object item:dogs) {Dog dog=(DOG) item; System. out. println (Dog.getname ()); } System. out. println (Dogs.size ());//View LengthSystem. out. println (Dogs.isempty ());//returns true for NULL, otherwise falseSystem. out. println (Dogs.contains (Feifeidog));//Returns true if the specified element exists, otherwise falseDogs.remove (Feifeidog); //Delete the specified elementdogs.clear (); //clears all elements in the collection            }

"Linkedhashset

 HashSet also has a subclass linkedhashset, and the Linkedhashset collection determines where the element is stored based on the hashcode value of the element, but it uses the list to maintain the order of the elements, so that the elements appear to be saved in the order in which they were inserted. That is, when traversing linkedhashset, the elements in the collection are accessed in the order in which they are added.

Linkedhashset needs to maintain the insertion order of the elements, so performance is slightly lower than HashSet performance, but it will perform well when iterating through all the elements in the Set because it maintains the internal order in the list. Although Linkedhashset uses the order of the list elements to be added, Linkedhashset remains HashSet, so it still does not allow the collection elements to be duplicated.

 Public Static voidMain (string[] args) {Dog Ououdog=NewDog ("Ouau","Snow Schnauzer"); Dog Yayadog=NewDog ("Asia and Asia","Labrador"); Dog Meimeidog=NewDog ("Meimei","Snow Schnauzer"); Dog Feifeidog=NewDog ("Feifei","Labrador"); Set Dogs=NewLinkedhashset ();          Dogs.add (Ououdog);        Dogs.add (Yayadog);        Dogs.add (Meimeidog);                           Dogs.add (Feifeidog);  for(Object item:dogs) {Dog dog=(DOG) item; System. out. println (Dog.getname ()); }        }

"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. Compared to HashSet, TreeSet also offers several additional methods.

 Public Static voidMain (string[] args) {TreeSet nums=NewTreeSet (); Nums.add (5); Nums.add (2); Nums.add (Ten); Nums.add (-9); Nums.add ( the); //Auto Sort from small to largeSystem. out. println (Nums);//output [-9, 2, 5, ten, up]//an element of the output collection elementSystem. out. println (Nums.first ());//Output-9//the last element of the output collection elementSystem. out. println (Nums.last ());//Output//returns a subset less than 4, not containing 4System. out. println (Nums.headset (4));//output [-9, 2]//returns a subset greater than 5, with a subset of 5System. out. println (Nums.tailset (5));//output [5, ten, +]//returns a subset greater than or equal to-3, less than 4System. out. println (Nums.subset (-3,4));//output [2]//returns the largest element less than the specified element, and the specified element does not need to be an element in the TreeSet collectionSystem. out. println (Nums.lower ( One)); //returns the smallest element greater than the specified elementSystem. out. println (Nums.higher (3)); //If TreeSet takes a custom sort, the method returns the Comparator used by the custom sort, or null if the natural sort is usedSystem. out. println (Nums.comparator ()); }

The performance analysis of each Set implementation class

  HashSet and TreeSet are the two typical implementations of Set, how do you choose HashSet and TreeSet? HashSet's performance is always better than TreeSet (Toby is a common addition, query element, etc.), because TreeSet needs an additional red-black tree algorithm to maintain the order of the collection elements, only if you need to maintain a sort of set, you should use the TreeSet, otherwise you should use the has Hset.

HashSet also has a sub-class: Linkedhashset, for normal insert, delete operation, Linkedhashset is slightly slower than HashSet, which is due to the additional overhead of maintaining the linked list, but because of the linked list, traverse Linkedhashset will be more.

Java--set Collection

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.