Tuesday, November 1, 2022

Question 20 : Flattening Nested Collections using java 8

Example of a Nested Collection

List<List<String>> nestedList = asList(
  asList("two:one", "two:two", "two:three"), 
  asList("three:one", "three:two", "three:three", "three:four"));

 Flattening the List With flatMap

 import java.util.*;

import java.util.Collections;
import java.util.stream.Collectors;

public class flattenListrMain {
 public static <T> List<T>  flattenListOfListsStream(List<List<T>> list) {
    return list.stream()
 public static void main(String[] args) {
      List<List<String>> nestedList = Arrays.asList(
      Arrays.asList("three:four", "five:six", "saven:nine"), 
      Arrays.asList("three:one", "three:two", "three:three", "three:four"));
      List<String> ls = StringAllUniqueCharMain.flattenListOfListsStream(nestedList);
      System.out.println("flattenList "+ ls);

You may also like

Kubernetes AWS Java Coding Question
Microservices Core Java Python
Spring Framework AI/MLSpring Boot