如何获取 MultiMap 值中元素的索引
How to get index of an element in value of MultiMap
我想检查元素 "Item Two" 是否在 MultiMap get index of that element 的值中,下面是我的代码。
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.apache.commons.collections.MultiMap;
import org.apache.commons.collections.map.MultiValueMap;
public class Test {
public static void main(String args[]) {
MultiMap mhm = new MultiValueMap();
String key ="";
key = "Group One";
mhm.put(key, "Item One");
mhm.put(key, "Item Two");
mhm.put(key, "Item Three");
key = "Group Two";
mhm.put(key, "Item Four");
mhm.put(key, "Item Five");
Set keys = mhm.keySet();
for (Object k : keys) {
System.out.println("+k+“ : "+mhm.get(k)+")");
List benefit = new ArrayList();
benefit.add(mhm.get(k));
System.out.println("+value at is+“ : "+benefit.contains("Item One")+")");
}
}
}
输出为:
mhm-keys : [Item One, Item Two, Item Three]
value at is : false
mhm-keys : [Item Four, Item Five]
value at is : false
你能帮忙吗,因为我不能使用 MultiMap 以外的东西
首先,MultiValueMap 已被弃用,您应该改用 MultiValuedMap。
https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/MultiValuedMap.html
MultiValuedMap<String, String> mhm = new ArrayListValuedHashMap<String, String>();
但是,如果您想使用已弃用的 MultiValueMap,那对我来说没问题。对于两者,您可以使用以下内容:
mhm.containsValue(value);
您还可以使用:
List<String> itemsWithKey = mhm.get(key);
System.out.println(itemsWithKey.contains(value));
更新:
这对我有用:
MultiMap mhm = new MultiValueMap();
String key ="";
key = "Group One";
mhm.put(key, "Item One");
mhm.put(key, "Item Two");
mhm.put(key, "Item Three");
key = "Group Two";
mhm.put(key, "Item Four");
mhm.put(key, "Item Five");
System.out.println(mhm.containsValue("Item One"));
System.out.println(mhm.containsValue("Item Nine"));
这个returns:
true
false
更新 2
这会检查所有键的 "Item Four" 和每个键的 return true/false
MultiMap mhm = new MultiValueMap();
String key ="";
key = "Group One";
mhm.put(key, "Item One");
mhm.put(key, "Item Two");
mhm.put(key, "Item Three");
key = "Group Two";
mhm.put(key, "Item Four");
mhm.put(key, "Item Five");
Set<String> keys = mhm.keySet();
String itemToLookFor = "Item Four";
for(String k : keys) {
List<String> itemsWithKey = (List<String>) mhm.get(k);
boolean doesExists = itemsWithKey.contains(itemToLookFor);
System.out.println("does " + itemToLookFor + " exists for key " + k + ": " +doesExists);
}
结果是:
does Item Four exists for key Group One: false
does Item Four exists for key Group Two: true
我想检查元素 "Item Two" 是否在 MultiMap get index of that element 的值中,下面是我的代码。
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.apache.commons.collections.MultiMap;
import org.apache.commons.collections.map.MultiValueMap;
public class Test {
public static void main(String args[]) {
MultiMap mhm = new MultiValueMap();
String key ="";
key = "Group One";
mhm.put(key, "Item One");
mhm.put(key, "Item Two");
mhm.put(key, "Item Three");
key = "Group Two";
mhm.put(key, "Item Four");
mhm.put(key, "Item Five");
Set keys = mhm.keySet();
for (Object k : keys) {
System.out.println("+k+“ : "+mhm.get(k)+")");
List benefit = new ArrayList();
benefit.add(mhm.get(k));
System.out.println("+value at is+“ : "+benefit.contains("Item One")+")");
}
}
}
输出为:
mhm-keys : [Item One, Item Two, Item Three]
value at is : false
mhm-keys : [Item Four, Item Five]
value at is : false
你能帮忙吗,因为我不能使用 MultiMap 以外的东西
首先,MultiValueMap 已被弃用,您应该改用 MultiValuedMap。 https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/MultiValuedMap.html
MultiValuedMap<String, String> mhm = new ArrayListValuedHashMap<String, String>();
但是,如果您想使用已弃用的 MultiValueMap,那对我来说没问题。对于两者,您可以使用以下内容:
mhm.containsValue(value);
您还可以使用:
List<String> itemsWithKey = mhm.get(key);
System.out.println(itemsWithKey.contains(value));
更新: 这对我有用:
MultiMap mhm = new MultiValueMap();
String key ="";
key = "Group One";
mhm.put(key, "Item One");
mhm.put(key, "Item Two");
mhm.put(key, "Item Three");
key = "Group Two";
mhm.put(key, "Item Four");
mhm.put(key, "Item Five");
System.out.println(mhm.containsValue("Item One"));
System.out.println(mhm.containsValue("Item Nine"));
这个returns:
true
false
更新 2 这会检查所有键的 "Item Four" 和每个键的 return true/false
MultiMap mhm = new MultiValueMap();
String key ="";
key = "Group One";
mhm.put(key, "Item One");
mhm.put(key, "Item Two");
mhm.put(key, "Item Three");
key = "Group Two";
mhm.put(key, "Item Four");
mhm.put(key, "Item Five");
Set<String> keys = mhm.keySet();
String itemToLookFor = "Item Four";
for(String k : keys) {
List<String> itemsWithKey = (List<String>) mhm.get(k);
boolean doesExists = itemsWithKey.contains(itemToLookFor);
System.out.println("does " + itemToLookFor + " exists for key " + k + ": " +doesExists);
}
结果是:
does Item Four exists for key Group One: false
does Item Four exists for key Group Two: true