Java –从值HashMap获取键
本文介绍了几种从HashMap中的值获取键的方法。
1.HashMap的键和值可以包含null
2.从HashMap获取所有键–keySet()
3.从HashMap中的值获取键
4.从HashMap(Java8Stream)中的值获取键
5.HashMap只有一项
当我们创建一种从HashMap中的值查找键的方法时,我们必须注意以下问题:
HashMap可以包含null用于键和值。
HashMap键是唯一的,这意味着不会重复。
HashMap不保证插入顺序。
单个或多个键与单个值匹配。
一个HashMap例子。
Mapmap = new HashMap<>(); map.put("key1", 1); map.put("key2", 2); map.put("key3", 3); map.put("key4", 3); map.put("key5", null); // 空值 map.put(null, 3); // 键为null
map.keySet()返回Set,并且HashMap不保证插入顺序。
Mapmap = new HashMap<>(); map.put("key1", 1); map.put("key2", 2); map.put("key3", 3); map.put("key4", 3); map.put("key5", null); map.put(null, 3); //Set keys = map.keySet(); // 打印所有键 for (String key : map.keySet()) { System.out.println(key); }
输出
终端
key1 null key2 key5 key3 key4
要查找映射到某个值的所有键,我们可以循环entrySet()和Objects.equals比较值并获取键。
注意
常见的错误是使用entry.getValue().equals(value)来比较值,因为如果HashMap包含一个null值,则该方法将抛出NullPointerException。
3.1查找所有包含的值的键3。
FindKeysFromMap.java
package com.mkyong.basic; import java.util.*; public class FindKeysFromMap { public static void main(String[] args) { Mapmap = new HashMap<>(); map.put("key1", 1); map.put("key2", 2); map.put("key3", 3); map.put("key4", 3); map.put("key5", null); map.put(null, 3); for (String key : getKeys(map, 3)) { System.out.println(key); } } private static Set getKeys(Map map, Integer value) { Set result = new HashSet<>(); if (map.containsValue(value)) { for (Map.Entry entry : map.entrySet()) { if (Objects.equals(entry.getValue(), value)) { result.add(entry.getKey()); } // 我们不能像这样比较,null会引发异常 /*(if (entry.getValue().equals(value)) { result.add(entry.getKey()); }*/ } } return result; } }
输出
终端
null key3 key4
3.2查找所有包含的值的键null。
for (String key : getKeys(map, null)) { System.out.println(key); }
输出
终端
key5
4.1以下是Java8中的等效示例。
FindKeysFromMapJava8.java
package com.mkyong.basic; import java.util.*; import java.util.stream.Collectors; public class FindKeysFromMapJava8 { public static void main(String[] args) { Mapmap = new HashMap<>(); map.put("key1", 1); map.put("key2", 2); map.put("key3", 3); map.put("key4", 3); map.put("key5", null); map.put(null, 3); for (String key : getKeysJava8(map, 3)) { System.out.println(key); } } /*private static Set getKeys(Map map, Integer value) { Set result = new HashSet<>(); if (map.containsValue(value)) { for (Map.Entry entry : map.entrySet()) { if (Objects.equals(entry.getValue(), value)) { result.add(entry.getKey()); } } } return result; }*/ private static Set getKeysJava8( Map map, Integer value) { return map .entrySet() .stream() .filter(entry -> Objects.equals(entry.getValue(), value)) .map(Map.Entry::getKey) .collect(Collectors.toSet()); } }
输出
终端
null key3 key4
4.2如果我们只想查找与该值匹配的第一个键。
private static OptionalgetKeysJava8( Map map, Integer value) { return map .entrySet() .stream() .filter(entry -> Objects.equals(entry.getValue(), value)) .map(Map.Entry::getKey) .findFirst(); }
4.3或者,如果我们想要一个List和Optional。
private static Optional> getKeysJava8Optional( Map
map, Integer value) { List collect = map .entrySet() .stream() .filter(entry -> Objects.equals(entry.getValue(), value)) .map(Map.Entry::getKey) .collect(Collectors.toList()); return Optional.of(collect);
5.1如果HashMap唯一的项只有一个,我们可以使用将转换为,并从数组中获取第一个项,这意味着从中获得第一个键。map.keySet().toArray()SetArray[0]HashMap
Mapmap = new HashMap<>(); map.put("key1", 1); Object key = map.keySet().toArray()[0]; System.out.println(key);
输出
key1
5.2我们可以使用map.keySet().toArray()[5]从HashMap中获取第六个键吗?答案是否定的,因为结果将是一个随机键,HashMap不能保证插入顺序。
不要尝试这个
Mapmap = new HashMap<>(); map.put("key1", 1); map.put("key2", 2); map.put("key3", 3); map.put("key4", 3); map.put("key5", null); map.put(null, 3); Object key = map.keySet().toArray()[5]; // 我们期望从HashMap获得第六个键 System.out.println(key); // 但是结果是随机键
注意:
HashMap不保证插入顺序,这个map.keySet().toarray()[0]仅适用于HashMap只有一个条目。