Check in Map of list if exists value for specific key Java 8
Solution 1
You can simply use m.get("Name2")
, place the (nullable) result into an Optional
and then use a mapping:
boolean result = Optional.ofNullable(m.get("Name2"))
.map(l -> l.stream().anyMatch(s -> s.contains("@")))
.orElse(false);
This is preferable to looping over the entry set, as HashMap.get
is O(1) and iterating over the entry set is O(n).
Solution 2
Do the following:
boolean result = m.getOrDefault("Name2", Collections.emptyList()).stream()
.anyMatch(i -> i.contains("@"));
If the Map
contains a correct key, check whether any of its element of the List
as value contains the particular character. If the Map
doesn’t contain the key, mock the empty Collection
which doesn't contain anything at all and the result is evaluated automatically as false
.
Edit: As @Michael suggested, using the Collections.emptyList()
is a better choice than new ArrayList<>()
.
Solution 3
Try this:
boolean result = m.entrySet().stream()
.filter(e -> e.getKey().equals(Name2))
.map(Map.Entry::getValue)
.flatMap(List::stream)
.anyMatch(s -> s.contains("@"));
Solution 4
What about
String name = "Name1";
boolean result= m.containsKey(name) && m.get(name).stream().anyMatch(a -> a.contains("@"));
Solution 5
create a stream from the entrySet()
and then provide your criteria in the anyMatch
:
result = m.entrySet()
.stream()
.anyMatch(v -> Objects.equals("Name2", v.getKey()) &&
v.getValue().stream().anyMatch(s -> s.contains("@")));
or using getOrDefault
:
result = m.getOrDefault("Name2", Collections.emptyList())
.stream()
.anyMatch(s -> s.contains("@"));
Marinescu Raluca
Updated on July 09, 2022Comments
-
Marinescu Raluca almost 2 years
In Java 7 I have
Map<String, List<String>> m = new HashMap<String, List<String>>(); boolean result = false; m.put("Name1", Arrays.asList("abc*1")); m.put("Name2", Arrays.asList("abc@*1")); for (Map.Entry<String, List<String>> me : m.entrySet()) { String key = me.getKey(); List<String> valueList = me.getValue(); if (key.equals("Name2"){ System.out.print("Values: "); for (String s : valueList) { if(s.contains("@"){ result = true; } } } }
How can I get în a bool result for
Name2
if it contains@
using any match?I tried using The following Code but I Don t know how to use IT for specific key
result = m.values().stream().anyMatch(v -> v.contains("@"))
-
Michael almost 6 yearsThis is the best way to do it. Why iterate over all of the keys when most maps are specifically designed to have a O(1)
get
? -
Ousmane D. almost 6 yearsthe
map
should be.map(l -> l.stream().anyMatch(s -> s.contains("@")))
-
Holger almost 6 yearsBetter:
Optional.ofNullable(m.get("Name2")) .filter(l -> l.stream().anyMatch(s -> s.contains("@"))) .isPresent()
-
Holger almost 6 years@Michael Java 9 offers an in-between solution:
Stream.ofNullable(m.get("Name2")) .flatMap(List::stream) .anyMatch(i -> i.contains("@"))
-
Michael almost 6 years@Holger That's sufficiently different as to be worthy of its own answer, don't you think?
-
Holger almost 6 years@Michael it’s different code, but not a fundamentally different logic, so I wouldn’t add an answer consisting of a small code snippet only. You can add it as an alternative to your answer, if you wish.
-
Michael almost 6 years@Holger Hm, but most of the answers here aren't fundamentally different logic. They're all variations around 1 or 2 ideas. But that's up to you. I've found it interesting all of the subtly different ways that people have solved this problem, anyway.
-
Holger almost 6 years@Michael I have no objections against all these answers, but I don’t want to add another one. By the way, I see a fundamental difference. There are answers doing a linear iteration over the map and there are answers which avoid that.