Friday 21 February 2020

Is Collections.emptyList() singleton?

Collections.emptyList return an immutable singleton list. One advantage of this method is, it is singleton. You can call this method any number of times, but it always return the same instance.
public static List<Integer> getEvenIndexElements(List<Integer> list) {
  if (list == null || list.isEmpty()) {
    return Collections.EMPTY_LIST;
  }

  List<Integer> result = new ArrayList<>();

  for (int i = 0; i < list.size(); i += 2) {
    result.add(list.get(i));
  }

  return result;
}

As you see above snippet, ‘getEvenIndexElements’ method return all the elements at even index (0, 2, 4, ….) when the list is non empty. But when the list is empty it returns  ‘Collections.EMPTY_LIST’.

What if you return new ArrayList, when the list null or empty condition?
if (list == null || list.isEmpty()) {
 return new ArrayList<Integer> ();
}

It unnecessary to create new empty ArrayList object, which is not a an efficient solution. If ‘getEvenIndexElements’ method is called ‘N’ times with empty list, then ‘N’ new ArrayList objects get created.

Is Collections.emptyList is singleton
Yes, Collections.emptyList return an immutable singleton list.

App.java
package com.sample.app;

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

public class App {

 public static List<Integer> getEvenIndexElements(List<Integer> list) {
  if (list == null || list.isEmpty()) {
   return Collections.EMPTY_LIST;
  }

  List<Integer> result = new ArrayList<>();

  for (int i = 0; i < list.size(); i += 2) {
   result.add(list.get(i));
  }

  return result;
 }

 public static void main(String[] args) {
  List emptyList = Collections.EMPTY_LIST;

  boolean flag = true;
  for (int i = 0; i < 100; i++) {
   List result = getEvenIndexElements(null);

   if (emptyList != result) {
    System.out.println("EMPTY_LIST is not singleton");
    flag = false;
    break;
   }

  }

  if (flag) {
   System.out.println("EMPTY_LIST is singleton");
  }
 }

}

Output
EMPTY_LIST is singleton

Is Collections.emptyList is immutable?
Yes, Collections.emptyList return an immutable singleton list. You can’t perform any CRUD operation on immutable list once it is created.

EmptyListTest.java
package com.sample.app;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.Test;

public class EmptyListTest {

 private static final List EMPTY_LIST = Collections.emptyList();

 @Test(expected = UnsupportedOperationException.class)
 public void EMPTY_LIST_add_UnsupportedOperationException() {
  EMPTY_LIST.add(1);
 }

 @Test(expected = UnsupportedOperationException.class)
 public void EMPTY_LIST_remove_UnsupportedOperationException() {
  EMPTY_LIST.remove(1);
 }

 @Test(expected = UnsupportedOperationException.class)
 public void EMPTY_LIST_addAll_UnsupportedOperationException() {
  EMPTY_LIST.addAll(Arrays.asList(1, 2));
 }

 @Test(expected = UnsupportedOperationException.class)
 public void EMPTY_LIST_addAtIndex_UnsupportedOperationException() {
  EMPTY_LIST.add(1, 10);
 }

 @Test(expected = UnsupportedOperationException.class)
 public void EMPTY_LIST_addAllAtIndex_UnsupportedOperationException() {
  EMPTY_LIST.addAll(0, Arrays.asList(1, 2, 3));
 }

}

Why should I use Collections.emptyList?
a.   To avoid NullPointer exceptions
b.   We can reuse immutable objects
c.    We can cache the result of the operation that performed in immutable object and reuse.
d.   Since it is singleton, no duplicate lists created.


You may like

No comments:

Post a Comment