java基礎(五) 集合

2021-09-05 18:57:52 字數 3665 閱讀 4519

1.集合與陣列的區別

(1).陣列不可變長度,集合可變長度

(2).陣列裡可放基本資料型別和物件,集合只能放物件

2.集合框架頂層介面collection的共性方法

增:add(object obj);     addall(collection c);

刪:clear();    remove(object obj);    removeall(collection c);

判斷是否為空:isempty();

判斷長度:size();

判斷是否包含:contains(object obj);    containsall(collection c);

集合轉陣列:toarray();

a集合中去除和b集合有交集的部分:a.removeall(b);

a集合中取得和b集合的交集部分:a.retainall(b);

迭代器遍歷集合:

for(iterator it = coll.iterator(); it.hasnext();)

3.collection下一層

list:有索引,有序

set:不重複

4.list的特有方法

增:add(index,element);

刪:remove(index);

改:set(index,element);

查:get(index);    indexof(element);

遍歷:for(int i = 0; ilist.get(i);

}注:list遍歷一般不用迭代器,如果用的話,迭代器型別要用linkediterator,不然如果做增刪會報錯,因為沒有同步。

5.list子類

vector:陣列,執行緒同步。查詢、增刪都慢(用得少)

arraylist:陣列,執行緒不同步。查詢快、增刪慢

linkedlist:鍊錶,執行緒不同步。查詢慢,增刪快

arraylist的例項:將arraylist重複的部分去除

方法一:

for(int i = 0;iobject obj1 = list.get(i);

for(int j = i+1;jobject  obj2 = list.get(j);

if(obj1 == obj2)}}

方法二:

arraylist temp = new arraylist();

for(int i = 0;iobject obj = list.get(i);

if(!temp.contains(obj))

}linkedlist有幾個特有的方法:

addfirst(),    getfirst(),    removefirst();    (last同first)

6.set子類

hashset:雜湊表結構,查詢極快。

treeset:二叉樹結構,執行緒不同步。可通過構造器對元素排序。

hashset在做add時需要重寫hashcode()和equal()方法。

treeset在做add時,如果返回值為0,不插入。寫構造器的方法有兩種:

(1).實現comparable介面,覆蓋compareto()方法

(2).實現comparator介面,覆蓋compare()方法。

hashset重寫方法示例: 

public int hashcode()

public boolean equals(object obj)

class student implements comparable

}class comparestringbylength implements comparator

}7.map:雙列集合,鍵值對,鍵不可以相同。

共性功能:

增:put(key,value);    putall(map map);    (注:put()方法返回的是key的舊值)

刪:clear();    remove(key);

判斷長度:size();

判斷是非為空:isempty();

判斷是否包含:containskey();    containsvalue();

查:get(key);

遍歷(兩種方法):keyset,entryset

keyset:

setkeys = mapmap.keyset();

for (iteratorit = keys.iterator(); it.hasnext(); )

mapmap = new map();

map.put("a","aaa");

map.put("b","bbb");

setkeys = map.keyset();

for(iteratorit = keys.iterator(); it.hasnext();)

entryset:

mapmap = new map();

map.put("a","aaa");

map.put("b","bbb");

set> entry = map.entryset();

for(iterator> it = entry.iterator(); it.hasnext();)

8.map子類

hashtable:雜湊表,執行緒同步,不允許null鍵

hashmap:雜湊表,執行緒不同步,允許null鍵

linkedhashmap:在hashmap的基礎上加了排序

treemap:二叉樹,不同步,可對key排序

例項:乙個字串(str)統計每個字元出現的次數

treemapmap = new treemap();

char chars = str.tochararray();.

for(int i = 0;iinteger vaue = map.get(chars[i]);

int count = 0;

if(value != null)    count = value;

count++;

map.put(chars[i],count);

}stringbuffer sb = new stringbuffer();

setkeys = map.keyset();

for(iteratorit = keys.iterator();it.hasnext();)

9.集合框架總結

陣列:查詢快,增刪慢。(array)

鍊錶:查詢慢,增刪快。(linked)

list:有序

set:不能重複

排序:collections.sort(list,new comparestringbylength());    //自己定義的過濾器,參考上面

逆序:collections.sort(list,collection.reverseorder(new comparestringbylength()));

最大(小)值:collections.max(list,new comparestringbylength());

二分查詢:collections.binarysearch(list,key);

非同步變成同步:collections.synchronizedcollection(coll);

集合變陣列:list.toarray(new string[list.size()]);

陣列變集合:arrays.tolist(array);

Java基礎總結記錄 五 集合

集合類在應用中常常用到,裡面的知識也是比較多的,總結一下常用的一些集合類。一 集合圖 針對這張圖來展開集合之路。二 collections 這個類是個工具類用來服務我們的集合類的,裡面的方法本人沒有一一用過,也說不好,我就把一些可能會用到的方法拿出來說下,順便加深下集合類的記憶。1.排序 suppr...

任務五。集合的運算

printf 該兩集合是否相等 n xd a,b,x,y printf n printf 該兩個集合的並集結果如下 n bing a,b,x,y printf n printf 該兩個集合的交集結果如下 n jiao a,b,x,y printf n printf 該a b集合的相對補集結果如下 n...

PL SQL複習五 集合方法

1.exists 判斷某集合元素是否存在 declare type ename table type is table of emp.ename type ename table ename table type begin if ename table.exists 1 then dbms out...