我有以下HashMap
,其中key
是String
而value
由ArrayList
代表:
HashMap<String, ArrayList<String>> productsMap = AsyncUpload.getFoodMap();
我的应用程序中还实现了另一个ArrayList<String> foods
。
我的问题是,找出我的HashMap
是否包含我的第二个String
中的特定ArrayList
的最佳方法是什么?
我尝试过没有成功:
Iterator<String> keySetIterator = productsMap.keySet().iterator();
Iterator<ArrayList<String>> valueSetIterator = productsMap.values().iterator();
while(keySetIterator.hasNext() && valueSetIterator.hasNext()){
String key = keySetIterator.next();
if(mArrayList.contains(key)){
System.out.println("Yes! its a " + key);
}
}
答案 0 :(得分:16)
为什么不:
// fast-enumerating map's values
for (ArrayList<String> value: productsMap.values()) {
// using ArrayList#contains
System.out.println(value.contains("myString"));
}
如果你必须遍历整个ArrayList<String>
,而不是仅查找一个特定值:
// fast-enumerating food's values ("food" is an ArrayList<String>)
for (String item: foods) {
// fast-enumerating map's values
for (ArrayList<String> value: productsMap.values()) {
// using ArrayList#contains
System.out.println(value.contains(item));
}
}
修改强>
过去我用一些Java 8习语更新了这个。
Java 8流API允许以更具声明性(并且可以说是优雅)的方式处理这些类型的迭代。
例如,这是一种(略显过于冗长)实现相同目的的方法:
// iterate foods
foods
.stream()
// matches any occurrence of...
.anyMatch(
// ... any list matching any occurrence of...
(s) -> productsMap.values().stream().anyMatch(
// ... the list containing the iterated item from foods
(l) -> l.contains(s)
)
)
...这里有一种更简单的方法来实现相同的,最初迭代productsMap
值而不是foods
的内容:
// iterate productsMap values
productsMap
.values()
.stream()
// flattening to all list elements
.flatMap(List::stream)
// matching any occurrence of...
.anyMatch(
// ... an element contained in foods
(s) -> foods.contains(s)
)
答案 1 :(得分:9)
您需要使用containsKey()
方法。为此,您只需获取希望密钥输出的hashMap,然后使用containsKey
方法,如果有,则返回boolean
值。这将搜索整个hashMap,而不必迭代每个项目。如果您有密钥,则只需检索该值即可。
它可能看起来像:
if(productsMap.values().containsKey("myKey"))
{
// do something if hashMap has key
}
以下是Android
的链接来自Android文档:
public boolean containsKey(Object key)在API级别1中添加
返回此映射是否包含指定的键。参数键 搜索的关键。返回
true if this map contains the specified key, false otherwise.
答案 2 :(得分:3)
试试这个:
public boolean anyKeyInMap(Map<String, ArrayList<String>> productsMap, List<String> keys) {
Set<String> keySet = new HashSet<>(keys);
for (ArrayList<String> strings : productsMap.values()) {
for (String string : strings) {
if (keySet.contains(string)) {
return true;
}
}
}
return false;
}
答案 3 :(得分:3)
hashMap.containsValue("your value");
将检查HashMap是否包含值
答案 4 :(得分:1)
试试这个
Iterator<String> keySetIterator = productsMap.keySet().iterator();
Iterator<ArrayList<String>> valueSetIterator = productsMap.values().iterator();
while(keySetIterator.hasNext()){
String key = keySetIterator.next();
if(valueSetIterator.next().contains(key)){ // corrected here
System.out.println("Yes! its a " + key);
}
}
答案 5 :(得分:1)
如果你想要Java 8的方式,你可以做这样的事情:
private static boolean containsValue(final Map<String, ArrayList<String>> map, final String value){
return map.values().stream().filter(list -> list.contains(value)).findFirst().orElse(null) != null;
}
或类似的东西:
private static boolean containsValue(final Map<String, ArrayList<String>> map, final String value){
return map.values().stream().filter(list -> list.contains(value)).findFirst().isPresent();
}
两者都应该产生几乎相同的结果。
答案 6 :(得分:1)
这是一个测试两种方案的示例方法。搜索地图键中的项目,并搜索每个地图条目列表中的项目:
private static void testMapSearch(){
final ArrayList<String> fruitsList = new ArrayList<String>();
fruitsList.addAll(Arrays.asList("Apple", "Banana", "Grapes"));
final ArrayList<String> veggiesList = new ArrayList<String>();
veggiesList.addAll(Arrays.asList("Potato", "Squash", "Beans"));
final Map<String, ArrayList<String>> productsMap = new HashMap<String, ArrayList<String>>();
productsMap.put("fruits", fruitsList);
productsMap.put("veggies", veggiesList);
final ArrayList<String> foodList = new ArrayList<String>();
foodList.addAll(Arrays.asList("Apple", "Squash", "fruits"));
// Check if items from foodList exist in the keyset of productsMap
for(String item : foodList){
if(productsMap.containsKey(item)){
System.out.println("productsMap contains a key named " + item);
} else {
System.out.println("productsMap doesn't contain a key named " + item);
}
}
// Check if items from foodList exits in productsMap's values
for (Map.Entry<String, ArrayList<String>> entry : productsMap.entrySet()){
System.out.println("\nSearching on list values for key " + entry.getKey() + "..");
for(String item : foodList){
if(entry.getValue().contains(item)){
System.out.println("productMap's list under key " + entry.getKey() + " contains item " + item);
} else {
System.out.println("productMap's list under key " + entry.getKey() + " doesn't contain item " + item);
}
}
}
}
结果如下:
productsMap不包含名为Apple的密钥
productsMap不包含名为Squash的键
productsMap包含一个名为fruits的键
搜索关键水果的列表值..
productMap在主要成果下的列表包含项目Apple
关键水果下的productMap的列表不包含项目Squash
关键水果下的productMap列表不包含项目水果
搜索关键蔬菜的列表值..
关键蔬菜下的productMap列表不包含项目Apple
productMap在关键蔬菜下的列表包含项目Squash
关键蔬菜下的productMap列表不包含项目水果