当前位置: 代码迷 >> .NET Framework >> Java Collection Framework 学习札记
  详细解决方案

Java Collection Framework 学习札记

热度:65   发布时间:2016-05-02 00:21:15.0
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 ImplementationsInterfacesHash 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.

      相关解决方案