When you use map operations in Java Stream
resulting stream is obtained by applying the given function to all the elements of this stream. Java Stream API also provides a flatMap()
method that apart from applying the given function to all the elements of this stream, flattens the resulting elements too so that all the nested elements are at the same level.
flatMap is an intermediate operation.
flatMap method signature
<R> Stream<R> flatMap(Function<? super T,? extends Stream<? extends R>> mapper)
Here mapper is a non-interfering, stateless function applied to each element of the stream.
R is the element type of the new stream.
flatMap Java examples
Let’s try to understand with few examples how flatMap() flattens the structure and how it helps.
Suppose there is an ArrayList that contains ArrayLists in turn and you want to count the total number of elements in the list. If you use map function then you will get the number of elements as 2 because map function will get each nested list as element of the stream.
public class FlatMapExample { public static void main(String[] args) { List<List<String>> list = Arrays.asList(Arrays.asList("a", "b"), Arrays.asList("c", "d")); long noOfElements = list.stream().map(a -> a.stream()).count(); System.out.println("Number of elements- "+ noOfElements); } }Output
Number of elements- 2
When you use flatMap function, structure is flattened so that the number of elements is counted properly.
public class FlatMapExample { public static void main(String[] args) { List<List<String>> list = Arrays.asList(Arrays.asList("a", "b"), Arrays.asList("c", "d")); long noOfElements = list.stream().flatMap(a -> a.stream()).count(); System.out.println("Number of elements- "+ noOfElements); } }Output
Number of elements- 4
If you want to display elements in nested ArrayLists in uppercase using map function returns List<Stream<String>>
public class FlatMapExample { public static void main(String[] args) { List<List<String>> list = Arrays.asList(Arrays.asList("a", "b"), Arrays.asList("c", "d")); List<Stream<String>> resultlist = list.stream() .map(a -> a.stream() .map(String::toUpperCase)) .collect(Collectors.toList()); resultlist.forEach(a -> a.forEach(System.out::println)); } }
If you use flatMap in such scenario both lists are flattened so the return value is List
public class FlatMapExample { public static void main(String[] args) { List<List<String>> list = Arrays.asList(Arrays.asList("a", "b"), Arrays.asList("c", "d")); List<String> resultlist = list.stream() .flatMap(a -> a.stream() .map(String::toUpperCase)) .collect(Collectors.toList()); resultlist.forEach(System.out::println); } }Output
A B C D
Here is another example where we have a List of lists. There is a class Order which has a field items of type List. Now you want to display all the items in all the orders.
public class Order { private String orderId; private List<String> items; public String getOrderId() { return orderId; } public void setOrderId(String orderId) { this.orderId = orderId; } public List<String> getItems() { return items; } public void setItems(List<String> items) { this.items = items; } }
public class FlatMapExample { public static void main(String[] args) { // Create list of orders List<Order> listOfOrders = new ArrayList<Order>(); Order order = new Order(); order.setOrderId("1"); order.setItems(Arrays.asList("Book", "Shoes", "Watch")); listOfOrders.add(order); order = new Order(); order.setOrderId("2"); order.setItems(Arrays.asList("Mobile", "Book")); listOfOrders.add(order); List<String> listOfItems = listOfOrders.stream() .flatMap(o -> o.getItems() .stream()) .collect(Collectors.toList()); listOfItems.forEach(System.out::println); } }Output
Book Shoes Watch Mobile Book
flatMap for primitive type stream
There are also flatMap() variants to be used for getting primitive type streams in Java.
- flatMapToInt(Function<? super T,? extends IntStream> mapper)- Returns an IntStream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.
- flatMapToLong(Function<? super T,? extends LongStream> mapper)- Returns a LongStream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.
- flatMapToDouble(Function<? super T,? extends DoubleStream> mapper)- Returns a DoubleStream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.
flatMapToInt Java example
Getting an IntStream by flattening a two dimensional array.
int[][] numbers = {{7,8}, {4,5}, {3,4}}; IntStream iStream = Stream.of(numbers).flatMapToInt(n -> Arrays.stream(n)); iStream.forEach(System.out::println);Output
7 8 4 5 3 4
That's all for the topic flatMap() in Java Stream. If something is missing or you have something to share about the topic please write a comment.
You may also like
No comments:
Post a Comment