Mam*_*dal 3 java android arraylist hashmap
我有一个类型的哈希图String,ArrayList<String>.两个不同keys的存储在哈希映射中,其中包含值列表.
现在我必须比较不同键的值并提取公共值.怎么能实现这个功能?
以下是Hashmap我使用的类型:
示例列表:
{Size=[43, 53, 63, 48, 58], Color=[66, 62, 65, 64, 63]}
Run Code Online (Sandbox Code Playgroud)
这是代码......
private HashMap<String, ArrayList<String>> mapMatchvalues = new HashMap<>();
for (Map.Entry<String, ArrayList<String>> map3 : mapMatchvalues.entrySet()) {
List<String> getList1 = new ArrayList<>();
getList1 = map3.getValue();
for (int i = 0; i < getList1.size(); i++) {
if (getList.contains(getList1.get(i))) {
//Print values
} else {
// Print if not matched....
}
}
}
Run Code Online (Sandbox Code Playgroud)
你可以使用retainAll.ArrayList如果您不想影响现有列表中的值,请使用new .
List<String> common = new ArrayList<>(mapMatchvalues.get("key1"));
common.retainAll(mapMatchvalues.get("key2"));
Run Code Online (Sandbox Code Playgroud)
common 将包含列表中的匹配元素.
如果地图中有多个条目,则可以循环显示
// initialize the common list with List
List<String> common = new ArrayList<>(mapMatchvalues.entrySet().iterator().next().getValue());
for (List<String> list : mapMatchvalues.values()) {
common.retainAll(list);
}
Run Code Online (Sandbox Code Playgroud)