In daily development work, we sometimes need to construct something like Map
[code]import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.junit.Test; public class MultimapTest { Map<String, List<StudentScore>> StudentScoreMap = new HashMap<String, List<StudentScore>>(); @Test public void testStudentScore(){ for(int i=10;i<20;i++){ StudentScore studentScore=new StudentScore(); studentScore.CourseId=1001+i; studentScore.score=100-i; addStudentScore("peida",studentScore); } System.out.println("StudentScoreMap:"+StudentScoreMap.size()); System.out.println("StudentScoreMap:"+StudentScoreMap.containsKey("peida")); System.out.println("StudentScoreMap:"+StudentScoreMap.containsKey("jerry")); System.out.println("StudentScoreMap:"+StudentScoreMap.size()); System.out.println("StudentScoreMap:"+StudentScoreMap.get("peida").size()); List<StudentScore> StudentScoreList=StudentScoreMap.get("peida"); if(StudentScoreList!=null&&StudentScoreList.size()>0){ for(StudentScore stuScore:StudentScoreList){ System.out.println("stuScore one:"+stuScore.CourseId+" score:"+stuScore.score); } } } public void addStudentScore(final String stuName,final StudentScore studentScore) { List<StudentScore> stuScore = StudentScoreMap.get(stuName); if (stuScore == null) { stuScore = new ArrayList<StudentScore>(); StudentScoreMap.put(stuName, stuScore); } stuScore.add(studentScore); } } class StudentScore{ int CourseId; int score; }
Description: If you want to Map
[code] @Test public void teststuScoreMultimap(){ Multimap<String,StudentScore> scoreMultimap = ArrayListMultimap.create(); for(int i=10;i<20;i++){ StudentScore studentScore=new StudentScore(); studentScore.CourseId=1001+i; studentScore.score=100-i; scoreMultimap.put("peida",studentScore); } System.out.println("scoreMultimap:"+scoreMultimap.size()); System.out.println("scoreMultimap:"+scoreMultimap.keys()); }
, calling Multimap.get(key) will return this key The view of the collection of corresponding values. If there is no corresponding collection, an empty collection is returned. For ListMultimap, this method returns a List, and for SetMultimap, this method returns a Set. Modifying data is achieved by modifying the underlying Multimap. For example:
[code] @Test public void teststuScoreMultimap(){ Multimap<String,StudentScore> scoreMultimap = ArrayListMultimap.create(); for(int i=10;i<20;i++){ StudentScore studentScore=new StudentScore(); studentScore.CourseId=1001+i; studentScore.score=100-i; scoreMultimap.put("peida",studentScore); } System.out.println("scoreMultimap:"+scoreMultimap.size()); System.out.println("scoreMultimap:"+scoreMultimap.keys()); Collection<StudentScore> studentScore = scoreMultimap.get("peida"); studentScore.clear(); StudentScore studentScoreNew=new StudentScore(); studentScoreNew.CourseId=1034; studentScoreNew.score=67; studentScore.add(studentScoreNew); System.out.println("scoreMultimap:"+scoreMultimap.size()); System.out.println("scoreMultimap:"+scoreMultimap.keys()); }
Multimap also supports a series of powerful view functions:
1.asMap implements its own Multimap
[code]@Test public void teststuScoreMultimap(){ Multimap<String,StudentScore> scoreMultimap = ArrayListMultimap.create(); for(int i=10;i<20;i++){ StudentScore studentScore=new StudentScore(); studentScore.CourseId=1001+i; studentScore.score=100-i; scoreMultimap.put("peida",studentScore); } System.out.println("scoreMultimap:"+scoreMultimap.size()); System.out.println("scoreMultimap:"+scoreMultimap.keys()); Collection<StudentScore> studentScore = scoreMultimap.get("peida"); StudentScore studentScore1=new StudentScore(); studentScore1.CourseId=1034; studentScore1.score=67; studentScore.add(studentScore1); StudentScore studentScore2=new StudentScore(); studentScore2.CourseId=1045; studentScore2.score=56; scoreMultimap.put("jerry",studentScore2); System.out.println("scoreMultimap:"+scoreMultimap.size()); System.out.println("scoreMultimap:"+scoreMultimap.keys()); for(StudentScore stuScore : scoreMultimap.values()) { System.out.println("stuScore one:"+stuScore.CourseId+" score:"+stuScore.score); } scoreMultimap.remove("jerry",studentScore2); System.out.println("scoreMultimap:"+scoreMultimap.size()); System.out.println("scoreMultimap:"+scoreMultimap.get("jerry")); scoreMultimap.put("harry",studentScore2); scoreMultimap.removeAll("harry"); System.out.println("scoreMultimap:"+scoreMultimap.size()); System.out.println("scoreMultimap:"+scoreMultimap.get("harry")); }
Multimap
Multimap provides Rich implementation, so you can use it to replace Map
The above is the content of Java-Class Library-Guava-Multimap. For more related content, please pay attention to the PHP Chinese website (www.php.cn) !

Analysis of class loading behavior of SPI mechanism when Tomcat loads Spring-Web modules. Tomcat is used to discover and use the Servle provided by Spring-Web when loading Spring-Web modules...

Confused with choosing Java project management tools for beginners. For those who are just beginning to learn backend development, choosing the right project management tools is crucial...

Regarding the implementation principles and methods of listening events In programming, listening to events is a common requirement, especially listening for changes in a certain value. Many people may...

How to dynamically configure the parameters of entity class annotations in Java During the development process, we often encounter the need to dynamically configure the annotation parameters according to different environments...

Analysis of the reason why Python script cannot be found when submitting a PyFlink job on YARN When you try to submit a PyFlink job through YARN, you may encounter...

The difficulties encountered when calling third-party interfaces to transmit data in SpringBoot project will be used for a Spring...

In IntelliJ...

How to convert names to numbers to implement sorting within groups? When sorting users in groups, it is often necessary to convert the user's name into numbers so that it can be different...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.