码迷,mamicode.com
首页 > 编程语言 > 详细

java Multimap

时间:2017-05-29 19:57:16      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:nan   highlight   实现   void   multimap   apple   int   hash   put   

实现

{
    "a": [
        1,
        2,
        3
    ],
    "b": [
        3,
        7
    ]
}

当然, HashMap<String, List<Integer>> 是可以的,但是不方便。这里使用Multimap

举例

package com.example.ch6_2;
import java.util.Collections;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;


public class testMultimap {
    public static void main(String[] argv) {
        Multimap<String, String> myMultiMap = ArrayListMultimap.create();
        myMultiMap.put("Fruits", "Apple");
        myMultiMap.put("Fruits", "Pear");
        myMultiMap.put("Fruits", "Banana");
        myMultiMap.put("Vegetables", "eggplant");

        System.out.println("\nmyMultiMap size:" + myMultiMap.size());

        System.out.println("\nmyMultiMap.get(Fruits): " + myMultiMap.get("Fruits"));
        System.out.println("\nmyMultiMap.get(Vegetables): " + myMultiMap.get("Vegetables"));

        System.out.println("\nkeys:");
        for (String key : myMultiMap.keys()) {
            System.out.println(key);
        }

        System.out.println("\nvalues:");
        for (String value : myMultiMap.values()) {
            System.out.println(value);
        }

        myMultiMap.remove("Fruits", "Apple");
        System.out.println("\nmyMultiMap size:" + myMultiMap.size());

        myMultiMap.removeAll("Fruits");
        System.out.println("\nmyMultiMap size:" + myMultiMap.size());
    }
}

结果

myMultiMap size:4

myMultiMap.get(Fruits): [Apple, Pear, Banana]

myMultiMap.get(Vegetables): [eggplant]

keys:
Vegetables
Fruits
Fruits
Fruits

values:
eggplant
Apple
Pear
Banana

myMultiMap size:3

myMultiMap size:1

  

java Multimap

标签:nan   highlight   实现   void   multimap   apple   int   hash   put   

原文地址:http://www.cnblogs.com/kaituorensheng/p/6918809.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!