[英]Java - Check if value in ArrayList nested in HashMap exists
I have various ArrayList
s nested in one HashMap
. 我在一个HashMap
嵌套了各种ArrayList
。
How can I check if my array list nested in the hashmap exists? 如何检查嵌套在哈希图中的数组列表是否存在?
public static HashMap<Integer, ArrayList<Integer>> transactionMap = new HashMap<Integer, ArrayList<Integer>>();
public static void initializeLists() {
transactionMap.put(7, new ArrayList<Integer> (Arrays.asList(1050, 1125)));
transactionMap.put(305, new ArrayList<Integer>(Arrays.asList(1125)));
transactionMap.put(1125, new ArrayList<Integer>(Arrays.asList(250, 252, 251)));
transactionMap.put(1050, new ArrayList<Integer>(Arrays.asList(251, 252)));
transactionMap.put(1124, new ArrayList<Integer>(Arrays.asList(250)));
transactionMap.put(1049, new ArrayList<Integer>(Arrays.asList(251, 252)));
}
This post here describes how to check directly in an Array List
using #contains(Object)
, which does not fit for HashMap
. 此处的这篇文章介绍了如何使用#contains(Object)
直接检查Array List
,该列表不适用于HashMap
。
This one is close but it would mean I would have to give a name to all my lists (and there could be many). 这是很接近的,但这意味着我必须在所有列表中都起一个名字(可能有很多)。 I would like to avoid having to nest a map inside a map if possible unless needed. 如果可能的话,我想避免在必要时将地图嵌套在地图中。 Does anyone have any points to the best solution? 有谁对最佳解决方案有什么看法?
If your question is "how do I check if a specific list is a value in the map" then you can do it as follows: 如果您的问题是“如何检查特定列表是否在地图中是一个值”,则可以按照以下步骤进行操作:
if (transactionMap.containsValue(Arrays.asList(1, 2, 3)) {
...
}
If your question is "how do I check if a specific integer appears somewhere in a list as a value in the map" then you can do the following (using Java 8): 如果您的问题是“如何检查特定整数是否在列表中的某个位置作为映射中的值”,则可以执行以下操作(使用Java 8):
if (transactionMap.values().stream().anyMatch(l -> l.contains(17)) {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.