Java 处理中的多重映射

Java 处理中的多重映射,java,hashmap,processing,multimap,Java,Hashmap,Processing,Multimap,我想知道一种在IDE中使用multimap的方法 有我可以使用的图书馆吗 我需要为映射中的同一个键添加几个值 非常感谢你的帮助 您可以添加以下库 Multimap myMultimap=ArrayListMultimap.create(); 公共类MutliMapTest{ 公共静态void main(字符串…参数){ Multimap myMultimap=ArrayListMultimap.create(); //添加一些键/值 myMultimap.put(“水果”、“班纳纳”); my

我想知道一种在IDE中使用multimap的方法

有我可以使用的图书馆吗

我需要为映射中的同一个键添加几个值


非常感谢你的帮助

您可以添加以下库

Multimap myMultimap=ArrayListMultimap.create();
公共类MutliMapTest{
公共静态void main(字符串…参数){
Multimap myMultimap=ArrayListMultimap.create();
//添加一些键/值
myMultimap.put(“水果”、“班纳纳”);
myMultimap.put(“水果”、“苹果”);
myMultimap.put(“水果”、“梨”);
myMultimap.put(“蔬菜”、“胡萝卜”);
//得到尺寸
int size=myMultimap.size();
System.out.println(大小);//4
//获取价值
收集水果=myMultimap.get(“水果”);
System.out.println(水果);//[香蕉、苹果、梨]
收集蔬菜=myMultimap.get(“蔬菜”);
System.out.println(蔬菜);//[胡萝卜]
//在整个Mutlimap上迭代
for(字符串值:myMultimap.values()){
系统输出打印项次(值);
}
//删除单个值
myMultimap.移除(“水果”、“梨”);
System.out.println(myMultimap.get(“水果”);/[Bannana,Pear]
//删除键的所有值
myMultimap.removeAll(“水果”);
System.out.println(myMultimap.get(“水果”);//[](空集合!)
}
}
你可以从这里下载


Multimap似乎是Apache commons collections库的一部分。从(commons-collections4-4.0-bin.zip)下载包含它的zip文件,解压缩它并从那里获取jar文件(commons-collections4-4.0.jar)。然后,您必须在草图文件夹中创建一个名为“code”的文件夹,并将jar放在其中

下面是一个如何在处理中使用它的示例:

import java.util.Collection;

void setup() {
 MultiMap mhm = new MultiValueMap();
 mhm.put("Fruit", "Apple");
 mhm.put("Fruit", "Banana");
 mhm.put("Fruit", "Kiwi");
 Collection coll = (Collection) mhm.get("Fruit");
 for(Object o: coll) {
   println(o);
 }
}
…或者,如果您知道只将字符串作为值放置,则不需要导入:

void setup() {
 MultiMap mhm = new MultiValueMap();
 mhm.put("Fruit", "Apple");
 mhm.put("Fruit", "Banana");
 mhm.put("Fruit", "Kiwi");
 ArrayList<String> coll = (ArrayList<String>) mhm.get("Fruit");
 for(String o: coll) {
   println(o);
 }
}
void setup(){
MultiMap mhm=新的多值映射();
水果、苹果;
水果、香蕉;
mhm.put(“水果”、“猕猴桃”);
ArrayList coll=(ArrayList)mhm.get(“水果”);
for(字符串o:coll){
println(o);
}
}
…或者,你自己滚吧!像这样:

// This essentially says that you want to create a map 
// with keys of type String and values of type List of String
Map<String, List<String>> myMap = new HashMap<String,List<String>>();

// add a new ArrayList of Strings in the map:
myMap.put("Fruits",new ArrayList<String>());

// add Strings in the list:
myMap.get("Fruits").add("Apple");
myMap.get("Fruits").add("Banana");
//这实际上是说您想要创建一个映射
//具有字符串类型的键和字符串列表类型的值
Map myMap=newhashmap();
//在映射中添加新的字符串数组列表:
put(“水果”,新的ArrayList());
//在列表中添加字符串:
myMap.get(“水果”).add(“苹果”);
myMap.get(“水果”).add(“香蕉”);

谢谢,我如何在processing IDE中导入此库?如果您使用的是
Eclipse
,请将项目
lib
文件夹添加到
processing IDE
中,我对此一无所知。该下载链接只有一些PDF文件。不是吗?不应该有任何jar文件吗?在Processing IDE中添加LIB与在Eclipse中添加LIB是一样的。我下载了jar文件,现在通过处理
Multimap
identify无法识别集合类型?现在你的问题
收集
无法识别
Collection
是Java的一部分,您必须导入
Java.util.Collection
谢谢,您能用一个例子详细说明一下吗?@Hasala yep,这就是Hanks Petros。非常感谢你的帮助。
// This essentially says that you want to create a map 
// with keys of type String and values of type List of String
Map<String, List<String>> myMap = new HashMap<String,List<String>>();

// add a new ArrayList of Strings in the map:
myMap.put("Fruits",new ArrayList<String>());

// add Strings in the list:
myMap.get("Fruits").add("Apple");
myMap.get("Fruits").add("Banana");