Guava learning-ETS

Source: Internet
Author: User
Tags google guava

Today, we will talk about the multi-dataset data structure in the guava class library. Although it is not often used, it is necessary to discuss it. We know that the set in the Java class library cannot store the same elements, and the elements in the class are unordered. The list can store the same elements in sequence. The current multi-dataset can store the same elements, but the order between elements is unordered. We can also see from this that multisets is definitely not implementing the set interface in Java, because the set interface cannot store the same element! The elements in the Set in Java are a bit like: [A, C, B], and Multiset will be like this: [a x 2, C x 3, B x 5], this is different.
In the past, if we needed to count the number of times each word appears in an article, we may use the following method:

public void wordCounts(List words) {Map<String, Integer> counts = new HashMap<String, Integer>();for (String word : words) {Integer count = counts.get(word);if (count == null) {counts.put(word, 1);} else {counts.put(word, count + 1);}}}

If we need to get the number of occurrences of a word (such as good), we may write as follows:

int goodCount = counts.get("good");

Very troublesome, right? In addition, the get (e key) meaning in map is not so obvious. If we use multisets to see:

import com.google.common.collect.HashMultiset;import com.google.common.collect.Multiset;Multiset countMultiset = HashMultiset.create();countMultiset.addAll(words);

Is it easy? Even loops are not required. How can we get the number of times a word (such as good) appears? Simple:

int goodCount= countMultiset .count(“good”);

In Multiset, a count (object element) method is provided to obtain the number of times an object appears in Multiset. This is obviously much more readable than calling get in map, isn't it?

Note: Multiset provides the setcount (E, INT) method to modify the number of times Element E appears in Multiset. However, you cannot change the number of times an element appears to a negative number or a value greater than integer. max_value. Otherwise, an exception is thrown.

Countmultiset. setcount ("good", integer. max_value + 1); or countmultiset. setcount ("good",-1 );

Will throw

Exception in thread "main" java.lang.IllegalArgumentException: count cannot be negative: -2147483648(-1)at com.google.common.base.Preconditions.checkArgument(Preconditions.java:119)at com.google.common.collect.Multisets.checkNonnegative(Multisets.java:1061)at com.google.common.collect.AbstractMapBasedMultiset.setCount(AbstractMapBasedMultiset.java:265)at com.google.common.collect.HashMultiset.setCount(HashMultiset.java:34)at com.wyp.test.testFiles(test.java:150)at com.wyp.test.main(test.java:156)

In Multiset, setcount first determines the status of the set data.

 static void checkNonnegative(int count, String name) {checkArgument(count >= 0, "%s cannot be negative: %s", name, count); }

It can be seen that count is of the int type, and the maximum value is integer. max_value. Adding 1 on the Base will become a negative number, so no.
Multiset is not a map <E, integer> type structure. We can use Map <E, integer> to implement Multiset, but use Map <e, integer> the implementation of Multiset differs greatly from that of Google guava, mainly as follows:

  1. The number of occurrences of elements in Multiset can only be positive. If the number of E occurrences is 0, e will not appear in the Multiset, but cannot be in the views of elementset () and entryset;
  2. Multiset. Size () returns the size of the Set, which is equivalent to the total number of elements in Multiset. You can use elementset (). Size () to obtain the total number of different elements in Multiset;
  3. Multiset. iterator () can traverse all elements in Multiset, so the number of iteration traversal times is equal to Multiset. Size ();
  4. Multiset supports adding and deleting elements and sets the number of occurrences of the elements. setcount (ELEM, 0) is equivalent to removing all the elements of ELEM;
  5. If elem in the Multiset. Count (ELEM) method is not displayed in Multiset, the returned value is always 0.

Frequently Used classes that implement the Multiset interface include:

  1. Hashmultiset: elements are stored in hashmap.
  2. Linkedhashmultiset: elements are stored in linkedhashmap, that is, the order of elements is determined by the order in which they are placed for the first time.
  3. TreeMultiset: elements are sorted and stored in TreeMap.
  4. EnumMultiset: the element must be of the enum type.
  5. ImmutableMultiset: Mutiset that cannot be modified
Reprinted Please note: Reprinted from past memories (http://www.wypblog.com /)
Multi-datasets (http://www.wypblog.com/archives/506) for guava Learning)

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.