Showing posts with label List. Show all posts
Showing posts with label List. Show all posts

Removing null elements from List in Java

In this tutorial lets see how to remove null elements from the given list by using java Collections. Basic we could think iterating through List and to delete all null elements. But by using Collections class it makes easy to remove all null elements. Lets see simple example to delete null elements from list.
Removing null elements from List in Java


public static void main(String[] args) {
 
 final List<Integer> list = new ArrayList<Integer>();
 Collections.addAll(list, 1,2,null,3,4,null,5,6,null,7,8,null,9);
 
 System.out.println("LIST SIZE : "+list.size()+ " - " + list);
 
 list.removeAll(Collections.singleton(null));
 
 System.out.println("LIST SIZE : "+list.size()+ " - " + list);
}



OUTPUT:


LIST SIZE : 13 - [1, 2, null, 3, 4, null, 5, 6, null, 7, 8, null, 9]
LIST SIZE : 9 - [1, 2, 3, 4, 5, 6, 7, 8, 9]


Reverse LinkedList

Reverse Linkedlist using java code

This is one of the interview question which asked in recent interview like need to create a LinkedList and need to reverse those values in same linkedList without using addition list. We can make use of List implementation class LinkedList to achieve this. There are couple of ways we can achieve this in java by using Collection utility function reverse() and without reverse() function also. 
Lets see both the implementation in below examples. First lets see how to reverse LinkedList by using reverse() function. 


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

public class LinkedListReverse {

 public static void main(String[] args) {
  
  List<Integer> list = new LinkedList<Integer>();
  list.add(3);
  list.add(6);
  list.add(7);
  list.add(0);
  list.add(1);
  list.add(4);
  list.add(9);
  list.add(5);
  
  System.out.println("Before Reverse : "+list.toString());
  
  Collections.reverse(list);
  
  System.out.println("After Reverse : "+list.toString());  
 }
}

OUTPUT:


Before Reverse : [3, 6, 7, 0, 1, 4, 9, 5]
After Reverse : [5, 9, 4, 1, 0, 7, 6, 3]

Next we will see how to reverse linkedlist without using reverse() function.


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

public class LinkedListReverse {

 public static void main(String[] args) {
  
  List<Integer> list = new LinkedList<Integer>();
  list.add(3);
  list.add(6);
  list.add(7);
  list.add(0);
  list.add(1);
  list.add(4);
  list.add(9);
  list.add(5);
  
  System.out.println("Before Reverse : "+list.toString());
  
  ListIterator<Integer> itrFirst = list.listIterator();
  ListIterator<Integer> itrLast = list.listIterator();
  
  while (itrLast.hasNext()) {
   itrLast.next();   
  }
  
  for(int i = (list.size()/2)-1;itrFirst.hasNext() && itrLast.hasPrevious() && i>=0;i--){
   int first = itrFirst.next();
   int last = itrLast.previous();
   itrFirst.set(last);
   itrLast.set(first);
  }
  
  System.out.println("After Reverse : "+list.toString());  
 }
}

OUTPUT:


Before Reverse : [3, 6, 7, 0, 1, 4, 9, 5]
After Reverse : [5, 9, 4, 1, 0, 7, 6, 3]


How to convert list to read-only list in Java?

 
By using Collections class we can convert List to read-only List. This can done by using Collections.unmodifiableList(list) function from Collections class. By this we can't add or remove any element from the List. Not only List we can make other Collection Objects like Map, Set also to read-only as like below.

Collections.unmodifiableMap(map);
Collections.unmodifiableSet(set);
Collections.unmodifiableSortedMap(sortedMap);
Collections.unmodifiableSortedSet(sortedSet);

Lets see small example how to convert list to read-only list.


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ReadOnlyList {

 public static void main(String[] args) {
  
  List<String> list = new ArrayList<String>();
  list.add("java");
  list.add("discover");
  list.add("threads");
  list.add("oops");
  list.add("servlet");
  
  System.out.println("LIST : "+list);
  
  // Removing "oops" from from list
  list.remove(3);
  
  System.out.println("LIST : "+list);
    
  // Making list to read-only
  list = Collections.unmodifiableList(list);

  // trying to removing "threds" from list
  list.remove(2);
    
  System.out.println("LIST : "+list);
 }
}


OUTPUT:


LIST : [java, discover, threads, oops, servlet]
LIST : [java, discover, threads, servlet]
Exception in thread "main" java.lang.UnsupportedOperationException


If we seen in above program we have added 5 values in list and we have removed 1 value "oops" from the list. Next we have converted list to read-only list by using unmodifiableList() method. Later when we try to remove "discover" value it throws UnsupportedOperationException because of read-only list. Even we can't add any value to this list.