Table of Contents
In this posts, we will see how to create a list of lists in java. List<ArrayList<String>> listOfLists = new ArrayList<ArrayList<String>>(); or ArrayList<ArrayList<String>> listOfLists = new ArrayList<ArrayList<String>>(); One of the UsecaseThis is generally useful when you are trying to read a CSV file and then you need to handle list of lists to get it in memory, perform some processing and write back to another CSV file. Lets take a simple example for list of lists.
When you run above program, you will get below output. Delhi Mumbai Beijing Shanghai Question on list of listsCan you instantiate List as below
Lets understand the reason for it.
Now suppose you could assign that to
Now, you should be able to do this:
But that means you have just added a LinkedList to a list whose elements are supposed to be ArrayList only.Thats why it is not allowed to do it. import_contacts You may also like:How to remove element from Arraylist in java while iteratingPrint HashMap in JavaPrint LinkedList in javaJava Set to ArrayPrint ArrayList in JavaHow to Deep Copy Arraylist in JavaInitialize ArrayList with values in JavaPriorityQueue in Java 82d Arraylist java exampleHow HashMap works in java
|