Java LinkedList subList() Method with Examples

In the previous article, we have discussed about Java LinkedList isEmpty() Method with Examples

In this article we are going to see the use of Java LinkedList  subList() method along with suitable examples.

Java LinkedList subList() Method with Examples

This java.util.LinkedList.subList() method is used to extract a part of the LinkedList.

It returns all the elements of the specified range within the LinkedList.

If the from Index(start index) or  to Index(end index) are greater than or less than the size of the LinkedList then it shows IndexOutOfBoundException.

For example:

Let, subList(2,7) then it returns all the elements from index 2 to index 6

Syntax:

LinkedListName.subList(int fromIndex, int toIndex)

Where,

  • LinkedListName refers to the name of your LinkedList.
  • fromIndex refers to the start index of the LinkedList from which you want the elements
  • toIndex refers to theend index of the LinkedList upto which you want the elements

Let’s see different examples to understand it more clearly.

Example-1: Java LinkedList subList() Method – Example with String Type LinkedList

Approach:

  • Create a new LinkedList of type String.
  • Add string elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Then prints a new sublist from the LinkedList using subList() method.
  • Print the new LinkedList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of string datatype
        LinkedList<String> l = new LinkedList<String>();
        // Adding some elements to the LinkedList
        l.add("apple");
        l.add("banana");
        l.add("orange");
        l.add("grapes");
        l.add("guava");
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // Prints a new sublist of the previous LinkedList
        System.out.println("The elements of subList are: "+l.subList(2, 4));
    }
}

Output:

The elements of LinkedList are: [apple, banana, orange, grapes, guava]
The elements of subList are: [orange, grapes]

Example-2: Java LinkedList subList() Method – Example with Integer Type LinkedList

Approach:

  • Create a new LinkedList of type Integer.
  • Add integer elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Then prints a new sublist from the LinkedList using subList() method.
  • Print the new LinkedList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {     
        // Create a LinkedList of Integer datatype
        LinkedList<Integer> l = new LinkedList<Integer>();
        // Adding some elements to the LinkedList
        l.add(2);
        l.add(12);
        l.add(22);
        l.add(32);
        l.add(42);
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // Prints a new sublist of the previous LinkedList
        System.out.println("The elements of subList are: "+l.subList(2, 4));
    }
}
Output:

The elements of LinkedList are: [2, 12, 22, 32, 42]
The elements of subList are: [22, 32]

Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.

Related Java Programs: