容器的特点

有幸参加一次电话面试,让我知道了一个成功的钓鱼人只和一只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}
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值