How is ArrayListMultimap different from LinkedListMultimap?
所以,我只是在读取
它在文档......和代码中。基本上除了你已经看到的一个区别(
-
ArrayListMultimap 使用HashMap 表示map和ArrayList cor集合,这意味着未定义entries() ,asMap().keySet() 或asMap.entrySet() 等方法的迭代顺序。这是ListMultimap 的简单实现,你应该从这个开始。 -
LinkedListMultimap 使用LinkedList 进行收集和专用数据结构(自定义链表)以维护上述方法的迭代顺序:Order is maintained using a linked list containing all key-value
pairs. In addition, a series of disjoint linked lists of
"siblings", each containing the values for a specific key, is used
to implement ValueForKeyIterator in constant time.此外,它使用很少的其他结构来维护"链表"式行为:
1
2
3
4
5private transient Node<K, V> head; // the head for all keys
private transient Node<K, V> tail; // the tail for all keys
private transient Multiset<K> keyCount; // the number of values for each key
private transient Map<K, Node<K, V>> keyToKeyHead; // the head for a given key
private transient Map<K, Node<K, V>> keyToKeyTail; // the tail for a given key
此外,内存占用是这些
就个人而言,当我需要具有定义的密钥迭代顺序的高效,可变
1 2 | ListMultimap<String, Integer> treeListMultimap = MultimapBuilder.linkedHashKeys().arrayListValues().build(); |
在v16.0之前创建自定义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * Creates {@link ListMultimap} preserving insertion order of keys and values * (it's backed by {@link LinkedHashMap} and {@link ArrayList}). */ public static <K, V> ListMultimap<K, V> newLinkedArrayListMultimap() { return Multimaps.newListMultimap( Maps.<K, Collection<V>>newLinkedHashMap(), new Supplier<List<V>>() { @Override public List<V> get() { return Lists.newArrayList(); } }); } |