标签:java集合初始化
import java.util.*;
/**
* Created by frank on 2015/12/28.
*/
public class Test {
public static void main(String[] args) {
//List
List<String> members1 = new ArrayList<String>() {
{
add("frank");
add("arvin");
add("jennifer");
add("jerry");
add("cartier");
add("tony");
}
};
for (String member:members1) {
System.out.println(member);
}
//Set
Set<String> members2 = new HashSet<String>() {
{
add("frank");
add("arvin");
add("jennifer");
add("jerry");
add("cartier");
add("tony");
}
};
for (String member:members2) {
System.out.println(member);
}
//Map
Map<String, String> members3 = new HashMap<String, String>() {
{
put("frank", "M");
put("arvin", "M");
put("jennifer", "F");
put("jerry", "M");
put("cartier", "F");
put("tony", "M");
}
};
for (String name:members3.keySet()) {
System.out.printf("%s,%s\n", name, members3.get(name));
}
}
}本文出自 “Frank” 博客,请务必保留此出处http://zengfanhong.blog.51cto.com/8894077/1729334
标签:java集合初始化
原文地址:http://zengfanhong.blog.51cto.com/8894077/1729334