
目錄
1.Collection集合
2.List集合
3.List集合的實作類
4.Set集合
5.TreeSet集合
6.HashSet集合
7.Map集合
8.HashMap集合
9.TreeMap集合
1.Collection集合
1.1 Collection集合概述 :
① 是單例集合的頂層介面,它表示一組物件,這些物件也稱為Collection的元素
② JDK 不提供此介面的任何直接實作.它提供更具體的子介面(如Set和List)實作
1.2 Collection集合常用方法
| boolean add(E e) | 添加元素 |
| boolean remove(Object o) | 從集合中移除指定的元素 |
| boolean removeIf(Object o) | 根據條件進行移除 |
| void clear() | 清空集合中的元素 |
| boolean contains(Object o) | 判斷集合中是否存在指定的元素 |
| boolean isEmpty() | 判斷集合是否為空 |
| int size() | 集合的長度,也就是集合中元素的個數 |
1.3 Collection集合的遍歷
①迭代器 :
Iterator<E> iterator(): 回傳此集合中元素的迭代器,通過集合物件的iterator()方法得到
②Iterator中的常用方法
boolean hasNext(): 判斷當前位置是否有元素可以被取出 ?
E next(): 獲取當前位置的元素,將迭代器物件移向下一個索引位置
示范代碼 :
public class IteratorDemo {
public static void main(String[] args) {
//創建集合物件
Collection<String> c = new ArrayList<>();
//添加元素
c.add("hello");
c.add("world");
c.add("java");
//Iterator<E> iterator():回傳此集合中元素的迭代器,通過集合的iterator()方法得到
Iterator<String> it = c.iterator();
//用while回圈改進元素的判斷和獲取
while (it.hasNext()) {
String s = it.next();
System.out.println(s);
}
}
}
2.List集合
2.1 List集合概述
① 有序集合,這里的有序指的是存取順序
② 用戶可以精確控制串列中每個元素的插入位置,用戶可以通過整數索引訪問元素,并搜索串列中的元素
③ 與Set集合不同,串列通常允許重復的元素
2.2 List集合的特點
存取有序
可以重復
有索引
2.3 List集合的方法
| void add(int index,E element) | 在此集合中的指定位置插入指定的元素 |
| E remove(int index) | 洗掉指定索引處的元素,回傳被洗掉的元素 |
| E set(int index,E element) | 修改指定索引處的元素,回傳被修改的元素 |
| E get(int index) | 回傳指定索引處的元素 |
示例代碼 :
public class ListDemo {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
//method1(list);
//method2(list);
//method3(list);
//method4(list);
}
private static void method4(List<String> list) {
// E get(int index) 回傳指定索引處的元素
String s = list.get(0);
System.out.println(s);
}
private static void method3(List<String> list) {
// E set(int index,E element) 修改指定索引處的元素,回傳被修改的元素
//被替換的那個元素,在集合中就不存在了.
String result = list.set(0, "ppp");
System.out.println(result);
System.out.println(list);
}
private static void method2(List<String> list) {
// E remove(int index) 洗掉指定索引處的元素,回傳被洗掉的元素
//在List集合中有兩個洗掉的方法
//洗掉指定的元素,回傳值表示當前元素是否洗掉成功
String s = list.remove(0);
//洗掉指定索引的元素,回傳值表示實際洗掉的元素
System.out.println(s);
System.out.println(list);
}
private static void method1(List<String> list) {
// void add(int index,E element) 在此集合中的指定位置插入指定的元素
//原來位置上的元素往后挪一個索引.
list.add(0,"ppp");
System.out.println(list);
}
}
3.List集合的實作類
3.1 ArrayList集合
底層是陣列結構實作,查詢快、增刪慢
3.2 LinkedList集合
底層是鏈表結構實作,查詢慢、增刪快
3.3 LinkedList集合特有方法
| public void addFirst(E e) | 在該串列開頭插入指定的元素 |
| public void addLast(E e) | 將指定的元素追加到此串列的末尾 |
| public E getFirst() | 回傳此串列中的第一個元素 |
| public E getLast() | 回傳此串列中的最后一個元素 |
| public E removeFirst() | 從此串列中洗掉并回傳第一個元素 |
| public E removeLast() | 從此串列中洗掉并回傳最后一個元素 |
示例代碼 :
public class LinkedListDemo {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
// public void addFirst(E e) 在該串列開頭插入指定的元素
//method1(list);
// public void addLast(E e) 將指定的元素追加到此串列的末尾
//method2(list);
// public E getFirst() 回傳此串列中的第一個元素
// public E getLast() 回傳此串列中的最后一個元素
//method3(list);
// public E removeFirst() 從此串列中洗掉并回傳第一個元素
// public E removeLast() 從此串列中洗掉并回傳最后一個元素
//method4(list);
}
private static void method4(LinkedList<String> list) {
String first = list.removeFirst();
System.out.println(first);
String last = list.removeLast();
System.out.println(last);
System.out.println(list);
}
private static void method3(LinkedList<String> list) {
String first = list.getFirst();
String last = list.getLast();
System.out.println(first);
System.out.println(last);
}
private static void method2(LinkedList<String> list) {
list.addLast("www");
System.out.println(list);
}
private static void method1(LinkedList<String> list) {
list.addFirst("qqq");
System.out.println(list);
}
}
4.Set集合
4.1 Set集合特點
不可以存盤重復元素
沒有索引,不能使用普通for回圈遍歷
示例代碼 :
public class SetDemo {
public static void main(String[] args) {
//創建集合物件
Set<String> set = new TreeSet<>();
//添加元素
set.add("ccc");
set.add("aaa");
set.add("aaa");
set.add("bbb");
// for (int i = 0; i < set.size(); i++) {
// //Set集合是沒有索引的,所以不能使用通過索引獲取元素的方法
// }
//遍歷集合
//迭代器
Iterator<String> it = set.iterator();
while (it.hasNext()){
String s = it.next();
System.out.println(s);
}
System.out.println("-----------------------------------");
//增強for
for (String s : set) {
System.out.println(s);
}
}
}
5.TreeSet集合
5.1 TreeSet集合特點
不可以存盤重復元素
沒有索引
可以將元素按照規則進行排序 :
①TreeSet():根據其元素的自然排序進行排序
②TreeSet(Comparator comparator) :根據指定的比較器進行排序
5.2 TreeSet基本使用示例代碼 :
存盤Integer型別的整數并遍歷
public class TreeSetDemo {
public static void main(String[] args) {
//創建集合物件
TreeSet<Integer> ts = new TreeSet<Integer>();
//添加元素
ts.add(10);
ts.add(40);
ts.add(30);
ts.add(50);
ts.add(20);
ts.add(30);
//遍歷集合
for(Integer i : ts) {
System.out.println(i);
}
}
}
5.3 自然排序Comparable的使用 :
-
案例需求
-
存盤學生物件并遍歷,創建TreeSet集合使用無參構造方法
-
要求:按照年齡從小到大排序,年齡相同時,按照姓名的字母順序排序
-
-
實作步驟
-
使用空參構造創建TreeSet集合
用TreeSet集合存盤自定義物件,無參構造方法使用的是自然排序對元素進行排序的 -
自定義的Student類實作Comparable介面
自然排序,就是讓元素所屬的類實作Comparable介面,重寫compareTo(T o)方法 -
重寫介面中的compareTo方法
重寫方法時,一定要注意排序規則必須按照要求的主要條件和次要條件來寫
-
代碼實作 :
學生類 :
public class Student implements Comparable<Student>{
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public int compareTo(Student o) {
//按照物件的年齡進行排序
//主要判斷條件: 按照年齡從小到大排序
int result = this.age - o.age;
//次要判斷條件: 年齡相同時,按照姓名的字母順序排序
result = result == 0 ? this.name.compareTo(o.getName()) : result;
return result;
}
}
測驗類 :
public class TreeSetDemo {
public static void main(String[] args) {
//創建集合物件
TreeSet<Student> ts = new TreeSet<>();
//創建學生物件
Student s1 = new Student("zhangsan",23);
Student s2 = new Student("lisi",24);
Student s3 = new Student("wangwu",25);
Student s4 = new Student("zhaoliu",26);
Student s5 = new Student("qianqi",27);
//把學生添加到集合
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
//遍歷集合
for (Student student : ts) {
System.out.println(student);
}
}
}
5.4 比較器排序Comparator的使用
案例需求
存盤老師物件并遍歷,創建TreeSet集合使用帶參構造方法
要求:按照年齡從小到大排序,年齡相同時,按照姓名的字母順序排序
實作步驟
1. 用TreeSet集合存盤自定義物件,帶參構造方法使用的是比較器排序對元素進行排序的
2. 比較器排序,就是讓集合構造方法接收Comparator的實作類物件,重寫compare(T o1,T o2)方法
3. 重寫方法時,一定要注意排序規則必須按照要求的主要條件和次要條件來寫
代碼實作 :
老師類 :
public class Teacher {
private String name;
private int age;
public Teacher() {
}
public Teacher(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
測驗類 :
public class TreeSetDemo2 {
public static void main(String[] args) {
//創建集合物件
TreeSet<Teacher> ts = new TreeSet<>(new Comparator<Teacher>() {
@Override
public int compare(Teacher o1, Teacher o2) {
//o1表示現在要存入的那個元素
//o2表示已經存入到集合中的元素
//主要條件
int result = o1.getAge() - o2.getAge();
//次要條件
result = result == 0 ? o1.getName().compareTo(o2.getName()) : result;
return result;
}
});
//創建老師物件
Teacher t1 = new Teacher("zhangsan",23);
Teacher t2 = new Teacher("lisi",22);
Teacher t3 = new Teacher("wangwu",24);
Teacher t4 = new Teacher("zhaoliu",24);
//把老師添加到集合
ts.add(t1);
ts.add(t2);
ts.add(t3);
ts.add(t4);
//遍歷集合
for (Teacher teacher : ts) {
System.out.println(teacher);
}
}
}
6.HashSet集合
6.1 HashSet集合的特點
底層資料結構是哈希表
存取無序
不可以存盤重復元素
沒有索引,不能使用普通for回圈遍歷
HashSet基本使用示例代碼 :
public class HashSetDemo {
public static void main(String[] args) {
//創建集合物件
HashSet<String> set = new HashSet<String>();
//添加元素
set.add("hello");
set.add("world");
set.add("java");
//不包含重復元素的集合
set.add("world");
//遍歷
for(String s : set) {
System.out.println(s);
}
}
}
7.Map集合
7.1 Map集合的特點
雙列集合,一個鍵對應一個值
鍵不可以重復,值可以重復
Map集合的基本使用示例代碼:
public class MapDemo01 {
public static void main(String[] args) {
//創建集合物件
Map<String,String> map = new HashMap<String,String>();
//V put(K key, V value) 將指定的值與該映射中的指定鍵相關聯
map.put("itheima001","林青霞");
map.put("itheima002","張曼玉");
map.put("itheima003","王祖賢");
map.put("itheima003","吳彥祖");
//輸出集合物件
System.out.println(map);
}
}
7.2 Map集合方法
| V put(K key,V value) | 添加元素 |
| V remove(Object key) | 根據鍵洗掉鍵值對元素 |
| void clear() | 移除所有的鍵值對元素 |
| boolean containsKey(Object key) | 判斷集合是否包含指定的鍵 |
| boolean containsValue(Object value) | 判斷集合是否包含指定的值 |
| boolean isEmpty() | 判斷集合是否為空 |
| int size() | 集合的長度,也就是集合中鍵值對的個數 |
示例代碼 :
public class MapDemo02 {
public static void main(String[] args) {
//創建集合物件
Map<String,String> map = new HashMap<String,String>();
//V put(K key,V value):添加元素
map.put("張無忌","趙敏");
map.put("郭靖","黃蓉");
map.put("楊過","小龍女");
//V remove(Object key):根據鍵洗掉鍵值對元素
// System.out.println(map.remove("郭靖"));
// System.out.println(map.remove("郭襄"));
//void clear():移除所有的鍵值對元素
// map.clear();
//boolean containsKey(Object key):判斷集合是否包含指定的鍵
// System.out.println(map.containsKey("郭靖"));
// System.out.println(map.containsKey("郭襄"));
//boolean isEmpty():判斷集合是否為空
// System.out.println(map.isEmpty());
//int size():集合的長度,也就是集合中鍵值對的個數
System.out.println(map.size());
//輸出集合物件
System.out.println(map);
}
}
7.3Map集合的獲取功能
| V get(Object key) | 根據鍵獲取值 |
| Set<K> keySet() | 獲取所有鍵的集合 |
| Collection<V> values() | 獲取所有值的集合 |
| Set<Map.Entry<K,V>> entrySet() | 獲取所有鍵值對物件的集合 |
示例代碼 :
public class MapDemo03 {
public static void main(String[] args) {
//創建集合物件
Map<String, String> map = new HashMap<String, String>();
//添加元素
map.put("張無忌", "趙敏");
map.put("郭靖", "黃蓉");
map.put("楊過", "小龍女");
//V get(Object key):根據鍵獲取值
// System.out.println(map.get("張無忌"));
// System.out.println(map.get("張三豐"));
//Set<K> keySet():獲取所有鍵的集合
// Set<String> keySet = map.keySet();
// for(String key : keySet) {
// System.out.println(key);
// }
//Collection<V> values():獲取所有值的集合
Collection<String> values = map.values();
for(String value : values) {
System.out.println(value);
}
}
}
7.4 Map集合的遍歷方式(1)
分析 :
1. 獲取所有鍵的集合,用keySet()方法實作
2. 遍歷鍵的集合,獲取到每一個鍵,用增強for實作
3. 根據鍵去找值,用get(Object key)方法實作
public class MapDemo01 {
public static void main(String[] args) {
//創建集合物件
Map<String, String> map = new HashMap<String, String>();
//添加元素
map.put("張無忌", "趙敏");
map.put("郭靖", "黃蓉");
map.put("楊過", "小龍女");
//獲取所有鍵的集合,用keySet()方法實作
Set<String> keySet = map.keySet();
//遍歷鍵的集合,獲取到每一個鍵,用增強for實作
for (String key : keySet) {
//根據鍵去找值,用get(Object key)方法實作
String value = map.get(key);
System.out.println(key + "," + value);
}
}
}
7.5 Map集合的遍歷方式(2)
分析 :
1. 根據鍵值對物件獲取鍵和值
2. 用getKey()得到鍵
3. 用getValue()得到值
public class MapDemo02 {
public static void main(String[] args) {
//創建集合物件
Map<String, String> map = new HashMap<String, String>();
//添加元素
map.put("張無忌", "趙敏");
map.put("郭靖", "黃蓉");
map.put("楊過", "小龍女");
//獲取所有鍵值對物件的集合
Set<Map.Entry<String, String>> entrySet = map.entrySet();
//遍歷鍵值對物件的集合,得到每一個鍵值對物件
for (Map.Entry<String, String> me : entrySet) {
//根據鍵值對物件獲取鍵和值
String key = me.getKey();
String value = me.getValue();
System.out.println(key + "," + value);
}
}
}
8.HashMap集合
8.1 HashMap集合的特點
HashMap底層是哈希表結構的
依賴hashCode方法和equals方法保證鍵的唯一
如果鍵要存盤的是自定義物件,需要重寫hashCode和equals方法
8.2 HashMap集合應用
示例代碼 :
需求 :
創建一個HashMap集合,鍵是學生物件(Student),值是居住地 (String),存盤多個元素,并遍歷,
要求保證鍵的唯一性:如果學生物件的成員變數值相同,我們就認為是同一個物件
學生類 :
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
if (age != student.age) return false;
return name != null ? name.equals(student.name) : student.name == null;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + age;
return result;
}
}
測驗類 :
public class HashMapDemo {
public static void main(String[] args) {
//創建HashMap集合物件
HashMap<Student, String> hm = new HashMap<Student, String>();
//創建學生物件
Student s1 = new Student("林青霞", 30);
Student s2 = new Student("張曼玉", 35);
Student s3 = new Student("王祖賢", 33);
Student s4 = new Student("王祖賢", 33);
//把學生添加到集合
hm.put(s1, "西安");
hm.put(s2, "武漢");
hm.put(s3, "鄭州");
hm.put(s4, "北京");
//遍歷集合
Set<Student> keySet = hm.keySet();
for (Student key : keySet) {
String value = hm.get(key);
System.out.println(key.getName() + "," + key.getAge() + "," + value);
}
}
}
9.TreeMap集合
9.1TreeMap集合的特點
TreeMap底層是紅黑樹結構
依賴自然排序或者比較器排序,對鍵進行排序
如果鍵存盤的是自定義物件,需要實作Comparable介面或者在創建TreeMap物件時候給出比較器排序規則
9.2 TreeMap集合應用
需求 :
創建一個TreeMap集合,鍵是學生物件(Student),值是籍貫(String),學生屬性姓名和年齡,按照年齡進行排序并遍歷
要求按照學生的年齡進行排序,如果年齡相同則按照姓名進行排序
學生類 :
public class Student implements Comparable<Student>{
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public int compareTo(Student o) {
//按照年齡進行排序
int result = o.getAge() - this.getAge();
//次要條件,按照姓名排序,
result = result == 0 ? o.getName().compareTo(this.getName()) : result;
return result;
}
}
測驗類 :
public class TreeMapDemo {
public static void main(String[] args) {
// 創建TreeMap集合物件
TreeMap<Student,String> tm = new TreeMap<>();
// 創建學生物件
Student s1 = new Student("xiaohei",23);
Student s2 = new Student("dapang",22);
Student s3 = new Student("xiaomei",22);
// 將學生物件添加到TreeMap集合中
tm.put(s1,"江蘇");
tm.put(s2,"北京");
tm.put(s3,"天津");
// 遍歷TreeMap集合,列印每個學生的資訊
tm.forEach(
(Student key, String value)->{
System.out.println(key + "---" + value);
}
);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/294345.html
標籤:java
