Home » Java programming language

How to remove a SubList from a List in Java?

Example of SubList(): Here, we are going to learn how to remove a sub list from a given list (LinkedList) in Java?
Submitted by Preeti Jain, on July 18, 2019

Removing SubList from a List

Suppose, we have a list of few elements like this,

    list = [10,20,30,40,50]

From the list, we have to delete a sub list between sourcing_index (inclusive) and destinating_index (exclusive).

This can be done by two ways,

  1. By Using subList(int sourcing_index, int destinating_index) and clear() method of interface.
  2. By Using removeRange(int sourcing_index, int destinating_index) method of List interface.

subList(int sourcing_index, int destinating_index) and clear() of List

This method is available in List interface.

Syntax:

    subList(int sourcing_index, int destinating_index);

We pass two parameters in the method of the List,

  • Sourcing_index is the selection of the starting point of the subList.
  • Destinating_index is the selection of the ending point of the subList.

Example:

import java.util.*;

public class DeleteSublist {
    public static void main(String[] args) {
        LinkedList list = new LinkedList();

        // use add() method to add elements in the list 
        list.add(10);
        list.add(20);
        list.add(30);
        list.add(40);
        list.add(50);

        //  Current list Output
        System.out.println("The Current list is:" + list);

        // We will delete sublist by using subList(int,int) 
        // and clear() method of List.
        list.subList(2, 4).clear();

        //  New list Output after implementation of 
        // subList() and clear() method.
        System.out.println("The New list is:" + list);
    }
}

Output

E:\Programs>javac DeleteSublist.java

E:\Programs>java DeleteSublist
The Current list is:[10, 20, 30, 40, 50]
The New list is:[10, 20, 50]

removeRange(int sourcing_index, int destinating_index)

This method is available in List interface.

Syntax:

    removeRange(int sourcing_index, int destinating_index);

We pass two parameters in the method of the List,

  • Sourcing_index is the selection of the starting point of the subList.
  • Destinating_index is the selection of the ending point of the subList.

Example:

import java.util.*;

public class DeleteSublist extends LinkedList {
    public static void main(String[] args) {
        DeleteSublist list = new DeleteSublist();

        // use add() method to add elements in the list 
        list.add(10);
        list.add(20);
        list.add(30);
        list.add(40);
        list.add(50);

        //  Current list Output
        System.out.println("The Current list is:" + list);

        // We will delete sublist by using removeRange(int,int) 
        // method of List.
        list.removeRange(2, 4);

        //  New list Output after implementation of 
        // removeRange(int,int) method.
        System.out.println("The New list is:" + list);
    }
}

Output

E:\Programs>javac DeleteSublist.java

E:\Programs>java DeleteSublist
The Current list is:[10, 20, 30, 40, 50]
The New list is:[10, 20, 50]


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.