How to Create and Iterate list of lists in Java with Examples

In this post, I will be sharing how to create and iterate the list of lists in Java with simple examples.

Read Also:  5 ways: How to iterate ArrayList                   

1. Create a list of lists in Java with Example

Given below is the simplest way to create a list of lists in Java:

For String:

List<List<String>> listOfLists = new ArrayList<>(); 

That's it. The above line will create a list of lists of String.

For Integer:

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

The above line will create a list of lists of Integer.

Read Also:  Convert LinkedList to ArrayList


1.1 Example of String and Integer list of lists in Java:


1. Given below is an example of String list of lists:

import java.util.*;

public class JavaHungry
{
    public static void main(String[] args)
    {
        // Create listOfLists in Java
        List<List<String>> listOfLists = new ArrayList<>();
        
        // Creating innerList 
        List<String> innerList = new ArrayList<>();
        
        // Adding elements to innerList
        innerList.add("Boston");
        innerList.add("San Francisco");
        innerList.add("New York");
        
        // Adding innerList to listOfLists
        listOfLists.add(innerList);
        
        // Creating another inner list 
        List<String> innerList2 = new ArrayList<>();
        
        // Adding elements to innerList2
        innerList2.add("LinkedIn");
        innerList2.add("Twitter");
        innerList2.add("Facebook");
        
        // Adding innerList2 to listOfLists
        listOfLists.add(innerList2);        

        // Printing listOfLists elements
        System.out.println(listOfLists);
    }
}

Output:
[[Boston, San Francisco, New York], [LinkedIn, Twitter, Facebook]]


2. Given below is an example of Integer list of lists:

import java.util.*;

public class JavaHungry
{
    public static void main(String[] args)
    {
        // Create listOfLists in Java
        List<List<Integer>> listOfLists = new ArrayList<>();
        
        // Creating innerList 
        List<Integer> innerList = new ArrayList<>();
        
        // Adding elements to innerList
        innerList.add(1);
        innerList.add(2);
        innerList.add(3);
        
        // Adding innerList to listOfLists
        listOfLists.add(innerList);
        
        // Creating another inner list 
        List<Integer> innerList2 = new ArrayList<>();
        
        // Adding elements to innerList2
        innerList2.add(100);
        innerList2.add(101);
        innerList2.add(102);
        
        // Adding innerList2 to listOfLists
        listOfLists.add(innerList2);        

        // Printing listOfLists elements
        System.out.println(listOfLists);
    }
}

Output:
[[1, 2, 3], [100, 101, 102]]

2. Iterate list of lists in Java with Example

There are two ways through which you can iterate the list of lists in Java.

a. Using nested advance for loop
b. Using forEach (after Java 8)

2.1 Iterate using nested advance for loop


        /* Iterating listOfLists elements
        using advance for loops */
        for(List innerlist : listOfLists)
        {
            for(Object i : innerlist)
            {
                System.out.print( i+ " ");
            }
            System.out.println("");
        }

Output:
1 2 3 
100 101 102

2.2 Iterate using Java8 forEach


        /* Iterating listOfLists elements
        using Java8 forEach*/
        listOfLists.forEach((list) -> {
            list.forEach((num) -> System.out.println(num));
        });

Output:
1
2
3
100
101
102

That's all for today, please mention in comments in case you have any questions related to create and iterate the list of lists in java with examples.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry