類集框架及容器遍歷

2021-08-30 13:01:34 字數 894 閱讀 6341

·類框架的最大介面:collection、map、iterator、enumeration

·collection:存放單值

|-list:允許重複值

|-arraylist:非同步處理,非執行緒安全,新的

|-vector:同步處理,執行緒安全,支援enumeration輸出,舊的

|-set:不允許有重複內容,考hashcode()和equals()進行重複驗證

|-hashset:無序存放

|-treeset:有序存放,按comparable排序

·map:存放一對值

|-hashmap:新的類,非同步處理,非執行緒安全,允許null

|-hashtable:舊的類,同步處理,非執行緒安全,不允許null

|-treemap:有序排列,按key排序,按comparable排序

·iterator:

|-迭代輸出,依靠collection和map中的iterator方法輸出,是新的輸出標準

·enumeration:舊的輸出操作

所有的collection都可以用一下3種方法遍歷:

1.for(int i=0; ii=c.iterator();

while(i.hasnext())

而map可用一下2種方法

map map = new hashmap();

for(object obj : map.keyset())

map map = new hashmap() ;

iterator iter = map.entryset().iterator();

while(iter.hasnext())

注:其實增強的for迴圈就是簡化了iterator的寫法,上面的2種寫法各有含iterator或省略的寫法,參見:

Java 類集框架

主要方法 add 增加資料 clear 清空資料 contains 是否包含某個資料 isempty 是否為空 remove 移除某個資料 size 獲取集合中的資料個數 toarray 轉換為物件陣列 iterator 例項化父介面iterator 物件陣列使用remove 和contains 時...

類集框架2

1.collection和iterator介面 collection集合set介面 set為collection的子介面,繼承了collection介面的方法 add 重複新增的元素會被忽略 因為set是無序的 所以不能像list那樣用get 也沒有get 方法 只能用迭代器來進行操作 迭代器 it...

容器類和介面框架

主要的介面 collection list set map 主要的類 arraylist linkedlist hashset linkedhashset treeset hashmap linkedhashmap treemap vector hashtable collections 1 阻塞佇...