《Java Generics and Collections》笔记-Lists/Maps

本文详细探讨了Java中的Lists和Maps实现,包括LinkedList、CopyOnWriteArrayList、HashMap、LinkedHashMap、ConcurrentHashMap等,分析了不同实现的性能比较,并重点介绍了并发集合的使用。
摘要由CSDN通过智能技术生成

1.Lists

前面主要是在介绍用法,省去不写。
List<E> subList(int fromIndex, int toIndex)// return a view of a portion of the list
The returned list has no separate existence. it is just a view of part of the list from which it was obtained,so changes in it are reflected in the original list. There is an important difference from subSet, though; changes you make to the sublist write through to the backing list,but the reverse is not always true. If elements have been inserted into or removed from the backing list by directly calling one of its "structure changing" methods (Section 12.1), any subsequent attempts to use the sublist will result in a ConcurrentModificationException.
ListIterator positions
The current position of a ListIterator always lies between two elements, so in a list of length n, there are n+1 valid list iterator positions, from 0 (before the first element) to n (after the last one).
如果当前在位置0处,previousIndex返回-1,nextIndex返回n。而它的previous和next方法,抛出NoSuchElementException。

Implementing List
The performance of ArrayList reflects array performance for "random-access" operations: set and get take constant time. The downside of an array implementation is in inserting or removing elements at arbitrary positions, because that may require adjusting the position of other elements. (We have already met this problem with the remove method of the iterators of array-based queues for example, ArrayBlockingQueue (see Section 14.3.2). But the performance of positional add and remove methods are much more important for lists than iterator.remove is for queues.)

LinkedList

CopyOnWriteArrayList

In Section 13.1 we met CopyOnWriteArraySet, a set implementation designed toprovide thread safety together with very fast read access. CopyOnWriteArrayList is a List implementation with the same design aims. This combination of thread safety with fast read access is useful in some concurrent programs, especially when a collection of observer objects needs to receive frequent event notifications. The cost is that the array which backs the collection has to be treated as immutable, so a new copy is created whenever any changes are made to the collection.This cost may not be too high to pay if changes in the set of observers occur only rarely.

The class CopyOnWriteArraySet in fact delegates all of its operations to an instance of CopyOnWriteArrayList, taking advantage of the atomic operations addIfAbsent and addAllAbsent provided by the latter to enable the Set methods add and addAll to avoid introducing duplicates to the set. In addition to the two standard constructors (see Section 12.3), CopyOnWriteArrayList has an extra one that allows it to be created using the elements of a supplied array as its initial contents. Its iterators are snapshot iterators, reflecting the state of the list at the time of their creation.

Comparing List Implementations


For most list applications the choice is between ArrayList and LinkedList, synchronized or not. Once again, your decision will depend on how the list is used in practice. Ifset and get predominate, or element insertion and removal is mainly at the end of the list, then ArrayList will be the best choice. If, instead, your application needs to frequentlyinsert and remove elements near the start of the list as part of a process that uses iteration, LinkedList may be better. If you are in doubt, test the performance with each implementation. A Java 6 alternative for single-threaded code that may be worth considering in the last case if the insertions and removals are actually at the start of the list is to write to the Deque interface, taking advantage of its very efficient ArrayDeque implementation. For relatively infrequent random access, use an iterator, or copy the ArrayDeque elements into an array using toArray.
It is possible that, in a future release, ArrayDeque will be retrofitted to implement the List interface; if that happens, it will become the implementation of choice for both Queue and List in single-threaded environments
.

2.Maps


The Map interface is the last of the major Collections Framework interfaces, and the only one that does not inherit from Collection.
看一下需要注意的方法
Providing Collection Views of the Keys, Values, or Associations:
Set<Map.Entry<K, V>> entrySet() // return a Set view of the associations
Set<K> keySet()                 // return a Set view of the keys
Collection<V> values()          // return a Collection view of the values

The collections returned b
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值