聊聊java的容器类

java的容器类库可以说是我们平时平时编程时使用最频繁的类库了。下面介绍下这个使用最频繁的兄弟伙:

java容器的分类图

(http://www.cnblogs.com/wishyouhappy/p/3669198.html)

聊聊java的容器类_第1张图片
Paste_Image.png

图一 java容器简化图
图的解释:点线框表示接口,实线框表示普通的(具体的)类。带有空心箭头的点线表示一个特定的类实现了一个接口,实心箭头表示某个类可以生成箭头所指向类的对象

聊聊java的容器类_第2张图片
Paste_Image.png

图二 java容器详细图

1)Collection:一个独立元素的序列,这些元素都服从一条或多条规则。(注:Collection其实就是将一组数据对象按照一维线性的方式组织起来)List必须按照插入的顺序保存元素,而set不能有重复元素。Queue按照排队规则来确定对象产生的顺序(通常与它们被插入的顺序相同)。
2)Map:一组成对的“键值对”对象,允许你使用键来查找值。(注:Map其实是将键与值形成的二元组按照一维线性的方式组织起来,这里值得注意的是值可以使一个Collection或者Map,即嵌套结构,如:Map>,Map>)从另一个角度来考虑Map,其实Map相当于ArrayList或者更简单的数组的一种扩展、推广。在数组中我们可以利用下标即数字访问数组当中的不同元素,那么数字与对象之间形成了一种关联,那么如果将这个数字的概念扩展成为对象,那同样的我们可以将对象与对象之间关联起来。即Map,也称为映射表、关联数组、字典允许我们使用一个对象来查找某个对象。

容器类的接口和抽象容器类

容器接口是容器的基础。

使用接口可以将容器的实现与容器接口分开,因而可以使用相同的方法访问容器而不需关心容器具体的数据结构。同理,Iterator接口也使用户能够使用相同的方法访问不同的容器类。

常见的容器接口

  • collection接口
        * boolean add(Object obj): 添加对象,集合发生变化则返回true
        * Iterator iterator():返回Iterator接口的对象
        * int size()    
    * boolean isEmpty()
        * boolean contains(Object obj)
        * void clear()
        * T[] toArray(T[] a)
  • Map接口(Map中的值也可以是一个容器)
    * Object get(Object key)
        * Object put(Object key, Object value)
        * Set keySet() : returns the keys set Set keySet()
        * Set entrySet(): returns mappings set Set> entrySet()
        * containsKey()    * containsValue()
  • Iterator接口
        * Object next()
        * boolean hasNext()
        * void remove()

For each 与迭代器

Iterator,即迭代器。
主要用于遍历容器

public static void main(String[] args){
Collection cs = new LinkedList();
Collection.addAll(cs,"take the long way home".split(" "));
for(String s:cs){
system.out.println(s);
}
}

之所以能够使用foreach遍历容器,是因为容器实现了iterator接口。
只要实现了iterator接口就可以使用foreach遍历,所以我们也可以实现自己的实现类。
exp:

public class IterableClass implements Iterable{
protected String[] words = ("And that is how " +
"we know the Earch to be banana-shaped.").split(" ");
public Iterator iterator(){
return new Iterator(){
private int index = 0;
public boolean hasNext(){
return index < words.length;
}
public String next(){
return words[index++];
}
public void remove(){
throw new UnsupportedOperationException();
}
}

}
}

适配Collection的迭代器接口

针对我们自己创建的类我们可以通过实现Iterable接口来完成我们自己需要的遍历操作。然而如果我们利用foreach操作遍历Java类库中提供的Collection时,就会限制我们的遍历操作,因为如Java在ArrayList中只提供了从前往后的顺序遍历迭代器,假设我想要逆序遍历

import java.util.*;
class ReversibleArrayList extends ArrayList {
public ReversibleArrayList(Collection c) { super(c); }
public Iterable reversed() {
return new Iterable() {
public Iterator iterator() {
return new Iterator() {
int current = size() - 1;
public boolean hasNext() { return current > -1; }
public T next() { return get(current--); }
public void remove() { // Not implemented
throw new UnsupportedOperationException();
}
};
}
};
}
}
public class AdapterMethodIdiom {
public static void main(String[] args) {
ReversibleArrayList ral =
new ReversibleArrayList(
Arrays.asList("To be or not to be".split(" ")));
// Grabs the ordinary iterator via iterator():
for(String s : ral)
System.out.print(s + " ");
System.out.println();
// Hand it the Iterable of your choice
for(String s : ral.reversed())

        System.out.print(s + " "); 
} 

} /* Output:
To be or not to be
be to not or be To

你可能感兴趣的:(聊聊java的容器类)