首頁 >Java >java教程 >Java-類別庫-Guava-Multimap

Java-類別庫-Guava-Multimap

黄舟
黄舟原創
2017-01-19 13:20:421756瀏覽

在日常的開發工作中,我們有的時候需要構造像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)會返回這個鍵對應的值的集合的視圖(view),沒有對應集合就傳回空集合。對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 Mult

以上就是Java-類別庫-Guava-Multimap的內容,更多相關內容請關注PHP中文網(www.php.cn)!


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn