Java Collections & Collection

2018-02-21  本文已影响0人  YoungJadeStone

在Java里,有Collection和Collections。


Collection是一个interface (see java docs)。

Set, List, Queue, Dequeue也是interface,它们都是Collection的subinterface。而我们常用的Map并不继承Collection,它是和Collection并列关系interface。

Collection, Set, List, Map关系

The collection interfaces are divided into two groups. The most basic interface, java.util.Collection, has the following descendants:

java.util.Set
java.util.SortedSet
java.util.NavigableSet
java.util.Queue
java.util.concurrent.BlockingQueue
java.util.concurrent.TransferQueue
java.util.Deque
java.util.concurrent.BlockingDeque

The other collection interfaces are based on java.util.Map and are not true collections. However, these interfaces contain collection-view operations, which enable them to be manipulated as collections. Map has the following offspring:

java.util.SortedMap
java.util.NavigableMap
java.util.concurrent.ConcurrentMap
java.util.concurrent.ConcurrentNavigableMap


而Collections是一个class (see java docs)。

常用的:
Collections.EMPTY_LIST
Collections.EMPTY_SET
Collections.EMPTY_MAP
Collections.singleton(T o) => return Set
Collections.singletonList(T o) => return List
Collections.singletonMap(K key, V value) => return Map
Collections.sort(List list)


比较:

@SuppressWarnings("unchecked")
public static final <T> List<T> emptyList() {
    return (List<T>) EMPTY_LIST;
}

Reference
https://docs.oracle.com/javase/7/docs/technotes/guides/collections/overview.html

上一篇下一篇

猜你喜欢

热点阅读