一、集合框架的概述
-
集合、陣列都是對多個資料進行存盤操作的結構,簡稱Java容器,
說明:此時的存盤,主要指的是記憶體層面的存盤,不涉及到持久化的存盤, -
陣列在存盤多個資料方面的特點
- 一旦初始化以后,其長度就確定了,
- 陣列一旦定義好,其元素的型別也就確定了,只能操作指定型別的資料,
- 陣列的缺點:
- 一旦初始化以后,其長度就不可修改,
- 陣列中提供的方法非常有限,對于添加、洗掉、插入資料等操作,非常不便,同時效率不高,
- 獲取陣列中實際元素的個數的需求,陣列沒有現成的屬性或方法可用,
- 陣列存盤資料的特點:有序、可重復,無法滿足無序、不可重復的需求,
二、集合框架
- Collection介面:單列集合,用來存盤一個一個的物件
- List介面:存盤有序的、可重復的資料, --> “動態”陣列
ArrayList、LinkedList、Vector - Set介面:存盤無序的、不可重復的資料 --> 類似于高中講的“集合”
HashSet、LinkedHashSet、TreeSet
- Map介面:雙列集合,用來存盤一對一對(key - value)的資料 --> 類似于高中函式 y=f(x)
HashMap、LinkedHashMap、TreeMap、HashTable、Properties
三、Collection介面中方法的使用
結論:向Collection介面的實作類的物件中添加資料obj時,要求obj所在類要重寫equals(),
@Test
public void test1(){
Collection coll = new ArrayList();
//add(Object e):將元素e添加到集合coll中
coll.add("AA");
coll.add("BB");
coll.add(123);//自動裝箱
coll.add(new Date());
//size():獲取添加的元素的個數
System.out.println(coll.size());//4
//addAll(Collection coll1):將coll1集合中的元素添加到當前的集合中
Collection coll1 = new ArrayList();
coll1.add(456);
coll1.add("CC");
coll.addAll(coll1);
System.out.println(coll.size());//6
System.out.println(coll);
//clear():清空集合元素
coll.clear();
//isEmpty():判斷當前集合是否為空
System.out.println(coll.isEmpty());
coll.add(123);
coll.add(456);
coll.add(new Person("Jerry",20));
coll.add(new String("Tom"));
coll.add(false);
//在判斷時會呼叫obj物件所在類的equals(),
//1.contains(Object obj):判斷當前集合中是否包換obj
System.out.println(coll.contains(123));
System.out.println(coll.contains(new String("Tom")));
System.out.println(coll.contains(new Person("Jerry", 20)));//Person重寫了equals()后為true,不重寫則為false
//2.containsAll(Collection coll1):判斷形參coll1中的所有元素是否都存在于當前集合中
Collection coll2 = Arrays.asList(123,456);
System.out.println(coll.containsAll(coll2));
}
@Test
public void test2(){
//3.remove(Object obj):從當前集合中移除obj元素,
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new Person("Jerry",20));
coll.add(new String("Tom"));
coll.add(false);
coll.remove(1234);
System.out.println(coll);
coll.remove(new Person("Jerry",20));
System.out.println(coll);
//4.removeAll(Collection coll1):從當前集合中移除coll1中所有的元素(差集),
Collection coll1 = Arrays.asList(123,4567);
coll.removeAll(coll1);
System.out.println(coll);
}
@Test
public void test3(){
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new Person("Jerry",20));
coll.add(new String("Tom"));
coll.add(false);
//5.retainAll(Collection coll1):獲取當前集合與coll1集合的交集,并回傳給當前集合(交集),
// Collection coll1 = Arrays.asList(123,456,789);
// coll.retainAll(coll1);
// System.out.println(coll);
//6.equals(Object obj):想要回傳true,需要當前集合與形參集合的元素都相同,
Collection coll1 = new ArrayList();
coll1.add(456);
coll1.add(123);
coll1.add(new Person("Jerry",20));
coll1.add(new String("Tom"));
coll1.add(false);
System.out.println(coll.equals(coll1));
}
@Test
public void test4(){
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new Person("Jerry",20));
coll.add(new String("Tom"));
coll.add(false);
//7.hashCode():回傳當前物件的哈希值
System.out.println(coll.hashCode());
//8.集合 --> 陣列:toArray()
Object[] arr = coll.toArray();
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
//拓展:陣列 --> 集合:呼叫Arrays類的靜態方法asList()
List<String> list = Arrays.asList(new String[]{"AA", "BB", "CC"});
System.out.println(list);
List<int[]> arr1 = Arrays.asList(new int[]{123, 456});
System.out.println(arr1.size());//1
List<Integer> arr2 = Arrays.asList(new Integer[]{123, 456});
System.out.println(arr2.size());//2
//9.iterator():回傳Iterator介面的實體,用于遍歷集合元素,放在IteratorTest中測驗
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/288064.html
標籤:其他
上一篇:Python爬蟲實戰,Scrapy實戰,爬取并簡單分析知網中國專利資料
下一篇:Python開發一個滑雪小游戲
