有幸参加一次电话面试,让我知道了一个成功的钓鱼人只和一只3000斤的鱼合影,而不是30只100斤的鱼。技术在深,不在广。
ArrayList和LinkedList都是List类型,他们都按照插入元素的顺序保存元素。两者的区别是ArrayList在随机访问时效率较高,插入和移除元素时较慢;LinkedList随机访问时的效率低,插入和移除元素时效率高。
HashSet、TreeSet、LinkedHashSet都是Set类型。Set容器中相同的元素只保留一次。HashSet采用相当复杂的方式存放元素,但是查询速度最快。TreeSet的元素按照升序保存,需要顺序输出时使用。LinkedHashSet采用链表存储元素,元素按照输入的顺序排序。
HashMap与HashSet一样,拥有最快的查询速度;TreeMap存放的元素按照比较结果的升序排列;LinkedHashSet按照插入时的顺序保存元素,同时保留了HashMap的查询速度。
public class Test{
public static Collection fill(Collection<String> collection){
collection.add("dog");
collection.add("cat");
collection.add("rat");
collection.add("dog");
return collection;
}
public static Map fill(Map<String,String> map){
map.put("rat", "Fuzzy");
map.put("cat", "Rags");
map.put("dog", "Bosco");
map.put("dog", "Spot");
return map;
}
public static void main(String[] args){
System.out.println(fill(new ArrayList()));
System.out.println(fill(new LinkedList()));
System.out.println(fill(new HashSet()));
System.out.println(fill(new TreeSet()));
System.out.println(fill(new LinkedHashSet()));
System.out.println(fill(new HashMap()));
System.out.println(fill(new TreeMap()));
System.out.println(fill(new LinkedHashMap()));
}
}
/*
[dog, cat, rat, dog]
[dog, cat, rat, dog]
[dog, cat, rat]
[cat, dog, rat]
[dog, cat, rat]
{dog=Spot, cat=Rags, rat=Fuzzy}
{cat=Rags, dog=Spot, rat=Fuzzy}
{rat=Fuzzy, cat=Rags, dog=Spot}
*/