1. Overview
In this tutorial, We'll learn how to create the List of Lists and iterate them in java and the newer java 8 version with simple example programs.
Read more on How to iterate List in Java 8 using forEach?
This is like List is holding another list of strings or list of integers or in some cases it can be user-defined custom objects.
Let us look at each type of object creation and loop through them in java.
2. Java - Create List of Lists With Example Program
In the below examples we are creating the List of List of strings and List of List of Integers.
Simple high level of creating List of Lists.
With String objects
List<List<String>> listOfLists = new ArrayList<>();
With Integer Objects
List<List<Integer>> listOfLists = new ArrayList<>();
2.1 List of List of Strings in Java
The below example is based on a String of Lists of List.
Example 1
package com.javaprogramto.java8.listoflists; import java.util.ArrayList; import java.util.List; public class ListOfListsStrings { public static void main(String[] args) { List<List<String>> listOfLists = new ArrayList<>(); List<String> innerList1 = new ArrayList<>(); innerList1.add("A"); innerList1.add("B"); innerList1.add("C"); List<String> innerList2 = new ArrayList<>(); innerList2.add("D"); innerList2.add("E"); innerList2.add("F"); listOfLists.add(innerList1); listOfLists.add(innerList2); System.out.println("List of lists - "+listOfLists); } }
Output
List of lists - [[A, B, C], [D, E, F]]
2.2 List of List of Integers in Java
The below example is based on the Integer of Lists of List.
Example 2
package com.javaprogramto.java8.listoflists; import java.util.ArrayList; import java.util.List; public class ListOfListsIntegers { public static void main(String[] args) { List<List<Integer>> listOfLists = new ArrayList<>(); List<Integer> innerList1 = new ArrayList<>(); innerList1.add(1); innerList1.add(2); innerList1.add(3); List<Integer> innerList2 = new ArrayList<>(); innerList2.add(4); innerList2.add(5); innerList2.add(6); listOfLists.add(innerList1); listOfLists.add(innerList2); System.out.println("Integer List of lists - "+listOfLists); } }
Output
Integer List of lists - [[1, 2, 3], [4, 5, 6]]
Read more on How to convert List to Map in Java 8?
3. Java - Iterating List of Lists With Example Program
Iterating nested lists can be done in 3 ways in java
A) Using advanced nested loops
B) Java 8 forEach()
C) Java 8 flatMap()
3.1 Itertate Using Advanced nested for loop
Use the loop inside the loop on the list of lists.
Example 3
for(List<String> list : listOfLists) { for(String letter : list) { System.out.println(letter); } } for (List<Integer> list : listOfLists) { for (Integer i : list) { System.out.println(i); } }
Output
A B C D E F 1 2 3 4 5 6
3.2 Itertate Using Java 8 forEach()
Use forEach() method from Java 8 and later versions API.
Example 4
listOfLists.forEach( (List<String> innerList) -> innerList.forEach((String item) -> System.out.println(item)) ); listOfLists.forEach( (List<Integer> innerList) -> innerList.forEach((Integer item) -> System.out.println(item) ) );
This code produces the same output as before.
3.3 Itertate Using Java 8 flatMap()
The new stream API flatMap() method does merge inner lists into a single list. That is called flattening the collections.
Example 5
List<Integer> ints = listOfLists.stream() .flatMap(list -> list.stream()) .collect(Collectors.toList()); System.out.println("flat map ints - "+ints); List<String> strings = listOfLists.stream() .flatMap(list -> list.stream()) .collect(Collectors.toList()); System.out.println("flat map strings - "+strings);
Output
flat map strings - [A, B, C, D, E, F] flat map ints - [1, 2, 3, 4, 5, 6]
4. Conclusion
In this article, We've seen how to create and iterate the list of lists in java with examples.
No comments:
Post a Comment
Please do not add any spam links in the comments section.