java集合框架

2021-06-21 23:12:48 字數 3466 閱讀 4720

集合框架包括集合與對映(collection and map), 以及它們的子類(容器類)

1) list 元素有先後次序的集合, 元素有index位置, 元素可以重複,繼承自collection介面,

? 實現類: arraylist, vector, linkedlist

2) set 元素無續, 不能重複新增, 是數學意義上的集合, 繼承自collection 介面

? 實現類: hashset(是乙個只有key的hashmap)

3) collection 集概念, 沒有說明元素是否重複和有序, 使用集合的跟介面, 很少直接使用

其他集合都是實現類: arraylist, hashset

4) map 描述了(key:value)成對放置的集合,key丌重複,value可以重複(key重複算一 個)

collection介面 表示集合的概念

list介面

set介面

list介面 表示有序線性表的概念     

arraylist 底層實現是陣列

linkedlist 底層實現是鍊錶

set介面 表示無序不重複的概念

hashset

注意collection 介面儲存一組不唯一,無序的物件

list 介面儲存一組不唯一,有序(插入順序)的物件

set 介面儲存一組唯一,無序的物件

map介面儲存一組鍵值物件,提供key到value的對映

list

arraylist實現了長度可變的陣列,在記憶體中分配連續的空間。遍歷元素和隨機訪問元素的效率比較高

linkedlist採用鍊錶儲存方式。插入、刪除元素時效率比較高

書籍管理系統中

採用linkedlist對書籍增加刪除操作

修改和查詢書籍使用arraylist

例子1:

student(string name,int age)

student stu1=new student("張三", 15);

student stu1=new student("張三",15);

student stu2=new student("李四",15);

student stu3=new student("王五",15);

student stu4=new student("測試",15);

listlist=new arraylist();

list.add(stu1);

list.add(stu2);

list.add(stu3);

system.out.println(list.size());

例子2:

student s=(student)list.get(i);------報錯,強制轉換為student型別,如果使用泛型規定好型別,就不需要強制轉換

把任何型別物件通過add(object obj) 放入list中,認為只是object型別

通過get(int index) 取出list中元素時必須進行強制型別轉換,繁瑣而且容易出現異常

使用map的put(object key, object value)和get (object key)訪問物件時存在同樣問題

jdk5.0中通過引入泛型有效的解決了這個問題

jdk5.0使用泛型改寫了集合框架中的所有介面和類

例子3:

列印出儲存在student的list中的學生姓名

清空集合list.clear();

刪除指定位置的學生,如第乙個list.remove(index); list.remove(object);

刪除指定的學生,如刪除張三物件

迴圈中找到姓名為張三的物件

判斷集合中是否包含指定

list.contains(object)

在指定位置新增物件

list.add(1, stu4);

插入、刪除操作頻繁時,可使用linkedlist來提高效率

linkedlist還額外提供對頭部和尾部元素進行新增和刪除操作的方法

例子1linkedlistlist=new linkedlist();

list.add(stu1);

list.add(stu2);

list.addfirst(stu3);

list.addlast(stu4);

例子2刪除頭處

list.removefirst();   list.removelast();--刪除尾部    

list.getfirst().getname()--獲得首部

map介面專門處理鍵值對映資料的儲存,可以根據鍵實現對值的操作

最常用的實現類是hashmap

例子1:新增

mapcountries =new hashmap();

countries.put("四川", "成都");

countries.put("四川", "綿陽");

countries.put("四川", "宜賓");

countries.put("四川", "樂山");

例子2:

獲得指定元素的值

string address=countries.get("四川");

system.out.println(address);

這時候只會列印樂山-----被覆蓋了(key,value)

例子3:

countries.size()-----獲得map裡邊元素的個數

countries.remove()----刪除指定元素

countries.containskey()

countries.keyset()----獲得所有map裡邊的key

countries.values()----獲得所有map裡邊的value

vector和arraylist的異同

實現原理相同,功能相同,很多情況下可以互用

兩者的主要區別如下

vector執行緒安全,arraylist重速度輕安全,執行緒非安全

長度需增長時,vector預設增長一倍,arraylist增長50%

hashtable和hashmap的異同

實現原理相同,功能相同,在很多情況下可以互用

兩者的主要區別如下

hashtable繼承dictionary類,hashmap實現map介面

hashtable執行緒安全,hashmap執行緒非安全

hashtable不允許null值,hashmap允許null值

元素遍歷·

所有集合介面和類都沒有提供相應遍歷方法,而是由iterator實現集合遍歷

collection 介面的iterate()方法返回乙個iterator,然後通過iterator介面的兩個方法可實現遍歷

boolean hasnext(): 判斷是否存在另乙個可訪問的元素

object next(): 返回要訪問的下乙個元素

iterator it = dogs.iterator();

while (it.hasnext())

java集合框架

框架 為了解決某一特定的問題,預先設計好的一系列具有繼承或實現關係的類的介面。集合裡的三大類 list直接繼承collection 特點 線性,有序 arraylist底層的實現是陣列 使用範圍 當某一陣列在實際應用中大量使用查詢和新增功能的時候用arraylist linkedlist底層的實現是...

java 集合框架

集合一般使用list,set,map首先說下list 1.list一般使用arraylist,linkedlist list的特點有序,重複,那麼arraylist和linkedlist的區別 arraylist插入和刪除資料慢,取值速度快,實際上arraylist是陣列的擴充 linkedlist...

java集合框架

說到集合你會想到什麼,容器,陣列?都對,都是用作儲存相同型別的資料,不過我要說的是集合和陣列之間最大的差別,陣列一旦定義長度就不能更改 定長 集合則相反,可以自由新增資料,容量自動增加。集合和陣列長度限制區別 public class container 現在重點來說說集合,前面說了,是儲存相同元素...