java集合——集合框架

【0】README

0.1) 本文描述转自 core java volume 1, 源代码为原创,旨在理解 java集合——集合框架 的相关知识;


【1】集合框架

1.1) java集合类库构成了集合类的矿建, 它为集合的实现者定义了大量的接口和抽象类。

  • 1.1.1)集合框架的接口如下图所示:
    这里写图片描述
    对上图的分析(Analysis):

  • A0)集合(Collection+Map)和集Set的区别:并不是所有的集合都是集,但所有的集都是集合;

  • A1)集合有两个基本接口: Collection 和 Map。 用add 方法插入集合;
  • A2)使用 V put(K key, V value) 插入键值对到Map集合;V get(K key)从集合中读取元素;(也需要使用迭代器访问)
  • A3)List 是一个有序集合。元素可以添加到 容器中某个特定的位置。
    • A3.1)将对象放置到某个位置上有两种方法: 使用整数索引或使用列表迭代器;
    • A3.2)List 接口定义 了几个用于随机访问的方法:
void add(int index, E lement)
E get(int index)
void remove(int idnex)
  • A3.3)RandomAccess 接口: List 接口在提供这些随机访问方法时,并不管他们对某种特定实现是否高效。 为了避免执行成本较高的随机访问操作, java SE 1.4 引入了一个标记接口 RandomAccess;

    • A3.4)这个接口没有任何方法, 但可以用来检测一个特定的集合是否支持高效的随机访问:(干货——RandomAccess 接口的作用)
* java.util Interface RandomAccess **All Known Implementing Classes:** ArrayList, AttributeList, CopyOnWriteArrayList, RoleList, RoleUnresolvedList, Stack, Vector public interface RandomAccess * Marker interface used by List implementations to indicate that they support fast (generally constant time) random access. The primary purpose of this interface is to allow generic algorithms to alter their behavior to provide good performance when applied to either random or sequential access lists. * The best algorithms for manipulating random access lists (such as ArrayList) can produce quadratic behavior when applied to sequential access lists (such as LinkedList). Generic list algorithms are encouraged to check whether the given list is an instanceof this interface before applying an algorithm that would provide poor performance if it were applied to a sequential access list, and to alter their behavior if necessary to guarantee acceptable performance. * It is recognized that the distinction between random and sequential access is often fuzzy. For example, some List implementations provide asymptotically linear access times if they get huge, but constant access times in practice. Such a List implementation should generally implement this interface. As a rule of thumb, a List implementation should implement this interface if, for typical instances of the class,
this loop:
for (int i=0, n=list.size(); i < n; i++)
    list.get(i); 
runs faster than this loop:
for (Iterator i=list.iterator(); i.hasNext(); )
    i.next(); 
* This interface is a member of the Java Collections Framework. Since: 1.4 * 该标记接口被List实现类用来指示是否它们支持快速(通常是常数时间)随机访问。该接口的主要目的是 允许一般算法可以更改其行为以提供良好性能, 当该接口应用于随机抑或是顺序访问列表的时候。 * 操纵随机访问列表(如ArrayList)的最佳算法是可以产生二次行为, 当它被应用于顺序访问列表的时候(如LinkedList)。 如果需要保证可接受的访问性能,在一个已经被应用到顺序访问列表且性能不佳的算法被应用之前,鼓励使用一般列表算法检查是否给定列表是该接口的一个实例,; * 我们意识到,随机访问和顺序访问的区别是模糊的。举个荔枝,一些List实现类提供趋于稳定地线性访问时间,如果它们在实际应用中获得一个较大且固定的访问时间的话。这样一个List实现类普遍应该实现该接口。 根据经验,一个List 实现类应该实现这个接口,如果可能的话。 * 该类的一个典型荔枝: 循环:
for (int i=0, n=list.size(); i < n; i++)
         list.get(i); 
明显快于下述循环:
 for (Iterator i=list.iterator(); i.hasNext(); )
         i.next(); 


if(c instanceof RandomAccess)
{
    use random access alg
}
else
{
    use sequential access alg
}

Annotation)

  • A1)理论上说, 有一个独立的Array接口, 它扩展于List接口, 并声明了随机访问方法是很合理的;
  • A2)但是,实际上,并没有去定义这样一个接口, 原因是 希望让类库的接口数量保持在一个较少的水平

1.2) ListIterator接口定义了一个方法,用于将一个元素添加到 迭代器所处位置的前面:void add(E element) , detailed Interface ListIterator , http://blog.csdn.net/pacosonswjtu/article/details/50302083

  • 1.2.1)要想获取和删除给定位置的元素, 只需要调用 Iterator 接口中的next方法和 remove 方法即可;

1.3)Set接口与 Collection接口是一样的,只不过其方法的行为有着更加严谨的定义:

  • 1.3.1)集Set的add 方法拒绝添加重复的元素;
  • 1.3.2)集Set的 equals 方法定义两个集相等的条件是它们包含相同的元素但顺序不必相同;
  • 1.3.3) hashCode 方法定义应该保证具有相同元素的集合将会得到相同的散列码;
  • 1.3.4)既然方法签名都相同了, 为什么还要建立一个独立的接口呢?(干货) 从概念上讲,并不是所有集合都是集。 建立Set 接口后, 可以让程序员编写仅接受集的方法;

1.4) SortedSet 和 SortedMap 接口暴露了 用于排序的比较器对象, 并且定义的方法可以获得集合的子集视图;
1.5)最后, Java SE 6 引入了接口 NavigableSet 和 NavigatableMap, 其中包含了 几个用于在有序集合映射表中查找和遍历的方法(从理论上讲, 这几个方法以及包含在 SortedSet 和 SortedMap的接口中)。 TreeSet 和 TreeMap 类实现了这几个接口;
1.6) 集合接口有大量的方法,这些方法可以通过更基本的方法加以实现。 抽象类提供了很多这样的实例:

AbstractCollection
AbstractList
AbstractSequentialLIst
AbstractSet
AbstractQueue
AbstractMap
  • 1.6.1)要知道, 抽象类实现了集合接口的部分方法, 其余方法由其子类实现;
  • 1.6.2) 如果实现了自己的集合类,就可能要扩展上面某个类;

1.7)java 类库支持下面几种具体类:

LinkedList
ArrayList
ArrayDeque
HashSet
TreeSet
PriorityQueue
HashMap
TreeMap
  • 1.7.1)下图展示了这些类间的关系:
    这里写图片描述
  • 1.7.2)最后, 还有许多java 第一版留下来的 容器类(遗留下来的):
Vector + Stack + Hashtable + Properties
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值