How to create list of lists in java

Previous
Next

In this posts, we will see how to create a list of lists in java.
You can easily create a list of lists using below syntax

One of the Usecase:

This 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.

Let’s 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 lists

Can you instantiate List as below

No, you can’t.
Let’s understand the reason for it.
Let’s say you have list as below:

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.That’s why it is not allowed to do it.
That’s all about how to create list of lists in java.

Previous
Next

Join Our News Letter – Stay Updated

Subscribe to Awesome Java Content.




Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog