Java Collection Framework 学习笔记

1. Use Iterator instead of the for-each construct when you need to:

a. Remove the current element. The for-each construct hides the iterator, so you cannot call remove. Therefore, the for-each construct is not usable for filtering.
b. Iterate over multiple collections in parallel.

for (Iterator<String> i = c.iterator(); i.hasNext(); )
      if (i.next().length() == 4)
        i.remove();

 

 2. bulk operation

a. removeAll()

c.removeAll(Collections.singleton(e));

More specifically, suppose you want to remove all of the null elements from a Collection.

c.removeAll(Collections.singleton(null));

 

3. Map Interface

a. Comparison to Hashtable 

Map provides Collection views instead of direct support for iteration via Enumeration objects. Collection views greatly enhance the expressiveness of the interface, as discussed later in this section. Map allows you to iterate over keys, values, or key-value pairs; Hashtable does not provide the third option. Map provides a safe way to remove entries in the midst of iteration; Hashtable did not. Hashtable has method contains, but Map has containsValue parallels containsKey.

 

4. Implementations

 

General-purpose Implementations
InterfacesHash table ImplementationsResizable array ImplementationsTree ImplementationsLinked list ImplementationsHash table + Linked list Implementations
SetHashSet TreeSet LinkedHashSet
List ArrayList LinkedList 
Queue     
MapHashMap TreeMap LinkedHashMap

 

Queue implementation include LinkedList and PriorityQueue.

 

5. Convenience Implementations

a. Arrays.asList

b. Immutable Multiple-Copy List

    Collections.nCopies

c. Immutable Singleton Set

    Collections.singleton

d.Empty Set, List, and Map Constants

    The Collections.emptySet, Collections.emptyList, and Collections.emptyMap.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值