java的容器類庫可以說是我們平時平時編程時使用最頻繁的類庫了月帝。下面介紹下這個使用最頻繁的兄弟伙:
java容器的分類圖
(http://www.cnblogs.com/wishyouhappy/p/3669198.html)
圖一 java容器簡化圖
圖的解釋:點線框表示接口瑰艘,實線框表示普通的(具體的)類。帶有空心箭頭的點線表示一個特定的類實現(xiàn)了一個接口盹牧,實心箭頭表示某個類可以生成箭頭所指向類的對象
圖二 java容器詳細(xì)圖
1)Collection:一個獨立元素的序列熄守,這些元素都服從一條或多條規(guī)則。(注:Collection其實就是將一組數(shù)據(jù)對象按照一維線性的方式組織起來)List必須按照插入的順序保存元素灌曙,而set不能有重復(fù)元素菊碟。Queue按照排隊規(guī)則來確定對象產(chǎn)生的順序(通常與它們被插入的順序相同)。
2)Map:一組成對的“鍵值對”對象在刺,允許你使用鍵來查找值逆害。(注:Map其實是將鍵與值形成的二元組按照一維線性的方式組織起來头镊,這里值得注意的是值可以使一個Collection或者M(jìn)ap,即嵌套結(jié)構(gòu)魄幕,如:Map<Integer,List<String>>,Map<String,Map<String>>)從另一個角度來考慮Map相艇,其實Map相當(dāng)于ArrayList或者更簡單的數(shù)組的一種擴(kuò)展、推廣纯陨。在數(shù)組中我們可以利用下標(biāo)即數(shù)字訪問數(shù)組當(dāng)中的不同元素坛芽,那么數(shù)字與對象之間形成了一種關(guān)聯(lián),那么如果將這個數(shù)字的概念擴(kuò)展成為對象翼抠,那同樣的我們可以將對象與對象之間關(guān)聯(lián)起來咙轩。即Map,也稱為映射表机久、關(guān)聯(lián)數(shù)組臭墨、字典允許我們使用一個對象來查找某個對象。
容器類的接口和抽象容器類
容器接口是容器的基礎(chǔ)膘盖。
使用接口可以將容器的實現(xiàn)與容器接口分開胧弛,因而可以使用相同的方法訪問容器而不需關(guān)心容器具體的數(shù)據(jù)結(jié)構(gòu)。同理侠畔,Iterator接口也使用戶能夠使用相同的方法訪問不同的容器類结缚。
常見的容器接口
- collection接口
* boolean add(Object obj): 添加對象,集合發(fā)生變化則返回true
* Iterator iterator():返回Iterator接口的對象
* int size()
* boolean isEmpty()
* boolean contains(Object obj)
* void clear()
* <T> T[] toArray(T[] a) - Map接口(Map中的值也可以是一個容器)
* Object get(Object key)
* Object put(Object key, Object value)
* Set keySet() : returns the keys set Set<K> keySet()
* Set entrySet(): returns mappings set Set<Map.Entry<K,V>> entrySet()
* containsKey() * containsValue() - Iterator接口
* Object next()
* boolean hasNext()
* void remove()
For each 與迭代器
Iterator软棺,即迭代器红竭。
主要用于遍歷容器
public static void main(String[] args){
Collection<String> cs = new LinkedList<String>();
Collection.addAll(cs,"take the long way home".split(" "));
for(String s:cs){
system.out.println(s);
}
}
之所以能夠使用foreach遍歷容器,是因為容器實現(xiàn)了iterator接口喘落。
只要實現(xiàn)了iterator接口就可以使用foreach遍歷茵宪,所以我們也可以實現(xiàn)自己的實現(xiàn)類。
exp:
public class IterableClass implements Iterable<String>{
protected String[] words = ("And that is how " +
"we know the Earch to be banana-shaped.").split(" ");
public Iterator<String> iterator(){
return new Iterator<String>(){
private int index = 0;
public boolean hasNext(){
return index < words.length;
}
public String next(){
return words[index++];
}
public void remove(){
throw new UnsupportedOperationException();
}
}
}
}
適配Collection的迭代器接口
針對我們自己創(chuàng)建的類我們可以通過實現(xiàn)Iterable接口來完成我們自己需要的遍歷操作瘦棋。然而如果我們利用foreach操作遍歷Java類庫中提供的Collection時稀火,就會限制我們的遍歷操作,因為如Java在ArrayList中只提供了從前往后的順序遍歷迭代器赌朋,假設(shè)我想要逆序遍歷
import java.util.*;
class ReversibleArrayList<T> extends ArrayList<T> {
public ReversibleArrayList(Collection<T> c) { super(c); }
public Iterable<T> reversed() {
return new Iterable<T>() {
public Iterator<T> iterator() {
return new Iterator<T>() {
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<String> ral =
new ReversibleArrayList<String>(
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