List有序key和value都能重复
Set 无序(除treeSet) key和value都不能重复
Map无序(除treeMap) key 必须唯一 value 可以重复//允许重复,并且是有序,允许为空
List list = new ArrayList();
list.add(123);
list.add(123);
list.add("");
for(int i=0; i<list.size(); i++){
System.out.println(list.get(i));
}
LinkedList ll = new LinkedList();
ll.add(456);
ll.add(789);
ll.add("");
for(int i=0; i<ll.size(); i++){
System.out.println(ll.get(i));
}
Vector v = new Vector();
v.add(456);
v.add(789);
v.add("");
for(int i=0; i<v.size(); i++){
System.out.println(v.get(i));
}
//不允许重复,并且无序,允许为空
Set set = new HashSet();
set.add("asdfasdf");
set.add("asdfasdf");
set.add("asdfasdf");
set.add("");
Iterator iterator =set.iterator();
while(iterator.hasNext()){
System.out.println("-->"+iterator.next().toString());
}
Set treeSet = new TreeSet();
treeSet.add("asdfasdf");
treeSet.add("asdfasdf");
treeSet.add("asdfasdf");
treeSet.add("");
iterator =treeSet.iterator();
while(iterator.hasNext()){
System.out.println("-->"+iterator.next().toString());
}
//无序,key 必须唯一 value 可以重复
Map map = new HashMap();
map.put(1, "asdfasdf");
map.put(1, "asdfasdf");
map.put(1, "asdfasdf");
map.put(1, "asdfasdf");
map.put("", "");
iterator = map.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry entry = (Map.Entry) iterator.next();
System.out.println(entry.getKey()+"="+entry.getValue());
}
map = new TreeMap();
User user = new User();
user.setId(1);
user.setName("Java");
map.put(1, user);
map.put(1, user);
map.put(1, user);
map.put(1, user);
iterator = map.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry entry = (Map.Entry) iterator.next();
user = (User)entry.getValue();
System.out.println(entry.getKey()+"="+user.getName());
}
来源http://www.secn.com.cn