Java 如果哈希列表中存在元素,如何返回键<;字符串,列表<;对象>>;?
如果键中的列表包含特定值,我需要获取该键 我唯一能想到的方法是迭代HashMap和for循环以获得每个键的值列表,然后检查列表是否包含值并返回键。大概是这样的:Java 如果哈希列表中存在元素,如何返回键<;字符串,列表<;对象>>;?,java,algorithm,sorting,Java,Algorithm,Sorting,如果键中的列表包含特定值,我需要获取该键 我唯一能想到的方法是迭代HashMap和for循环以获得每个键的值列表,然后检查列表是否包含值并返回键。大概是这样的: Map<String, List<MyItem>> map = new HashMap<>(); List<MyItem> list = new List<>(); list.add(new MyItem("Kim", 25); list.add(new MyItem("Lee
Map<String, List<MyItem>> map = new HashMap<>();
List<MyItem> list = new List<>();
list.add(new MyItem("Kim", 25);
list.add(new MyItem("Lee", 28);
map.put("Samsung", list);
String searchKeyWord = "Kim";
String myKey = getKeyByValue(map, searchKeyWord);
System.out.println("Found Key: " + myKey);
Map<String, Map<String, MyItem>> m = new HashMap<>();
Map<String, MyItem> items = Map.of(
"Kim", new MyItem("Kim", 25),
"Lee", new MyItem("Lee", 28)
);
m.put("Samsung", items);
String result = m.entrySet().stream()
.filter(e -> e.getValue().containsKey(searchKeyWord))
.findAny()
.map(Map.Entry::getKey)
.orElse(null);
Map Map=newhashmap();
列表=新列表();
增加(新的MyItem(“Kim”,25);
增加(新的MyItem(“Lee”,28);
地图放置(“三星”,列表);
字符串searchKeyWord=“Kim”;
字符串myKey=getKeyByValue(映射,搜索关键字);
System.out.println(“找到的键:+myKey”);
我不知道最好的办法是什么
一,
公共字符串getKeyByValue(注释中的映射解释:
private static String getKeyByValue(Map<String, List<MyItem>> map, String searchKeyWord) {
return map.entrySet().stream() //all entries in the map
.filter(e -> e.getValue().stream()
.anyMatch(i -> i.getName().equals(searchKeyWord))) //take only the ones which have searchKeyword in their list
.findAny() //take just one such entry
.map(Map.Entry::getKey) //change Entry to String (the key)
.orElse(null); //if there is no such entry, return null
}
map.entrySet().stream().filter(entry->entry.getValue().stream().anyMatch(item->item.name.equals(searchKeyWord)).map(entry::getKey.findAny().orElse(null)
Good lord…一行全明星解决方案正在玷污美…for(entry-entry-entry:map.entrySet())for(MyItem-item-item:entry.getValue())if(item.name.equals)(搜索关键字))返回true;返回false;
排序是浪费时间。我必须问@shmosel,如果它值得,为什么不回答它,这样你就可以提供一些上下文和细节?注意,我会返回一个空的可选的,而不是空的空的,但这只是个人品味的问题,我想…@mcepender完全同意。更新ed.谢谢,但我可以知道它的时间复杂性吗?@ChanjungKim第一种方法扫描地图的每个元素,直到找到匹配项,所以它是O(n),然后它扫描列表的所有元素,也就是O(m),所以总数变成O(mn)
。第二种方法在O(n)和O(1)中做两件事,所以它是O(n)
。我对O表示法不是很在行,所以我可能错了。
private static String getKeyByValue(Map<String, List<MyItem>> map, String searchKeyWord) {
return map.entrySet().stream() //all entries in the map
.filter(e -> e.getValue().stream()
.anyMatch(i -> i.getName().equals(searchKeyWord))) //take only the ones which have searchKeyword in their list
.findAny() //take just one such entry
.map(Map.Entry::getKey) //change Entry to String (the key)
.orElse(null); //if there is no such entry, return null
}
Map<String, Map<String, MyItem>> m = new HashMap<>();
Map<String, MyItem> items = Map.of(
"Kim", new MyItem("Kim", 25),
"Lee", new MyItem("Lee", 28)
);
m.put("Samsung", items);
String result = m.entrySet().stream()
.filter(e -> e.getValue().containsKey(searchKeyWord))
.findAny()
.map(Map.Entry::getKey)
.orElse(null);