Java-类库-Guava-Multimap

在日常的开发工作中,我们有的时候需要构造像Map

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

 @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来实现的。例如:

 @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

@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提供了丰富的实现,所以你可以用它来替代程序里的Map

时间: 2024-09-13 08:53:10

Java-类库-Guava-Multimap的相关文章

2016 年排名 Top 100 的 Java 类库

我们分析了GitHub中47,251个依赖,从中找出了排名前一百的Java类库,让我们看看谁在前面,谁在后面. 我们在漫长的周末的消遣方式就是浏览GitHub并且搜索流行的Java类库.我们决定把其中的乐趣与结果分享给你. 我们分析了GitHub中排名前3,862个项目中的47,251个导入语句,其中有12,059个Java类库被依赖.我们从这个列表中提取出前一百并把结果分享给你. 最受欢迎的前20个Java类库 和上次分析结果一致,junit依旧是GitHub中最受欢迎的类库.Java中的日志

PHP如何调用JAVA 类库

JAVA是个非常强大的编程利器,它的扩展库也是非常的有用,这篇教程,主要讲述怎样使用PHP调用功能强大的JAVA 类库(classes).为了方便你的学习,这篇教程将包括JAVA的安装及一些基本的例子. Windows下的安装 第一步:安装JDK,这是非常容易的,你只需一路回车的安装好.然后做好以下步骤. 在 Win9x 下加入 :"PATH=%PATH%;C:\jdk1.2.2\bin" 到AUTOEXEC.BAT文件中 在 NT 下加入 ";C:\jdk1.2.2\bin

怎样使用PHP调用功能强大的JAVA 类库

JAVA是个非常强大的编程利器,它的扩展库也是非常的有用,这篇教程,主要讲述怎样使用PHP调用功能强大的JAVA 类库(classes).为了方便你的学习,这篇教程将包括JAVA的安装及一些基本的例子. windows下的安装 第一步:安装JDK,这是非常容易的,你只需一路回车的安装好.然后做好以下步骤. 在 Win9x 下加入 :"PATH=%PATH%;C:\jdk1.2.2\bin" 到AUTOEXEC.BAT文件中 在 NT 下加入 ";C:\jdk1.2.2\bin

怎样使用PHP调用功能强大的JAVA类库

JAVA是个非常强大的编程利器,它的扩展库也是非常的有用,这篇教程,主要讲述怎样使用PHP调用功能强大的JAVA 类库(classes).为了方便你的学习,这篇教程将包括JAVA的安装及一些基本的例子. windows下的安装 第一步:安装JDK,这是非常容易的,你只需一路回车的安装好.然后做好以下步骤. 在 Win9x 下加入 :"PATH=%PATH%;C:\jdk1.2.2\bin" 到AUTOEXEC.BAT文件中 在 NT 下加入 ";C:\jdk1.2.2\bin

从Java类库看设计模式(2)

在上一部分的内容中,我们讲到什么是模式,什么是设计模式,以及对一个设计模式 Observer的详细阐叙.相信大家对于模式的概念应该是比较的理解了.这部分及以后的内容 ,将会步入正题,从Java类库的分析入手,来阐叙设计模式是如何应用到一个完美的设计中 的.实际上,Java类库非常的庞杂,这儿不可能把所有能够找到的设计模式的例子一一列举 ,只是找了一些容易发现的例子.实际上也没有必要,因为只要对一个设计模式有足够的理 解,对于它的具体应用而言,倒是一件不是很困难的事情. Command模式 在设计

从Java类库看设计模式(1)

在这一部分的内容中,介绍的是一个相对简单但功能强大的模式:Observer模式.希望通 过这部分地叙述,大家看了之后,能够对设计模式有一个比较全面地,感性的认识. 很多时候,对于一个设计来说(软件上的,建筑上的,或者它他工业上的),经验是至关 重要的.好的经验给我们以指导,并节约我们的时间:坏的经验则给我们以借鉴,可以减少 失败的风险.然而,从知识层面上来讲,经验只是作为一种工作的积累而存在于个人的大脑 中的,很难被传授或者记录.为了解决这样的问题,人们提出了所谓的模式的概念.所谓模 式,是指在

Thumbnailator v0.3.0发布 图像缩略图的Java类库

Thumbnailator 是一个用来生成图像缩略图的 Java 类库,通过很简单的代码即可生成图片缩略图,也可直接对一整个目录的图片生成缩略图. File originalhttp://www.aliyun.com/zixun/aggregation/19352.html">File = new File("original.jpg");File thumbnailFile = new File("thumbnail.jpg");Thumbnail

ExpressionJ v0.3.4发布 解析简单算术表达式的Java类库

ExpressionJ 是一个用来解析简单的算术表达式的 Java 类库. ExpressionJ is a Java library allowing to interpret simple numeric expressions, which may be used in all applications which have to combine numeric http://www.aliyun.com/zixun/aggregation/9541.html">values, bu

ExpressionJ v0.3.3发布 Java类库数字算术表达式

ExpressionJ是一个Java类库用于解析简单的数字算术表达式. 该版本修复了重置 FunctionsDefinitions的bug. This project was registered on SourceForge.net on Dec 31, 2010, and is described by the project team as follows: ExpressionJ is a Java library allowing to interpret simple numeric

ExpressionJ 0.8 Beta 1发布 解析简单算术表达式的Java类库

ExpressionJ 0.8 Beta 1此版本现在所有的单元都测试通过,并在相同的情况下,性能比0.7版本相比似乎提升了50%-100%. ExpressionJ 是一个用来解析简单的算术表达式的Java类库. 下载地址:http://sourceforge.net/projects/expressionj/files/920.html">Release%200.8/