LinkedList

Obtain sub List from LinkedList example

This is an example of how to obtain a subList of a LinkedList, that is a view to a specified range of the list. Getting a sublist of a LinkedList implies that you should:

  • Create a new LinkedList.
  • Populate the list with elements, with add(E e) API method of LinkedList.
  • Invoke the subList(int fromIndex, int toIndex) API method of LinkedList. It returns a List that is a view of the portion of this list between the specified fromIndex, that is inclusive, and toIndex, that is exclusive. The returned list is backed by the original LinkedList, so changes to subList are also reflected to the original LinkedList. To check if this is true, we can remove an element from the sub list and then check if it exists in the original LinkedList.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;
 
import java.util.LinkedList;
import java.util.List;
 
public class SubListLinkedList {
 
  public static void main(String[] args) {
 
    // Create a LinkedList and populate it with elements
    LinkedList linkedList = new LinkedList();
    linkedList.add("element_1");
    linkedList.add("element_2");
    linkedList.add("element_3");
    linkedList.add("element_4");
    linkedList.add("element_5");
 
    System.out.println("LinkedList contains : " + linkedList);

    // List subList(int start, int end) method returns portion of list containing element from start index inclusive to end index exclusive
    List subList = linkedList.subList(1,3);
    System.out.println("subList contains : " + subList);
 
    /*

Sub List returned is backed by original LinkedList. So any

changes made to sub list will also be reflected to the 

original LinkedList. We will test that by removing an element 

from the sub list and check that it is removed from the original 

LinkedList also
    */
    boolean removed = subList.remove("element_2");
    System.out.println("element_2 removed from subList : " + removed + ", subList now contains : " + subList);

    System.out.println("LinkedList now contains : " + linkedList);
  }
}

Output:

LinkedList contains : [element_1, element_2, element_3, element_4, element_5]
subList contains : [element_2, element_3]
element_2 removed from subList : true, subList now contains : [element_3]
LinkedList now contains : [element_1, element_3, element_4, element_5]

 
This was an example of how to obtain a subList of a LinkedList in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button