首页 > 代码库 > java Multimap
java Multimap
实现
{ "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:4myMultiMap.get(Fruits): [Apple, Pear, Banana]myMultiMap.get(Vegetables): [eggplant]keys:VegetablesFruitsFruitsFruitsvalues:eggplantApplePearBananamyMultiMap size:3myMultiMap size:1
java Multimap
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。