简体   繁体   English

如何获取MultiMap值中元素的索引

[英]How to get index of an element in value of MultiMap

I want to check if the element "Item Two" is in the values of MultiMap get index of that element , below is my code. 我想检查元素“ Item Two”是否在MultiMap的值中,获取该元素的索引,下面是我的代码。

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")+")");
       } 
   }    
} 

And the Output is: 输出为:

mhm-keys : [Item One, Item Two, Item Three]
value at is : false
mhm-keys : [Item Four, Item Five]
value at is : false

Could you please help as I cannot use something else than MultiMap 因为我不能使用MultiMap以外的其他功能,所以请您帮忙

First of all, MultiValueMap is deprecated, you should use MultiValuedMap instead. 首先,不建议使用MultiValueMap,而应改用MultiValuedMap。 https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/MultiValuedMap.html https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/MultiValuedMap.html

MultiValuedMap<String, String> mhm = new ArrayListValuedHashMap<String, String>();

But if you want to use the deprecated MultiValueMap, that's fine by me. 但是,如果您要使用已弃用的MultiValueMap,对我来说很好。 For both, you can use the following: 对于两者,您都可以使用以下内容:

mhm.containsValue(value);

you can also use: 您还可以使用:

List<String> itemsWithKey = mhm.get(key);
System.out.println(itemsWithKey.contains(value)); 

Update: This works for me: 更新:这对我有用:

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"));

This returns: 返回:

true
false

Update 2 This checks "Item Four" in all the keys and return true/false for each key 更新2这将检查所有键中的“项目4”,并为每个键返回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); 
}

The result is: 结果是:

does Item Four exists for key Group One: false
does Item Four exists for key Group Two: true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM