Open In App

How to Get Sublist of LinkedList in Java?

Improve
Improve
Like Article
Like
Save
Share
Report

Linked List is a part of the Collection framework present in java.util package. This class is an implementation of the LinkedList data structure which is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. 

Given a list of elements present in a LinkedList, we need to find the elements of a sublist of the given range.

Example :

The elements of the  LinkedList are: [3, 5, 2, 1, 7, 8]

Enter the start and end of the required sublist: 
      start position -> 1
      end position   -> 4
The required SubList is: [5, 2, 1]

where start position is inclusive and the end position is exclusive

Approach:  Using default subList() method present in LinkedList class of util package.

This one is quite simple and pretty straightforward. We basically use java.util.LinkedList.subList() .

Syntax:

public List subList(int fromIndex, int toIndex)

Parameters: This method takes the following argument as a parameter.

  • fromIndex – low endpoint (inclusive) of the subList
  • toIndex – high endpoint (exclusive) of the subList

Returns Value: This method returns a view of the specified range within this list.

Algorithm :

  • Enter the elements in the LinkedList or procure the LinkedList.
  • Enter the starting of the range(inclusive, 0 based) of the subList that you want to find.
  • Enter the ending of the range(exclusive, 0 based).
  • Use the start and end as parameters of the subList() method and assign it to a new list to store this sublist.

Example 1:

Java




// Java program to get Sublist of LinkedList
  
import java.util.LinkedList;
import java.util.List;
  
public class subLinkedList {
    public static void main(String[] args)
    {
        LinkedList<String> list = new LinkedList<String>();
  
        // adding elements
        list.add("apple");
        list.add("mango");
        list.add("peach");
        list.add("guava");
        list.add("banana");
        list.add("lichi");
  
        // printing initial elements
        System.out.println(
            "The elements of the  LinkedList are: " + list);
  
        System.out.println(
            "Enter the start and end of the required sublist: ");
  
        // entering start and end indices
        int start = 2, end = 5;
  
        List sublist = list.subList(start, end);
        System.out.println("The required SubList is: "
                           + sublist);
    }
}


Output

The elements of the  LinkedList are: [apple, mango, peach, guava, banana, lichi]
Enter the start and end of the required sublist: 
The required SubList is: [peach, guava, banana]

Example 2: To get the sublist from the Linked List of LinkedLists.

Java




// Java program to get the sublist from
// Linked List of Linked lists
  
import java.util.LinkedList;
import java.util.List;
  
public class subLinkedList {
    public static void main(String[] args)
    {
        // creating linkedlist of linkedlists
        LinkedList<LinkedList<Integer> > list
            = new LinkedList<>();
  
        // creating lists
        LinkedList<Integer> list1 = new LinkedList<>();
        list1.add(8);
        list1.add(0);
  
        LinkedList<Integer> list2 = new LinkedList<>();
        list2.add(10);
        list2.add(4);
        list2.add(3);
        list2.add(5);
  
        LinkedList<Integer> list3 = new LinkedList<>();
        list3.add(1);
        list3.add(2);
        list3.add(9);
  
        // adding linkedlists to main linkedlist
        list.add(list1);
        list.add(list2);
        list.add(list3);
  
        // printing initial lists
        System.out.println(
            "The elements of the  LinkedList are: " + list);
  
        System.out.println(
            "Enter the start and end of the required sublists: ");
  
        // entering start and end indices
        int start = 1, end = 3;
  
        List sublist = list.subList(start, end);
  
        System.out.println("The required SubList is: "
                           + sublist);
    }
}


Output

The elements of the  LinkedList are: [[8, 0], [10, 4, 3, 5], [1, 2, 9]]
Enter the start and end of the required sublists: 
The required SubList is: [[10, 4, 3, 5], [1, 2, 9]]


Last Updated : 28 Dec, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads