1
我想检查一个值是否存在。检查List是否是HashMap中的值
我的代码:
public static final Map<String, List<String>> m = new HashMap<>();
if(m.containsValue("gummi")) {...}
我的问题是该值是一个列表。 如何检查清单?
我想检查一个值是否存在。检查List是否是HashMap中的值
我的代码:
public static final Map<String, List<String>> m = new HashMap<>();
if(m.containsValue("gummi")) {...}
我的问题是该值是一个列表。 如何检查清单?
从List.equals
Javadoc中引用:
换句话说,两个列表被定义为等于如果它们包含以相同的顺序相同的元件。
因此,要检查是否有存在的价值,你可以创建一个列表,并调用containsValue
与List
,因为这种方法会调用equals
确定的平等。如果相同元素的顺序相同,则会找到匹配项。
您可以轻松创建值数组的List
与Arrays.asList
:
if(m.containsValue(Arrays.asList("gummi"))) {...}
示例代码:
Map<String, List<String>> m = new HashMap<>();
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
m.put("key", list);
if (m.containsValue(Arrays.asList("1", "2"))) {
System.out.println("contains value");
}
这要求主叫者知道这是一个值列表中的所有成员。 –