How to iterate over a 2D list (list of lists) in Java Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a 2D list, the task is to iterate this 2D list in Java. 2D list (list of lists) The 2D list refers to a list of lists, i.e. each row of the list is another list. [ [5, 10], [1], [20, 30, 40] ] Iterate a 2D list: There are two ways of iterating over a list of list in Java. Iterating over the list of lists using loop: Get the 2D list to the iterated We need two for-each loops to iterate the 2D list successfully. In the first for-each loop, each row of the 2D lists will be taken as a separate list for (List list : listOfLists) { } In the second for-each loop, each item of the list in each row will be taken separately for (K item : list) { } Hence we can do any operation on this item. Here we are printing the item. Below is the implementation of the above approach: Java // Java code to demonstrate the concept of // list of lists using loop import java.util.*; public class List_of_list { // Iterate the 2D list using loop // and print each element public static <K> void iterateUsingForEach(List<List<K> > listOfLists) { // Iterate the 2D list // and print each element System.out.println("["); for (List<K> list : listOfLists) { System.out.print(" ["); for (K item : list) { System.out.print(" " + item + ", "); } System.out.println("], "); } System.out.println("]"); } // Driver code public static void main(String[] args) { // List of Lists ArrayList<List<Integer> > listOfLists = new ArrayList<List<Integer> >(); // Create N lists one by one // and append to the list of lists List<Integer> list1 = new ArrayList<Integer>(); list1.add(5); list1.add(10); listOfLists.add(list1); List<Integer> list2 = new ArrayList<Integer>(); list2.add(1); listOfLists.add(list2); List<Integer> list3 = new ArrayList<Integer>(); list3.add(20); list3.add(30); list3.add(40); listOfLists.add(list3); // Iterate the 2D list iterateUsingForEach(listOfLists); } } Output: [ [ 5, 10, ], [ 1, ], [ 20, 30, 40, ], ] Iterating over the list of lists using iterator: Get the 2D list to the iterated We need two iterators to iterate the 2D list successfully. The first iterator will iterate each row of the 2D lists as a separate list Iterator listOfListsIterator = listOfLists.iterator(); Each row of the 2D list can be obtained with the help of next() method of Iterator listOfListsIterator.next(); But the next() method returns the Iterator as an Object's object. Hence we need to cast this returned object into a list. list = (List)listOfListsIterator.next(); The second iterator will iterate each item of the list in each row separately Iterator eachListIterator = list.iterator(); Hence we can do any operation on this item. Here we are printing the item. Below is the implementation of the above approach: Java // Java code to demonstrate the concept of // list of lists using iterator import java.util.*; class List_of_list { // Iterate the 2D list using Iterator // and print each element public static <K> void iterateUsingIterator(List<List<K> > listOfLists) { // Iterator for the 2D list Iterator listOfListsIterator = listOfLists.iterator(); System.out.println("["); while (listOfListsIterator.hasNext()) { // Type cast next() method // to convert from Object to List<K> List<K> list = new ArrayList<K>(); list = (List<K>)listOfListsIterator.next(); // Iterator for list Iterator eachListIterator = list.iterator(); System.out.print(" ["); while (eachListIterator.hasNext()) { System.out.print( " " + eachListIterator.next() + ", "); } System.out.println("], "); } System.out.println("]"); } // Driver code public static void main(String[] args) { // List of Lists ArrayList<List<Integer> > listOfLists = new ArrayList<List<Integer> >(); // Create N lists one by one // and append to the list of lists List<Integer> list1 = new ArrayList<Integer>(); list1.add(5); list1.add(10); listOfLists.add(list1); List<Integer> list2 = new ArrayList<Integer>(); list2.add(1); listOfLists.add(list2); List<Integer> list3 = new ArrayList<Integer>(); list3.add(20); list3.add(30); list3.add(40); listOfLists.add(list3); // Iterate the 2D list iterateUsingIterator(listOfLists); } } Output: [ [ 5, 10, ], [ 1, ], [ 20, 30, 40, ], ] Comment More infoAdvertise with us Next Article Convert an Iterator to a List in Java S SaisampathMahajan Follow Improve Article Tags : Java java-list Traversal Java-Iterator Practice Tags : JavaTraversal Similar Reads Split a List into Two Halves in Java Here we are given a list and the task is to split it into two news lists as one can better perceive from the below illustration as follows: Illustration: Input : list = {1, 2, 3, 4, 5, 6} Output : first = {1, 2, 3}, second = {4, 5, 6} Input : list = {1, 2, 3, 4, 5} Output : first = {1, 2}, second = 6 min read Convert an Iterator to a List in Java Given an Iterator, the task is to convert it into List in Java. Examples: Input: Iterator = {1, 2, 3, 4, 5} Output: {1, 2, 3, 4, 5} Input: Iterator = {'G', 'e', 'e', 'k', 's'} Output: {'G', 'e', 'e', 'k', 's'} Below are the various ways to do so: Naive Approach: Get the Iterator. Create an empty lis 2 min read How to use Iterator in Java? 'Iterator' is an interface which belongs to collection framework. It allows us to traverse the collection, access the data element and remove the data elements of the collection. java.util package has public interface Iterator and contains three methods: boolean hasNext(): It returns true if Iterato 3 min read Enumeration vs Iterator vs ListIterator in Java Enumeration is an interface. It is used in the collection framework in java to retrieve the elements one by one. Enumeration is a legacy interface that is applicable only for legacy classes like Vector, HashTable, Stack, etc. It provides a single direction iteration. By using enumeration, we can per 4 min read List listIterator() Method in Java with Examples This method returns a list iterator over the elements in the mentioned list (in proper sequence), starting at the specified position in the list. Syntax: ListIterator listIterator(int index) Parameters: This method has only argument, i.e, index - index of the first element to be returned from the li 2 min read Difference Between Iterator and Spliterator in Java The Java Iterator interface represents an object capable of iterating through a collection of Java objects, one object at a time. The Iterator interface is one of the oldest mechanisms in Java for iterating collections of objects (although not the oldest â Enumerator predated Iterator). Moreover, an 4 min read Like