Guava Study Notes: Guava adds the set type-Multimap

Source: Internet
Author: User

In daily development, we sometimes need to construct a Map <K, List <V> or Map <K, set <V> This is a complex data structure of the collection type for business logic processing. For example:

 <String, List<StudentScore>> StudentScoreMap =  HashMap<String, List<StudentScore>> ( i=10;i<20;i++==1001+=100-"peida""StudentScoreMap:"+"StudentScoreMap:"+StudentScoreMap.containsKey("peida""StudentScoreMap:"+StudentScoreMap.containsKey("jerry""StudentScoreMap:"+"StudentScoreMap:"+StudentScoreMap.get("peida"<StudentScore> StudentScoreList=StudentScoreMap.get("peida"(StudentScoreList!=&&StudentScoreList.size()>0"stuScore one:"+stuScore.CourseId+" score:"+  addStudentScore( String stuName,<StudentScore> stuScore = (stuScore == =  ArrayList<StudentScore>

Note: It is too difficult to implement <String, List <StudentScore> StudentScoreMap = HashMap <String, List <StudentScore> by yourself. You need to check whether the key exists, if the List does not exist, create one. If the List exists, add the previous one. This process is quite painful. If you want to check whether the objects in the List exist, delete an object, or traverse the entire data structure, you need more code to implement it.

  Multimap

The Multimap of Guava provides a data structure that conveniently maps a key to multiple values. This allows us to easily and elegantly implement the complex data structure above, so that we can focus on implementing business logic rather than on the data structure, next, let's take a look at the knowledge points of Multimap.

The above code and data structure are implemented using Multimap. The code structure is much clearer and simpler. The specific code is as follows:

 <String,StudentScore> scoreMultimap =( i=10;i<20;i++==1001+=100-"peida""scoreMultimap:"+"scoreMultimap:"+

When you call Multimap. get (key), the view of the set of values corresponding to the key is returned. An empty set is returned if no corresponding set exists. For ListMultimap, this method returns a List. For SetMultimap, this method returns a Set. Modifying data is implemented by modifying the underlying Multimap. For example:

 <String,StudentScore> scoreMultimap =( i=10;i<20;i++==1001+=100-"peida""scoreMultimap:"+"scoreMultimap:"+<StudentScore> studentScore = scoreMultimap.get("peida"==1034=67"scoreMultimap:"+"scoreMultimap:"+

Multimap also supports a series of powerful view functions:
1. asMap maps its Multimap <K, V> into Map <K, Collection <V> View. This Map view supports remove and modify operations, but does not support put and putAll. Strictly speaking, you can call asMap () when you want the input parameter to be a non-existent key and return null instead of an empty modifiable set (). get (key ). (You can force the asMap () transformation (). get (key) result type-convert SetMultimap results to Set, convert ListMultimap results to List-but directly convert ListMultimap to Map <K, list <V> is not acceptable .)
2. The entries view displays all key-value pairs in Multimap in the form of Collection <Map. Entry <K, V>.
3. The keySet view uses the key set of Multimap as the view.
4. The keys view returns a Multiset. This Multiset is based on the number of unique keys. This Multiset can modify Multimap by supporting the remove operation instead of the add operation.
5. The values () view splits all values in Multimap into a Collection <V>. This operation is similar to Iterables. concat (multimap. asMap (). values (), but it returns a complete Collection.

Although Map is used for Multimap implementation, Multimap <K, V> is not a Map <K, Collection <V>. Because there are obvious differences between the two:
1. Multimap. get (key) must return a non-null set. However, this does not mean that Multimap uses memory to associate these keys. On the contrary, the returned set is only a view that allows adding elements.
2. if you want to return null if there is no key like Map, instead of returning an empty set like Multimap, you can use the view returned by asMap () to obtain Map <K, collection <V>. (In this case, you have to convert the returned Collection <V> to List or Set ).
3. Multimap. containsKey (key) returns true only when the key exists.
4. Multimap. entries () returns all the key-value pairs of Multimap. However, if you need key-value pairs of key-collection, you must use asMap (). entries ().
5. Multimap. size () returns the number of entries instead of the number of duplicate keys. To obtain the number of unique keys, you must use Multimap. keySet (). size ().

 <String,StudentScore> scoreMultimap =( i=10;i<20;i++==1001+=100-"peida""scoreMultimap:"+"scoreMultimap:"+<StudentScore> studentScore = scoreMultimap.get("peida"==1034=67==1045=56"jerry""scoreMultimap:"+"scoreMultimap:"+"stuScore one:"+stuScore.CourseId+" score:"+"jerry""scoreMultimap:"+"scoreMultimap:"+scoreMultimap.get("jerry""harry""harry""scoreMultimap:"+"scoreMultimap:"+scoreMultimap.get("harry"

  Multimap implementation

  Multimap provides rich implementations, so you can use it to replace the Map <K, Collection <V> In the program. The specific implementation is as follows:
Implementation Keys behavior is similar to Values behavior.
ArrayListMultimap HashMap ArrayList
HashMultimap HashMap HashSet
LinkedListMultimap LinkedHashMap * LinkedList *
LinkedHashMultimap LinkedHashMap LinkedHashSet
TreeMultimap TreeMap TreeSet
ImmutableListMultimap ImmutableMap ImmutableList
ImmutableSetMultimap ImmutableMap ImmutableSet

  
All of the above implementations, except immutable, support null keys and values.
1. LinkedListMultimap. entries () can maintain the order of iteration.

2. LinkedHashMultimap maintains the insertion sequence and key insertion sequence.
Note that not all implementations actually implement Map <K, Collection <V>! (In particular, some Multimap implementations use a custom hash table for minimum overhead)

Related Article

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.