Java LinkedList subList - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Friday, December 2, 2016

Java LinkedList subList

LinkedList is doubly-linked list implementation of the List and Deque interfaces.

How to get sub list from Java LinkedList?

Java LinkedList subList method return portion of the list between the specified indexes from original list. The returned list is backed by original list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations. The semantics of the list returned by this method become undefined if the backing list (i.e., original list) is structurally modified in any way other than via the returned list. The structurally modification of original list change the actual modCount, when we try to work on sub list it will throw a ConcurrentModificationException.

Below is the program of LinkedList subList example.

package com.walking.techie;

import java.util.LinkedList;
import java.util.List;

public class LinkedListSubListDemo {

 public static void main(String[] args) {
  List<Integer> numbers = new LinkedList<Integer>();

  // add numbers from 1 to 10 in LinkedList
  for (int i = 1; i <= 10; i++) {
   numbers.add(i);
  }
  System.out.println("Original numbers Linked list " + numbers);
  // sublist between index 4 to 8
  List<Integer> subList = numbers.subList(4, 8);
  System.out.println("sub list from numbers list " + subList);

  // add 11 to subList, it will not change modCount
  subList.add(11);
  System.out.println("Original numbers linked list after adding 11 to subList " + numbers);
  System.out.println("sub list after adding 11 to subList " + subList);
  // Replaces the element at the 1th position in subList list with the 20.
  subList.set(1, 20);
  System.out.println("numbers list after set operation " + numbers);
  System.out.println("sub list after set operation " + subList);
  // add 11 to numbers list, it will change modCount
  numbers.add(12);
  System.out.println("numbers list after add operation " + numbers); // this line is fine
  System.out.println("sublist after adding 12 in numbers list " +subList); // this line will throw ConcurrentModificationException
 }
}

Output of above program is shown below:

Original numbers Linked list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
sub list from numbers list [5, 6, 7, 8]
Original numbers linked list after adding 11 to subList [1, 2, 3, 4, 5, 6, 7, 8, 11, 9, 10]
sub list after adding 11 to subList [5, 6, 7, 8, 11]
numbers list after set operation [1, 2, 3, 4, 5, 20, 7, 8, 11, 9, 10]
sub list after set operation [5, 20, 7, 8, 11]
numbers list after add operation [1, 2, 3, 4, 5, 20, 7, 8, 11, 9, 10, 12]
Exception in thread "main" java.util.ConcurrentModificationException
 at java.util.SubList.checkForComodification(AbstractList.java:769)
 at java.util.SubList.listIterator(AbstractList.java:695)
 at java.util.AbstractList.listIterator(AbstractList.java:299)
 at java.util.SubList.iterator(AbstractList.java:691)
 at java.util.AbstractCollection.toString(AbstractCollection.java:454)
 at java.lang.String.valueOf(String.java:2994)
 at java.lang.StringBuilder.append(StringBuilder.java:131)
 at com.walking.techie.LinkedListSubListDemo.main(LinkedListSubListDemo.java:31)

No comments :

Post a Comment