Anand Kumar
Anand Kumar

Reputation: 161

Linked List of Linked Lists in Java

I would like to know how to create a linked list of linked lists. Also, It would be helpful if the predefined LinkedList (class from Java) and its methods are used for defining and for other add, get, listIterating operations.

Upvotes: 9

Views: 29027

Answers (4)

Autar
Autar

Reputation: 1589

You can put any object in a list, including another list.

LinkedList<LinkedList<YourClass>> list = new LinkedList<LinkedList<YourClass>>();

is a LinkedList of LinkedLists of YourClass objects. It can also be written in a simplified way since Java 7:

LinkedList<LinkedList<YourClass>> list = new LinkedList<>();

Very simple examples of manipulating such a list:

You then need to create each sublist, here adding a single sublist:

list.add(new LinkedList<YourClass>());

Then create the content objects:

list.get(sublistIndex).add(new YourClass());

You can then iterate over it like this (sublists' items are grouped by sublist):

for(LinkedList<YourClass> sublist : list) {
    for(YourClass o : sublist) {
        // your code here
    }
}

If you want to add specific methods to this list of lists, you can create a subclass of LinkedList (or List, or any other List subclasses) or you can create a class with the list of lists as a field and add methods there to manipulate the list.

Upvotes: 25

Moritz Petersen
Moritz Petersen

Reputation: 13047

You can even simplify access to the secondary lists, e.g. using

    final List<List<String>> lists = new LinkedList<List<String>>() {
        @Override
        public List<String> get(final int index) {
            while (index >= size()) {
                add(new LinkedList<>());
            }
            return super.get(index);
        }
    };

This code automatically adds new LinkedLists to the outer list. With this code you can later easily add single values:

lists.get(2).add("Foo");

Upvotes: 1

Arjun K P
Arjun K P

Reputation: 2111

Well i've done this code and i've got it right

          java.util.LinkedList mainlist = new java.util.LinkedList();

          java.util.LinkedList sublist1 = new java.util.LinkedList();
          sublist1.add(object1);
          sublist1.add(object2);
          sublist1.add(object3);

          java.util.LinkedList sublist2=new java.util.LinkedList();
          sublist2.add(1);
          sublist2.add(2);

          mainlist.add(sublist1);
          mainlist.add(sublist2);

          // To retrieve the sublist1 from mainlist...........
          java.util.LinkedList temp = (java.util.LinkedList)mainlist.get(0);

Here variable mainlist is LinkedList of LinkedLists and variable temp contains the value the first list stored i.e sublist1..

Upvotes: 2

SetSlapShot
SetSlapShot

Reputation: 1298

LinkedList<LinkedList<YourClass>> yourList = new LinkedList<LinkedList<YourClass>>();

As the declaration. To add another linked list (to the end by default) you would do

yourList.add(new LinkedList<YourClass>());

To add an element to lets say the second linked list in the series:

yourList.get(1).add(new YourClass());

Upvotes: 0

Related Questions