題目一
You need to store elements in a collection that guarantees that no duplicates are stored and all elements can be access in nature order, which interface provies that capabiliy?
A. java.util.Map
B. java.util.Collection
C. java.util.List
D. java.util.Set
答案 D
題目二
List, Set, Map是否繼承自Collection接口,它們有什么區(qū)別?
List,Set是,Map不是
Set 不允許有重復的元素.且沒有順路 Set取元素時,沒法說取第幾個,只能以Iterator接口取得所有的元素,再逐一遍歷各個元素.
List表示有先后順序的集合并且允許重復
Map與List和Set不同,存儲一對key/value,不能存儲重復的key
題目三
public static void main(){
Map<String,String> map = new HashMap<String,String>();
map.out(String.valueOf(System.currentTimeMillis())+"a",1);
map.out(String.valueOf(System.currentTimeMillis())+"a",2);
map.out(String.valueOf(System.currentTimeMillis())+"a",3);
for(Map.Entry<String,String> entry : map.entrySet()){
System.out.printf(entry.getValue());
}
}
輸出順序是 123順序無法確定. Map 中的鍵是 Set. Set 順序是隨機的.