You can put any object in the list, including another list.
LinkedList<LinkedList<YourClass>> list = new LinkedList<LinkedList<YourClass>>();
is a LinkedList
object of a LinkedList
object of YourClass
. It can also be written in a simplified way with Java 7 :
LinkedList<LinkedList<YourClass>> list = new LinkedList<>();
Very simple examples of manipulating such a list:
Then you need to create each sublister by adding one subscription:
list.add(new LinkedList<YourClass>());
Then create content objects:
list.get(sublistIndex).add(new YourClass());
Then you can iterate over it this way (subscription items are grouped by subscription):
for(LinkedList<YourClass> sublist : list) { for(YourClass o : sublist) {
If you want to add specific methods to this list of lists, you can create a subclass from LinkedList
(or List
or any other subclasses of List
), or you can create a class with a list of lists as a field and add methods to manage the list.
Autar source share