Java中List与Map初始化的一些写法
// InitCollections.java - sample of init collect package com.util; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; /** * */ public class InitCollections { /** * @param args */ public static void main(final String[] args) { // add items into list with add method final List<Object> list = new ArrayList<Object>() { { add("obj1"); add("obj2"); add("objN"); } }; for (final Object item : list) { System.out.println(item); } System.out.println(); // add items into list with arrays final ArrayList<String> listFromArray = new ArrayList(Arrays.asList("Ryan", "Julie", "Bob")); for (final Object item : listFromArray) { System.out.println(item); } System.out.println(); // add values into HashMap when create final Map<String, Object> map = new HashMap<String, Object>() { { put("key1", "value1"); put("key2", "value2"); put("keyN", "valueN"); } }; for (final String key : map.keySet()) { System.out.println("key=" + key + " value=" + map.get(key)); } System.out.println(); final Set<String> set = new HashSet<String>() { { add("setString1"); add("setString2"); add("setString3"); add("setString1"); } }; for (final String item : set) { System.out.println("item=" + item); } } }