Sunday, 9 August 2015

Lists class

Lists class provides number of utility methods to work with Lists in java. By using Lists class you can perform following.
a.   Create unmodifiable list
b.   Convert string to unmodifiable list
c.    Create new ArrayList
d.   Create new LinkedList
e.   Partition list
f.     Reverse list
g.   Transform list
h.   Create CopyOnWriteArrayList instance

Create unmodifiable list
Lists class provides following methods to create unmodifiable list.

public static <E> List<E> asList(@Nullable E first, E[] rest)

public static <E> List<E> asList(@Nullable E first, @Nullable E second, E[] rest)

import java.util.List;

import com.google.common.collect.Lists;

public class ListsEx {
  public static void main(String args[]) {
    Integer data[] = { 2, 3, 5, 7, 11 };
    List<Integer> myList = Lists.asList(new Integer(1), data);

    System.out.println(myList);
  }
}

Output
[1, 2, 3, 5, 7, 11]

Convert string to unmodifiable list
Following methods take string/CharSequence as input and provide list behavior.

public static ImmutableList<Character> charactersOf(String string)

public static List<Character> charactersOf(CharSequence sequence)

import java.util.List;

import com.google.common.collect.Lists;

public class ListsEx {
  public static void main(String args[]) {
    String str = "Hello World";
    List<Character> myList = Lists.charactersOf(str);

    for (Character data : myList) {
      System.out.println(data);
    }

  }
}


Output
H
e
l
l
o

W
o
r
l
d

Create new ArrayList
Lists class provides following methods to create new ArrayList.

Method
Description
public static <E> ArrayList<E> newArrayList()
Create an empty ArrayList
public static <E> ArrayList<E> newArrayList(E... elements)
Creates ArrayList, contain given elements.
public static <E> ArrayList<E> newArrayList(Iterable<? extends E> elements)
Creates ArrayList, contain given elements.
public static <E> ArrayList<E> newArrayList(Iterator<? extends E> elements)
Creates ArrayList, contain given elements.
public static <E> ArrayList<E> newArrayListWithCapacity(int initialArraySize)
Creates new empty ArrayList with initial size.
public static <E> ArrayList<E> newArrayListWithExpectedSize(int estimatedSize)
Creates new empty ArrayList with estimated size plus an unspecified amount of padding.


import java.util.ArrayList;

import com.google.common.collect.Lists;

public class ListsEx {
  public static void main(String args[]) {
    Integer one = new Integer(1);
    Integer two = new Integer(2);
    Integer three = new Integer(3);
    Integer four = new Integer(4);
    Integer five = new Integer(5);

    ArrayList<Integer> myList = Lists.newArrayList(one, two, three, four,
        five);
    System.out.println(myList);
  }
}

Output
[1, 2, 3, 4, 5]
                 
Create new LinkedList
Lists class provides following methods to create LinkedList.


Method
Description
public static <E> LinkedList<E> newLinkedList()
Create an empty LinkedList.
public static <E> LinkedList<E> newLinkedList(Iterable<? extends E> elements)
Create Linked List with given elements


import java.util.ArrayList;
import java.util.LinkedList;

import com.google.common.collect.Lists;

public class ListsEx {
  public static void main(String args[]) {
    Integer one = new Integer(1);
    Integer two = new Integer(2);
    Integer three = new Integer(3);
    Integer four = new Integer(4);
    Integer five = new Integer(5);

    ArrayList<Integer> elements = Lists.newArrayList(one, two, three, four,
        five);

    LinkedList<Integer> myLinkedList = Lists.newLinkedList(elements);

    System.out.println(myLinkedList);
  }
}

Output
[1, 2, 3, 4, 5]

Partition list
Lists.partition method is used to partition list into sub lists.

public static <T> List<List<T>> partition(List<T> list, int n)        
Above method partition list into sub lists of size n. Last sub list may contain <n elements.

If list of size 10 and n = 2. Then list is divided into 4 sub lists, where subList1, subList2, subList3 contain elements of size 3 and subList4 contains elements of size 1.

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

import com.google.common.collect.Lists;

public class ListsEx {
  public static void main(String args[]) {
    Integer one = new Integer(1);
    Integer two = new Integer(2);
    Integer three = new Integer(3);
    Integer four = new Integer(4);
    Integer five = new Integer(5);
    Integer six = new Integer(6);
    Integer seven = new Integer(7);
    Integer eight = new Integer(8);
    Integer nine = new Integer(9);
    Integer ten = new Integer(10);

    ArrayList<Integer> list = Lists.newArrayList(one, two, three, four,
        five, six, seven, eight, nine, ten);

    List<List<Integer>> subLists = Lists.partition(list, 3);

    System.out.println("Size \t elements");
    for (List<Integer> subList : subLists) {
      System.out.print(subList.size() + "\t");
      for (Integer i : subList)
        System.out.print(i + " ");
      System.out.println();
    }

  }
}

Output
Size    elements
3       1 2 3
3       4 5 6
3       7 8 9
1       10

Reverse list

Lists.reverse method returns reversed view of the given list.

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

import com.google.common.collect.Lists;

public class ListsEx {
  public static void main(String args[]) {
    Integer one = new Integer(1);
    Integer two = new Integer(2);
    Integer three = new Integer(3);
    Integer four = new Integer(4);
    Integer five = new Integer(5);
    Integer six = new Integer(6);
    Integer seven = new Integer(7);
    Integer eight = new Integer(8);
    Integer nine = new Integer(9);
    Integer ten = new Integer(10);

    ArrayList<Integer> list = Lists.newArrayList(one, two, three, four,
        five, six, seven, eight, nine, ten);

    List<Integer> reverseList = Lists.reverse(list);

    System.out.println(reverseList);
  }
}

Output
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Transform list
Lists class provides transform method to transform list using given function.


public static <F, T> List<T> transform( List<F> fromList, Function<? super F, ? extends T> function)

import java.util.ArrayList;

import com.google.common.base.Function;
import com.google.common.collect.Lists;

public class ListsEx {
  public static void main(String args[]) {
    Integer one = new Integer(1);
    Integer two = new Integer(2);
    Integer three = new Integer(3);
    Integer four = new Integer(4);
    Integer five = new Integer(5);
    Integer six = new Integer(6);
    Integer seven = new Integer(7);
    Integer eight = new Integer(8);
    Integer nine = new Integer(9);
    Integer ten = new Integer(10);

    ArrayList<Integer> list = Lists.newArrayList(one, two, three, four,
        five, six, seven, eight, nine, ten);

    Function<Integer, Integer> incrementBy5 = new Function<Integer, Integer>() {

      @Override
      public Integer apply(Integer input) {
        return input + 5;
      }

    };

    System.out.println("Elements in list are : " + list);
    System.out.println("Elements in list after incrementing by 5 "
        + Lists.transform(list, incrementBy5));
  }
}

Output
Elements in list are : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Elements in list after incrementing by 5 [6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
         
Create CopyOnWriteArrayList instance
Lists class provides following methods to create CopyOnWriteArrayList instance.

Method
Description
public static <E> CopyOnWriteArrayList<E> newCopyOnWriteArrayList()
Creates an empty CopyOnWriteArrayList.
public static <E> CopyOnWriteArrayList<E> newCopyOnWriteArrayList(Iterable<? extends E> elements)
Creates a CopyOnWriteArrayList instance containing the given elements.



import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;

import com.google.common.collect.Lists;

public class ListsEx {
  public static void main(String args[]) {
    Integer one = new Integer(1);
    Integer two = new Integer(2);
    Integer three = new Integer(3);
    Integer four = new Integer(4);
    Integer five = new Integer(5);
    Integer six = new Integer(6);
    Integer seven = new Integer(7);
    Integer eight = new Integer(8);
    Integer nine = new Integer(9);
    Integer ten = new Integer(10);

    ArrayList<Integer> list = Lists.newArrayList(one, two, three, four,
        five, six, seven, eight, nine, ten);

    CopyOnWriteArrayList<Integer> copyOnWriteArrayList = Lists
        .newCopyOnWriteArrayList(list);

    System.out.println("Elements in copyOnWriteArrayList are : "
        + copyOnWriteArrayList);

  }
}

Output
Elements in copyOnWriteArrayList are : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]














Prevoius                                                 Next                                                 Home

No comments:

Post a Comment