Thursday, January 12, 2023

How to delete multiple elements from a LinkedList in Java? SubList() Example

Hello guys, if you are looking for an easy way to remove a sub set of elements from a LinkedList in Java then you have come to the right place. In the past, I have shown you how to sort a LinkedList in Java and how to create your own linked list implementation in Java and in this article, I am going to introduce you with an interesting method which can be used to delete a portion of LinkedList in Java in shot. Yes, there is a method exists  but not many Java developer knows about it. The method is called subList() and you can use this method with clear method in Java to delete a portion of linked list in one shot.

The subList() method is defined in AbstractList class in Java Collection framework and LinkedList gest this because it extends AbstractSequentialList class which is a subclass of AbstractList class. 

The public List<E> subList(int fromIndex,  int toIndex) method returns a view of the portion of the linked list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) 

By the way, you can use the subList() method for other purposes as well. For example, if you want a portion of LinkedList then you can use it and if you want to create a deep copy of the portion of linked list then you can create a new Collection based upon this list. 




How to remove a portion of LinkedList in Java? Example

Following example demonstrates how to delete many elements of LinkedList using clear() method. You can use this method if you want to truncate or remove all elements from the linked list in Java. 

import java.util.*;

public class Main {
   public static void main(String[] args) {
      LinkedList<String> linkedList = new LinkedList<String>();
      linkedList.add("one");
      linkedList.add("two");
      linkedList.add("three");
      linkedList.add("four");
      linkedList.add("five");
      System.out.println(linkedList);
      linkedList.subList(2, 4).clear();
      System.out.println(linkedList);
   }
}


Result:

The above code sample will produce the following result.

[one, two, three, four, five]                                                                                                        
[one, two, five] 


The key thing to note here is that returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. That's why when we called the clear() method it removes all the elements from the sub list but the result is also shown on the LinkedList object as well because the sub list was backed by it. 

How to delete multiple elements from a LinkedList in Java? Bulk Remove Example


That's all about how to remove a portion of a LinkedList in Java. You can see that its rather easy to delete many elements together in LinkedList. Just use the subList() method and then call the clear afterwards. 

Since the sub list returned by subList() method is backed by original linked list any structural operation like adding or removing a list will affect the original list and that's what happen. 

Btw, you can use this trick to remove a portion of elements from not just LinkedList but any other List as well like ArrayList, Vector etc. 

Other Programming Articles you may like
  • How to check if a given number is prime or not? (solution)
  • How to reverse String in Java without using StringBuffer? (solution)
  • 100+ Data Structure and Algorithms problems for interviews (questions)
  • How to print factorial of a given number in Java? (factorial)
  • 10 Dynamic Programming problems for interviews (dynamic programming)
  • How to find a missing number in a sorted array? (solution)
  • How to find if the given String is a palindrome in Java? (solution)
  • 15 Recursion exercise for Java Programmers (recursion)
  • How to reverse an integer variable in Java? (solution)
  • 75 Programming Questions for Interviews (questions)
  • 10 Free Courses to learn Java Programming (free courses)
  • Write a program to check if a number is a power of two or not? (solution)
  • 10 Free Courses to learn Data Structure and Algorithms (free courses)
  • How do you reverse the word of a sentence in Java? (solution)
  • How do you swap two integers without using a temporary variable? (solution)         
 
Thanks for reading this article so far. If you like this little trick to delete a portion of linked list in Java then please share with your friends and colleagues. If you have any questions or doubt feel free to ask in comments. 

P. S. - If you want to improve your Java Collection framework skills and looking for resources like online courses, tutorials and books then you can also check this list of best Java Collections and Stream courses to start with. It contains online courses to learn Java collection framework in depth. 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.