If I understand your filtering criteria correctly, you want to check if the filtered Stream you produced from the value List has any elements, and if so, pass the corresponding Map entry to the output Map.
Map<String, List<BoMLine>>
filtered = materials.entrySet()
.stream()
.filter(a->a.getValue()
.stream()
.anyMatch(l->MaterialDao.findMaterialByName(l.getMaterial())))
.collect(Collectors.toMap(e->e.getKey(),e->e.getValue()));
This is assuming MaterialDao.findMaterialByName(l.getMaterial()) returns a boolean.
Top answer 1 of 5
73
If I understand your filtering criteria correctly, you want to check if the filtered Stream you produced from the value List has any elements, and if so, pass the corresponding Map entry to the output Map.
Map<String, List<BoMLine>>
filtered = materials.entrySet()
.stream()
.filter(a->a.getValue()
.stream()
.anyMatch(l->MaterialDao.findMaterialByName(l.getMaterial())))
.collect(Collectors.toMap(e->e.getKey(),e->e.getValue()));
This is assuming MaterialDao.findMaterialByName(l.getMaterial()) returns a boolean.
2 of 5
54
Generally, this is how you can filter a map by its values:
static <K, V> Map<K, V> filterByValue(Map<K, V> map, Predicate<V> predicate) {
return map.entrySet()
.stream()
.filter(entry -> predicate.test(entry.getValue()))
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
}
Call it like this:
Map<String, Integer> originalMap = new HashMap<>();
originalMap.put("One", 1);
originalMap.put("Two", 2);
originalMap.put("Three", 3);
Map<String, Integer> filteredMap = filterByValue(originalMap, value -> value == 2);
Map<String, Integer> expectedMap = new HashMap<>();
expectedMap.put("Two", 2);
assertEquals(expectedMap, filteredMap);
Baeldung
baeldung.com › home › java › java collections › java map › working with maps using streams
Working With Maps Using Streams | Baeldung
November 10, 2025 - We are interested in finding the ISBN for the book titled “Effective Java.” · Since the book title could not exist in our Map, we want to be able to indicate that there is no associated ISBN for it. We can use an Optional to express that: Let’s assume for this example that we are interested in any key for a book matching that title: Optional<String> optionalIsbn = books.entrySet().stream() .filter(e -> "Effective Java".equals(e.getValue())) .map(Map.Entry::getKey) .findFirst(); assertEquals("978-0134685991", optionalIsbn.get());
Golang library for java 8 like streams
Any such library would need to be type-unsafe, use reflection or code generation to work. It's also not considered very Go-like: see https://github.com/robpike/filter for an implementation of apply / reduce and note the author says that it shouldn't be used because for loops are preferred in Go. More on reddit.com
Proposal for stream: a (casting) filter by class
It can be "simplified" with .filter(Circle.class::isInstance) and .map(Circle.class::cast) ;) More on reddit.com
Java 8 functional alternative to for loop break
The filter() method (from the Stream interface) along with the findFirst() method would solve your need. More on reddit.com
Java Stream Filter Predicate chain está permitindo itens inválidos
Mostra a implementação desse método filtrarporargumento
More on reddit.comMaster Java Streams: filter, map, flatMap | PART 3 | Real ...
Java Streams Explained – map, filter, reduce
28:01
Java Stream Gatherers Explained 🚀 | The Next Evolution of Streams ...
07:12
Stream API In Java 8 | Map | Filter | Sorted | Java Streams | Java ...
15:35
Java 8 Stream API : Streams Filter & Map Operations - YouTube
Java Streams filter and map Example tutorial - YouTube
Mkyong
mkyong.com › home › java8 › java 8 map filter examples
Java 8 Map filter examples | mkyong.com
March 13, 2024 - package com.mkyong.examples; import java.util.HashMap; import java.util.Map; import java.util.stream.Collectors; public class MapFilterExample2 { public static void main(String[] args) { Map<Integer, String> map = new HashMap<>(); map.put(1, "linode"); map.put(2, "heroku"); map.put(3, "aws"); //Map -> Stream -> Filter -> String String result1 = map.values().stream() .filter("linode"::equals) .collect(Collectors.joining()); // linode System.out.println(result1); //Map -> Stream -> Filter -> MAP Map<Integer, String> result2 = map.entrySet().stream() .filter(x -> x.getKey() == 1) .collect(Collect
Medium
medium.com › javarevisited › how-to-use-streams-map-filter-and-collect-methods-in-java-1e13609a318b
How to use map, filter, and collect methods in Java Stream? Example Tutorial | by javinpaul | Javarevisited | Medium
January 25, 2024 - Once we have the Stream of Integer, we can apply maths to find out even numbers. We passed that condition to the filter method. If we needed to filter on String, like select all string which has length > 2, then we would have called filter before map. Before any Java and Spring Developer interview, I always use to read the below resources
How to do in Java
howtodoinjava.com › home › java streams › filter a map by keys and values using java stream
Filter a Map by Keys and Values using Java Stream
May 3, 2024 - public static final class Filters { private Filters() { throw new AssertionError("Cannot be instantiated"); } public static <K, V> Map<K, V> byKey(Map<K, V> map, Predicate<K> predicate) { Objects.requireNonNull(map); Objects.requireNonNull(predicate); return map.entrySet() .stream() .filter(item -> predicate.test(item.getKey())) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue)); } //... }
Java Code Geeks
javacodegeeks.com › home › core java
Filter a Map by Keys and Values using Java Stream - Java Code Geeks
June 14, 2024 - Discover how to filter a Map by keys, values, or both using Java 8’s Stream API with the filter() and collect() methods. These straightforward code snippets demonstrate how to create generic functions for filtering any Map based on its keys ...
BeginnersBook
beginnersbook.com › 2017 › 10 › java-8-filter-a-map-by-keys-and-values
Java 8 – Filter a Map by keys and values
import java.util.Map; import java.util.HashMap; import java.util.stream.Collectors; public class Example { public static void main(String[] args) { Map<Integer, String> hmap = new HashMap<Integer, String>(); hmap.put(11, "Apple"); hmap.put(22, "Orange"); hmap.put(33, "Kiwi"); hmap.put(44, "Banana"); Map<Integer, String> result = hmap.entrySet() .stream() .filter(map -> map.getKey().intValue() <= 22) .collect(Collectors.toMap(map -> map.getKey(), map -> map.getValue())); System.out.println("Result: " + result); } }
WPI Computer Science
web.cs.wpi.edu › ~cs2102 › b15 › Labs › map-and-filter.html
Map, Filter, Lambdas and Java Streams
Java doesn’t provide filter and map directly on lists. Instead, these operations work on Streams, a special kind of data for collections of elements, designed to support maps and filters.
DZone
dzone.com › coding › languages › java 8 map, filter, and collect examples
Java 8 Map, Filter, and Collect Examples - DZone
June 21, 2018 - Similar to map, the filter is also an intermediate operation, which means you can call other Stream methods after calling the filter. The filter() method is also lazy, meaning it will not be evaluated until you call a reduction method, like collect, and it will stop as soon as it reaches the target. If you are not familiar with Stream behavior, I suggest you check out The Ultimate Java 9 Tutorial, which further explains Stream fundamentals in great detail.
How to do in Java
howtodoinjava.com › home › java streams › how to filter a map by list of keys in java
How to Filter a Map by List of Keys in Java
September 16, 2022 - In this Java tutorial, we learned to filter a HashMap by keys or values and collect the matching Entry instances into a submap. We also learned to collect the values in a List after filtering the Map keys against a List using Stream.filter() and forEach() APIs.
Medium
medium.com › spring-boot › java-stream-operations-filter-map-reduce-and-collect-65a1287ac2e0
Java Stream Operations: Filter, Map, Reduce, and Collect | by Piyu Jain | JavaToDev | Medium
May 23, 2023 - For example, you can filter a stream to select specific elements, map them to a different data type, perform calculations on the mapped elements using the reduce operation, and finally collect the results into a collection or a single value using the collect operation. In conclusion, Java streams provide a powerful and expressive way to process collections of data.
Stack Abuse
stackabuse.com › how-to-filter-a-map-by-key-or-value-in-java
How to Filter a Map by Key or Value in Java
October 7, 2023 - In this tutorial, we'll take a look at how to filter a Map by key or value in Java 8, using an enhanced for-loop and the Stream API's `filter()` method, with examples.
MIT
web.mit.edu › 6.031 › www › fa17 › classes › 26-map-filter-reduce
Reading 26: Map, Filter, Reduce
Given a Stream<File> files, we can now write, e.g., files.filter(endsWith(".java")) to obtain a new filtered stream. endsWith is a different kind of beast than our usual functions. It’s a higher-order function, meaning that it’s a function that takes another function as an argument, or returns another function as its result. Higher-order functions are operations on the datatype of functions; in this case, endsWith is a creator of functions. Now let’s use map and filter to recursively traverse the folder tree:
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › stream › Stream.html
Stream (Java Platform SE 8 )
July 21, 2026 - Java™ Platform Standard Ed. 8 ... A sequence of elements supporting sequential and parallel aggregate operations. The following example illustrates an aggregate operation using Stream and IntStream: int sum = widgets.stream() .filter(w -> w.getColor() == RED) .mapToInt(w -> w.getWeight()) .sum(); In this example, widgets is a Collection<Widget>. We create a stream of Widget objects via Collection.stream(), filter it to produce a stream containing only the red widgets, and then transform it into a stream of int values representing the weight of each red widget.
Blogger
javarevisited.blogspot.com › 2018 › 05 › java-8-filter-map-collect-stream-example.html
How to use filter + map + collect + Stream in Java? Example Tutorial
May 22, 2018 - Once we got the Stream of Integer, we can apply maths to find out even numbers, and we passed that condition to the filter method. If we needed to filter on String, like select all string which has length > 2 then we would have called filter before map. That's all about how to use map and filter in Java 8.