How to Initialize List in declaration?

UPDATED: 29 October 2015
Following excerpt show How you can Initialize List in its declaration.

Source code
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author javaQuery
 * @date 29th October, 2015 Initialize List in its declaration.
 */
public class InitializeListExample {

    public static void main(String[] args) {
        /* Declare and Initialize List of Integer */
        List<Integer> listIntegers = new ArrayList<Integer>(Arrays.asList(10, 20, 30));

        System.out.println("Print value of List<Integer>");
        /* Print value of List */
        for (Integer integer : listIntegers) {
            System.out.println(integer);
        }

        /* Declare and Initialize List of String */
        List<String> listStrings = new ArrayList<String>(Arrays.asList("Chirag", "Yogita", "Vicky", "Heer"));

        System.out.println("Print value of List<String>");
        /* Print value of List */
        for (String string : listStrings) {
            System.out.println(string);
        }
    }
}

Output
Print value of List<Integer>
10
20
30
Print value of List<String>
Chirag
Yogita
Vicky
Heer

Reference
How to Initialize Set in declaration?

0 comments :