Guava Study Notes: Guava adds the set type-Multiset

Source: Internet
Author: User

Guava introduces some new collection types that are not available in JDK, but are very useful. All these new collection types can be smoothly integrated with the collection in JDK. The Guava set accurately implements the JDK-defined interfaces. The new set defined in Guava includes:
Multiset
SortedMultiset
Multimap
ListMultimap
SetMultimap
BiMap
ClassToInstanceMap
Table

  Multiset set

What is Multiset? As the name suggests, the difference between Multiset and Set is that multiple identical objects can be saved. In JDK, there is a basic difference between List and Set, that is, List can contain multiple identical objects in sequence, and Set cannot have duplicates, the order is not guaranteed (some implementations are ordered, such as LinkedHashSet and SortedSet). Therefore, Multiset occupies a gray area between List and Set: duplicates are allowed, but the order is not guaranteed.
Common Use Cases: a useful feature of Multiset is to track the number of each object, so you can use it for numerical statistics. Common Implementation methods are as follows:

 ="wer|dffd|ddsa|dfd|dreg|de|dr|ce|ghrt|cf|gt|ser|tg|ghrt|cf|gt|" +                "ser|tg|gt|kldf|dfg|vcd|fg|gt|ls|lser|dfr|wer|dffd|ddsa|dfd|dreg|de|dr|" +                "ce|ghrt|cf|gt|ser|tg|gt|kldf|dfg|vcd|fg|gt|ls|lser|dfr"=strWorld.split("\\|"<String, Integer> countMap =  HashMap<String, Integer>= (count == 1+ 1"countMap:"+" count:"+

The code above provides a simple function to record the number of times a string appears in an array. This scenario is often used in the actual development process. If you use a specific class that implements the Multiset interface, you can easily implement the above functional requirements:

     ="wer|dfd|dd|dfd|dda|de|dr"=strWorld.split("\\|"<String> wordList= ArrayList<String><String> wordsMultiset =             +" count:"+

  Main Methods of Multiset

  The interfaces defined by the Multiset interface mainly include:
Add (E element): add a single element to it
Add (E element, int occurrences): add a specified number of elements to it.
Count (Object element): returns the number of given parameter elements.
Remove (E element): remove an element, and its count value will respond to decrease
Remove (E element, int occurrences): removes the corresponding number of elements.
ElementSet (): puts different elements into a Set.
EntrySet (): similar to Map. entrySet, Set <Multiset. Entry> is returned. The included entries support getElement () and getCount ()
SetCount (E element, int count): sets the number of repetitions of an element.
SetCount (E element, int oldCount, int newCount): Change the elements that match the original number of duplicates to the new number of duplicates.
RetainAll (Collection c): retains all elements that appear in a given set Parameter
RemoveAll (Collectionc): removes all elements that appear to a given set Parameter

Examples of common methods:

 ="wer|dfd|dd|dfd|dda|de|dr"=strWorld.split("\\|"<String> wordList= ArrayList<String><String> wordsMultiset =                +" count:"+(!wordsMultiset.contains("peida""peida", 2"============================================"+" count:"+(wordsMultiset.contains("peida""peida", 23"============================================"+" count:"+(wordsMultiset.contains("peida""peida", 23,45"============================================"+" count:"+(wordsMultiset.contains("peida""peida", 44,67"============================================"+" count:"+

Output:

de count:111211============================================1112211============================================11122311============================================11124511============================================11124511

Note: setCount (E element, int oldCount, int newCount): method. If the oldCount passed in is inconsistent with the element, the count of the element cannot be set to newCount. Note.

  Multiset is not a Map

It should be noted that Multiset is not a Map <E, Integer>, although Multiset provides some similar functions. Other notable differences are:
The number of repeated elements in Multiset is only positive, and the maximum value is not greater than Integer. MAX_VALUE. The element with the configured count of 0 does not appear in the multiset or the returned results of elementSet () and entrySet.
The multiset. size () method returns the sum of all elements, equivalent to adding the number of all duplicates. If you need to know the number of each element, you can use elementSet (). size () to get it. (therefore, calling the add (E) method will increase the value of multiset. size () by 1 ).
Multiset. iterator () cyclically iterates each element that appears, and the number of iterations is the same as that of multiset. size. Iterates over each occurrence of each element, so the length of the iteration is equal to multiset. size ().
Multiset supports adding, removing, and resetting the number of elements. Executing setCount (element, 0) is equivalent to removing all the same elements in the multiset.
When you call the multiset. count (elem) method, if the element is not in the Set, the returned result is only 0.

  Implementation of Multiset 

Guava provides multiple implementations of Multiset, which basically correspond to the implementation of Map in JDK:
  Map Corresponding Multiset Supports null elements
HashMap HashMultiset Yes
TreeMap TreeMultiset Yes (if the comparator does)
LinkedHashMap LinkedHashMultiset Yes
ConcurrentHashMap ConcurrentHashMultiset No
ImmutableMap ImmutableMultiset No

 

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.