现在的位置: 首页 > 综合 > 正文

Collection (part 1)

2013年10月06日 ⁄ 综合 ⁄ 共 2827字 ⁄ 字号 评论关闭

基本概念
首先说一下collection的本质。
本质上这东西就是实现了java.uil.collection接口的泛型类。

(在后面的collection framework中看到,其实还有map接口)
The fundamental interface for collection classes in the Java library is the Collection interface.
因此如果想实现自己的数据结构,通过implement collection interface就可以实现。比如实现某一种queue,就可以实现Queue interface(subinterface of collection)。当然也可以通过继承abstractQueue(可以少写不少方法呢~)。
再回来看看collection interface:
The interface has two fundamental methods:
public interface Collection<E>
{
boolean add(E element);
Iterator<E> iterator();--这里是implements Iterable interface。
. . .
}
Iterator的作用是you can use the iterator object to visit the elements in the collection one by one. 所有实现了Iterable接口的class都必须实现迭代器,然后就可以用foreach访问了。从后面的framework可以看出来,所有collection都可以用foreach。

The Iterator interface has three methods:
public interface Iterator<E>
{
E next();
boolean hasNext();
void remove();
}

The “for each” loop works with any object that implements the Iterable interface, an
interface with a single method:
public interface Iterable<E> --Iterable和Iterator都是接口。
{
Iterator<E> iterator();
}
Collection<String> c = ...;
Iterator<String> iter = c.Iterator();


The order in which the elements are visited depends on the collection type.
The lookup and position change are tightly coupled. The only way to look up an element is to call next, and that lookup advances the position.
More important, there is a dependency between calls to the next and remove methods. It is
illegal to call remove if it wasn’t preceded by a call to next。
所以java和其他语言中的collection有很大不同。其他语言中是根据对数字索引建模的。就是说可以用a[i]来引用,但是java中不行。

这话说的是Iterator的基本功能。从上面iterator接口的定义就可以看出,由于仅仅实现了next(),hasnext()和remove(),所以迭代器只能一个一个的next来获得collection内容。也就是说,如果自己建了一个collection且仅仅实现了最基本的接口,就无法使用index功能。

但是在实际应用中就完全不同了,由于实际的class实现了更多的方法,所以访问collection中的元素方法有:

1,一个一个next()。

2,每个具体collection实现的get方法。

3,有一些class仍然实现了index访问。

 

The concrete collections in Java

Collection <wbr>(part <wbr>1)

集合框架

 

有了前面的知识,要理解框架就很简单了。
collection中的interface:
Collection <wbr>(part <wbr>1)

可见有独立的两种collection:实现了collection接口和实现了map接口的类。

有一点需要注意的array并没有被算到collection的体系内,但有一些collection实现了array的接口。比如ArrayList,ArrayDeque。

ListIterator is a special iterator, that allows element insertion and replacement, and bidirectional access in addition to the normal operations that the Iterator interface provides.

NavigableSet is a sortedset extended with navigation methods reporting closest matches for given search targets.

 

collection中的抽象类:

Collection <wbr>(part <wbr>1)

 

wrapper,view和算法

 

比如Arrays类有一个方法allows arrays to be viewed as lists. 这就是wrapper的功能。

 

一个轻量级wrapper的实现:

The static asList method of the Arrays class returns a List wrapper around a plain Java
array. This method lets you pass the array to a method that expects a list or collection
argument. For example:
Card[] cardDeck = new Card[52];
List<Card> cardList = Arrays.asList(cardDeck); //注意List只是一个接口。

.asList实际上返回的是什么类,不太清楚。

 

这个视图和DB里的视图很像,都是在原有对象上又包装了一层,只留下最基本的性质。view的使用和wrapper我理解是融合在一起的。

 

算法的用途:其实就是让算法实现泛型操作。例如使用一个单一的算法(方法)就可以实现对
a linked list, an array list, or an array 全部的操作(计算)。

That’s a powerful concept.

后面就是讲了一些排序,二分以及如何实现自己订制的算法。

 

以上就是java collection全部涉及的概念。在后面的文章中会有更详细的说明。

除了看core java,还可以看看这篇文章:

http://java.sun.com/developer/onlineTraining/collections/Collection.html

抱歉!评论已关闭.