比较哈希映射和列表并返回值



我将联系人存储在用户手机中的地图中,电话号码作为键,联系人姓名作为值。此外,我还检索了用户注册时存储在Firebase中的phone_number。在下面的方法中,我正在比较此地图和列表。

公共静态集合包含(地图地图,列表列表({

Collection<String> keys = map.keySet();
Set<String> result = new HashSet<>();
for (String listItem:list) {
if (keys.contains(listItem)){
result.add(listItem);
}
}
return result;  
}

当地图的键匹配项(phone_number从 Firebase 检索时,我想获取与之关联的值。我该怎么做?

考虑使用 HashMap

//Considering this stored contacts
Map<String, String> contactsStored = new HashMap<>();
contactsStored.put("999 999 999", "Anne");
contactsStored.put("000 000 000", "Jonh");
//and this firebase data               
List<String> firebaseNumbers =  new ArrayList<>();
firebaseNumbers.add("000 000 000");
for (String firebaseNumberKey : firebaseNumbers) {
contactsStored.get(firebaseNumberKey);
}

最新更新