Heim  >  Artikel  >  Java  >  Java-Klassenbibliothek-Guava-Multimap

Java-Klassenbibliothek-Guava-Multimap

黄舟
黄舟Original
2017-01-19 13:20:421722Durchsuche

Bei der täglichen Entwicklungsarbeit müssen wir manchmal so etwas wie 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;
}


erstellen. Hinweis: Wenn Sie 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());
    }
Die Ansicht des Wertesatzes, der diesem Schlüssel entspricht, wird zurückgegeben. Wenn kein entsprechender Satz vorhanden ist, wird ein leerer Satz zurückgegeben. Für ListMultimap gibt diese Methode eine Liste zurück, und für SetMultimap gibt diese Methode einen Satz zurück. Das Ändern von Daten erfolgt durch Ändern der zugrunde liegenden Multimap. Zum Beispiel:

[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 unterstützt auch eine Reihe leistungsstarker Ansichtsfunktionen:


 1.asMap implementiert seine eigene 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 bietet eine umfangreiche Implementierung, sodass Sie Map im Programm ersetzen können

Das Obige ist der Inhalt der Java-Class Library-Guava-Multimap. Für weitere verwandte Inhalte achten Sie bitte auf PHP Chinesische Website (www.php.cn)!


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn