We have a map of Student to record Map<Student, StudentRecord>
.
Student class is as follows:
Student {
String id;
String grade;
Int age;
}
Additionally, we have a list of Student Id (List<String>)
provided.
Using Java streams, what would be the most efficient way to filter out records of students whose Id exists in the provided list?
The expected outcome is the filtered list mapped against the Id(String) - <Map<Id, StudentRecord>>
You can stream set of entries:
map.entrySet().stream()
.filter(e -> list.contains(e.getKey()))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
If you also want to map keys to id
field, then:
map.entrySet().stream()
.filter(e -> list.contains(e.getKey()))
.collect(toMap(e -> e.getKey().getId(), Map.Entry::getValue));
First of all, I'd convert your List
to a Set
, to avoid linear search time:
List<String> ids = ...
Set<String> idsSet = new HashSet<>(ids);
Now, you can stream over the entries of the Map
, filter out those having ids in the List
/Set
, and collect the remaining ones to an output Map
:
Map<String,StudentRecord> filtered =
input.entrySet()
.stream()
.filter(e -> !idsSet.contains(e.getKey().getId()))
.collect(Collectors.toMap(e -> e.getKey().getId(),Map.Entry::getValue));
Although other answers are correct but I think they are not more efficient since they use temporary memory or its complicity is not o(n)
.
the other answer is like this:
provided.stream()
.map(id -> new AbstractMap.SimpleEntry<>(id, map.entrySet()
.stream().filter(st -> st.getKey().id == id)
.map(Map.Entry::getValue).findFirst()))
.filter(simpleEntry ->simpleEntry.getValue().isPresent())
.map(entry-> new AbstractMap.SimpleEntry<>(entry.getKey(), entry.getValue().get()))
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With