일상적인 개발 작업에서는 때때로 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; }
과 같은 것을 생성해야 합니다. 참고: 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()); }
을 원할 경우 Multimap.get(key)을 호출하면 됩니다. 이 키를 반환합니다. 해당 값의 컬렉션 보기 해당 컬렉션이 없으면 빈 컬렉션이 반환됩니다. ListMultimap의 경우 이 메서드는 List를 반환하고 SetMultimap의 경우 이 메서드는 Set을 반환합니다. 데이터 수정은 기본 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.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은 일련의 강력한 보기 기능도 지원합니다.
1.asMap은 자체 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은 풍부한 기능을 제공합니다.
위 내용은 Java-Class Library-Guava-Multimap의 내용이며, 더 많은 관련 내용은 PHP 중국어 홈페이지(www. php.cn) !