Flatten List of Lists in Java 8 | flatMap Explained in 8 Easy Ways (Interview Ready)

 Hi All,

Today I have released new video on "Flatten List of Lists in Java 8 | flatMap Explained in 8 Easy Ways (Interview Ready)"


This is frequently asked question in Java Interview.


Please watch full video, share, like and Subscribe youtube channel and press bell icon. So you will get latest video notification.

 

Please click on below Image to view full explanation on below Youtube video .

Flatten List of Lists in Java 8 | flatMap Explained in 8 Easy Ways (Interview Ready) 



 package org.practice.basicprgms;


import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collection;

import java.util.Iterator;

import java.util.List;

import java.util.stream.Collectors;


public class FlattenFlatMapDemo {


public static void main(String[] args) {

List<List<Integer>> lists = Arrays.asList(

Arrays.asList(1,2,3),

Arrays.asList(4,5,6),

Arrays.asList(7,8,9)

);

List<Integer> list = new ArrayList<Integer>();

// 1. Using flatMap() (Most Recommended).

lists.stream()

.flatMap(List::stream)

.forEach(System.out::println);

System.out.println("Using flatMap() (Most Recommended). ");

// 2. Using flatMap() and Collect in another list.

list = lists.stream()

.flatMap(List::stream)

.collect(Collectors.toList());

System.out.println("Using flatMap() and Collect in another list. "+ list);

// 3. Using forEach() + addAll()

list.clear();

lists.stream()

.forEach(list::addAll);

System.out.println("Using forEach() + addAll() "+ list);


// 4. Using reduce() (Interview Favorite 🔥)

list.clear();

list =

lists.stream()

.reduce(

new ArrayList(),

(acc, ls) -> {

acc.addAll(ls);

return acc; }

);

System.out.println("Using reduce() "+ list);


// 5. Using flatMap() + toCollection()

list.clear();

list =

lists.stream()

.flatMap(Collection::stream)

.collect(Collectors.toCollection(ArrayList::new));

System.out.println("Using flatMap() + toCollection() "+ list);

// 6. Using Parallel Stream (Performance Case)

list.clear();

list =

lists.parallelStream()

.flatMap(List::stream)

.collect(Collectors.toList());

System.out.println("Using Parallel Stream (Performance Case) "+ list);


// 7. Using Traditional Loop (No Streams)

list.clear();

for(List<Integer> ls: lists) {

for(int i: ls) {

list.add(i);

}

}

System.out.println("Using Traditional Loop (No Streams) "+ list);

// 8. Using Iterator

list.clear();

Iterator<List<Integer>> iterator = lists.iterator();

while(iterator.hasNext()) {

list.addAll(iterator.next());

}

System.out.println(" Using Iterator "+ list);

}


}


output:

1

2

3

4

5

6

7

8

9

Using flatMap() (Most Recommended).

Using flatMap() and Collect in another list. [1, 2, 3, 4, 5, 6, 7, 8, 9]

Using forEach() + addAll() [1, 2, 3, 4, 5, 6, 7, 8, 9]

Using reduce() [1, 2, 3, 4, 5, 6, 7, 8, 9]

Using flatMap() + toCollection() [1, 2, 3, 4, 5, 6, 7, 8, 9]

Using Parallel Stream (Performance Case) [1, 2, 3, 4, 5, 6, 7, 8, 9]

Using Traditional Loop (No Streams) [1, 2, 3, 4, 5, 6, 7, 8, 9]

Using Iterator [1, 2, 3, 4, 5, 6, 7, 8, 9]

Post a Comment

0 Comments