- List集合序列排序方法
①集合中是簡單整型
ArrayList<Integer> nums = new ArrayList<>();
nums.add(1);
nums.add(3);
nums.add(2);
Collections.sort(nums);
輸出為:1 2 3
②集合中為對象
public static void main(String[] args) {
ArrayList<ProductBean> list = new ArrayList<>();
list.add(new ProductBean("b", 2));
list.add(new ProductBean("a", 1));
list.add(new ProductBean("c", 3));
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getName() + ":" + list.get(i).getPrice());
}
Collections.sort(list, new Comparator<ProductBean>() {
@Override
public int compare(ProductBean o1, ProductBean o2) {
return o2.getPrice().compareTo(o1.getPrice());
}
});
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getName() + ":" + list.get(i).getPrice());
}
}
實體類要實現(xiàn)Comparable接口
public class ProductBean implements Comparable<ProductBean> {
private String name;
private Integer price;
public ProductBean(String name, Integer price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
@Override
public int compareTo(ProductBean o) {
return this.price.compareTo(o.price);
}
}
- 集合中順序打亂
ArrayList<String> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
Collections.shuffle(list);
- 集合中隨機獲取一個下標缺厉,該方法隨機出來的整型是左閉右開的
randomPos = (int) (Math.random() * list.size());
- 集合中隨機獲取部分數(shù)據(jù)
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
Collections.shuffle(list);
list = list.subList(0, 2);
最后的list大小是2永高,可能是123中的任意兩個
一隧土、集合概述
1. 集合的由來
面向?qū)ο笳Z言對事物的體現(xiàn)都是以對象的形式,所以為了方便對多個對象的操作命爬,Java就提供了集合類曹傀。
2. 集合和數(shù)組有什么區(qū)別
- 集合長度是可變的,數(shù)組長度是不可變的。
- 數(shù)組中可以存儲基本數(shù)據(jù)類型和引用類型饲宛,集合只能存儲對象
- 同一個數(shù)組只能存儲一種引用類型數(shù)據(jù)皆愉,同一個集合可以存儲不同引用數(shù)據(jù)類型(不建議,遍歷不方便)
3. 集合的特點
集合只用于存儲對象,集合長度是可變的落萎,集合可以存儲不同類型的對象亥啦。
4.集合繼承體系
Java.util 包中提供了一些集合類,這些集合類又被稱為容器练链,提到容器不難想到數(shù)組翔脱,集合類與數(shù)組的不同之處是,數(shù)組的長度是固定的媒鼓,集合的長度是可變的届吁;數(shù)組用來存放基本數(shù)據(jù)類型和引用類型數(shù)據(jù);集合用來存放引用數(shù)據(jù)類型绿鸣;常用的集合有List集合疚沐、Set集合和Map集合,其中分為兩類潮模,單列和雙列亮蛔。單列集合的頂層接口是Collection。List接口和set接口繼承Collection擎厢。
?List和set分別提供了實現(xiàn)類ArrayList究流、LinkedList、Vector动遭,HashSet芬探、TreeSet、LinkedHashSet等實現(xiàn)類體系厘惦。Map是雙列集合偷仿,鍵值對的形式存儲數(shù)據(jù),Map是一個集合頂層接口宵蕉,常用實現(xiàn)類HashMap酝静,TreeMap,LinkedHashMap等體系羡玛。
二形入、Collection接口
Collection接口概述及方法
Collection 層次結構中的根接口。Collection 表示一組對象缝左,這些對象也稱為 collection 的元素亿遂。一些 collection 允許有重復的元素浓若,而另一些則不允許。一些 collection 是有序的蛇数,而另一些則是無序的挪钓。
因為 Collection 是一個接口,在使用它的方法的時候耳舅,選擇其中一個實現(xiàn)類ArrayList來做演示
Collection 接口中的常用方法
1. 添加:
// 向集合中添加一個元素
// e表示要向集合中添加的對象, 添加成功返回true, 添加失敗返回false
boolean add(E e)
案例:
Collection coll = new ArrayList<>();//創(chuàng)建集合
coll.add("hello");//給集合添加一個元素
coll.add("world");//給集合再添加一個元素
System.out.println(coll);// 顯示 [hello, world]
2. 移除:
// 移除集合中指定的元素
// o 表示要從集合中刪除的對象 刪除成功返回true 返回失敗(集合中把本來就沒有這個對象)發(fā)揮false
boolean remove(Object o)
Collection coll = new ArrayList<>();//創(chuàng)建集合
coll.add("hello");//給集合 添加一個元素
coll.add("world");//給集合再添加一個元素
System.out.println(coll);// 顯示 [hello, world]
coll.remove("hello");// 刪除 集合中 hello 這個//元素
System.out.println(coll);// [world]
3. 清除
//清除集合中的元素, 集合會還原回剛創(chuàng)建的狀態(tài)
void clear()
案例:
Collection coll = new ArrayList<>();//創(chuàng)建集合
coll.add("hello");//給集合 添加一個元素
coll.add("world");//給集合再添加一個元素
System.out.println(coll);// 顯示 [hello, world]
coll.clear();;// 刪除 集合中所有元素
System.out.println(coll);// [] 所有數(shù)據(jù)都刪除了
4. 是否包含
//集合中是否包含該元素
//判斷集合中是否包含o對象 如果集合中含有o對象,返回true; 如果不含有o對象,返回false
boolean contains(Object o)
案例:
Collection coll = new ArrayList<>();//創(chuàng)建一個集合
coll.add("hello");//給集合中 添加一個元素
coll.add("world");// 給集合再 添加一個元素
System.out.println(coll);// [hello,world]
boolean contains1 = coll.contains("hello");//集合中是否包含該元素
System.out.println(contains1);//true 包含
boolean contains2 = coll.contains("張三");//集合中是否包含該元素
System.out.println(contains2);//false 不包含
5. 集合是否為空
// 判斷集合是否為空, 如果集合中沒有數(shù)據(jù)返回true, 如果有數(shù)據(jù)返回false
boolean isEmpty()
案例:
Collection coll = new ArrayList<>();//創(chuàng)建一個集合
System.out.println(coll.isEmpty());// false 集合 沒有元素
coll.add("hello");//給集合中 添加一個元素
System.out.println(coll.isEmpty());// true 集合有元素 不為 空
6. 集合大小
// 獲取集合存有幾個元素 如果集合中存儲有5個元素,則返回5
int size()
案例:
Collection coll = new ArrayList<>();//創(chuàng)建一個集合
System.out.println(coll.size());// 0 集合 沒有元素
coll.add("hello");//給集合中 添加一個元素
System.out.println(coll.size());// 1 集合有1個元素
coll.add("world");//給集合中 添加一個元素
System.out.println(coll.size());// 2 集合有2個元素
7. 將該集合中的元素添加到另一個集合中
//將該集合中的元素添加到另一個集合中
//把集合c中的所有數(shù)據(jù)加入到集合中, 添加成功返回true 添加失敗返回false
boolean addAll(Collection c)
案例:
Collection coll = new ArrayList<>();//創(chuàng)建一個集合
coll.add("hello");//給集合中 添加一個元素
coll.add("world");// 給集合 添加一個元素
Collection coll2 = new ArrayList<>();//創(chuàng)建一個集合
System.out.println(coll2);//[]
coll2.add(coll); // 將 集合 coll 中的元素 都添加到 集合 coll2中
System.out.println(coll2);//[[hello, world]] 把集合coll的元素都添加進入了coll2
8. 移除另一個集合中包含該集合的元素
//移除另一個集合中包含該集合的元素
//如果本集合中有的對象,另外一個集合中也存在,則把這些對象刪除, 如果刪除了數(shù)據(jù)返回true; 沒有刪除數(shù)據(jù)返回false
boolean removeAll(Collection c)
案例:
Collection coll = new ArrayList<>();//創(chuàng)建一個集合
coll.add("hello");//給集合中 添加一個元素
coll.add("world");// 給集合 添加一個元素
System.out.println("coll: "+coll);
Collection coll2 = new ArrayList<>();//創(chuàng)建一個集合
coll2.add("hello");//給集合中 添加一個元素
coll2.add("world");// 給集合 添加一個元素
coll2.add("java");// 給集合 添加一個元素
System.out.println("coll2: "+coll2);
coll2.removeAll(coll); //從集合 coll2 中 移除 與 集合 coll 相同的元素
System.out.println("coll2刪除以后: "+coll2);//[java]
9.另一個集合是否包含指定的集合
//另一個集合是否包含指定的集合
//判斷集合中是否包含有c集合中的所有元素,如果包含返回true,否則返回false
boolean containsAll(Collection c)
案例:
Collection coll = new ArrayList<>();//創(chuàng)建一個集合
coll.add("hello");//給集合中 添加一個元素
coll.add("world");// 給集合 添加一個元素
Collection coll2 = new ArrayList<>();//創(chuàng)建一個集合
coll2.add("hello");//給集合中 添加一個元素
coll2.add("world");// 給集合 添加一個元素
coll2.add("java");// 給集合 添加一個元素
boolean b = coll2.containsAll(coll); //集合 coll2 是否包含 集合 coll
System.out.println(b);//true 包含
10. 移除 集合中與指定集合不同的元素
// 移除 集合中與指定集合不同的元素
//找到兩個集合中不相同的元素刪除 有刪除的數(shù)據(jù)返回true ,否則為false
boolean retainAll(Collection c)
案例:
Collection coll = new ArrayList<>();//創(chuàng)建一個集合
coll.add("hello");//給集合中 添加一個元素
coll.add("world");// 給集合 添加一個元素
Collection coll2 = new ArrayList<>();//創(chuàng)建一個集合
coll2.add("hello");//給集合中 添加一個元素
coll2.add("world");// 給集合 添加一個元素
coll2.add("java");// 給集合 添加一個元素
coll2.retainAll(coll); // 移除 集合 coll2 與 集合 coll 不同的元素
System.out.println(coll2);//[hello, world]
11. 把集合轉(zhuǎn)成數(shù)組碌上,可以實現(xiàn)集合的遍歷
//把集合轉(zhuǎn)成數(shù)組,可以實現(xiàn)集合的遍歷
//把集合轉(zhuǎn)換成存儲相同元素的數(shù)組
Object[] toArray()
案例:
Collection coll = new ArrayList<>();//創(chuàng)建一個集合
coll.add("hello");//給集合中 添加一個元素
coll.add("world");// 給集合 添加一個元素
Object[] array = coll.toArray(); // 轉(zhuǎn)成 Object 數(shù)組
for (int i = 0; i < array.length; i++) {
// 存入的是 字符串
// 所有要將 object 轉(zhuǎn)成 string
System.out.println((String)array[i]);
}
三浦徊、Iterator接口
1. Iterator接口概述及使用
迭代器馏予,集合的專用遍歷方式,對 collection 進行迭代的迭代器盔性,依賴于集合而存在
Iterator接口的成員方法:
//如果仍有元素可以迭代霞丧,則返回 true。
boolean hasNext():
//獲取元素,并移動到下一個位置冕香。
Object next():
//沒有這樣的元素蛹尝,因為你已經(jīng)找到最后了。
NoSuchElementException():
實際使用案例:
Collection coll = new ArrayList<>();//創(chuàng)建一個集合
coll.add("hello");//給集合中 添加一個元素
coll.add("world");// 給集合 添加一個元素
//獲得迭代器
Iterator iterator = coll.iterator();
while(iterator.hasNext()){// 如果有下一個元素
Object next = iterator.next();// 獲取下一個元素
// 存入的是 字符串
// 所有要將 object 轉(zhuǎn)成 string
System.out.println((String)next);
}
四悉尾、List接口
List接口概述及方法
有序的 collection(也稱為序列)突那。此接口的用戶可以對列表中每個元素的插入位置進行精確地控制。用戶可以根據(jù)元素的整數(shù)索引(在列表中的位置)訪問元素构眯,并搜索列表中的元素愕难。與 set 不同,列表通常允許重復的元素惫霸,存取元素有序务漩。
List接口的特殊成員方法(根據(jù)索引進行操作)
1. 添加
//添加指定元素 把要添加進集合的element對象,添加到指定索引位置index
void add(int index,E element)
案例:
List coll = new ArrayList<>();//創(chuàng)建一個集合
coll.add("hello");//給集合中 添加一個元素
coll.add("world");// 給集合 添加一個元素
System.out.println(coll);// [hello, world]
2.移除指定元素
// 移除指定元素 刪除集合中指定index位置處元素對象
E remove(int index)
案例:
List coll = new ArrayList<>();//創(chuàng)建一個集合
coll.add("hello");//給集合中 添加一個元素
coll.add("world");// 給集合 添加一個元素
coll.remove(1);// 移除 1 這個位置的 元素,集合元素也是從 0 開始計算索引
System.out.println(coll);//[hello]
3. 獲取 指定位置的元素
// 獲取 指定位置的元素
//index 集合索引值,從0開始,不能大于等于集合中元素個數(shù)
E get(int index)
案例:
List coll = new ArrayList<>();//創(chuàng)建一個集合
coll.add("hello");//給集合中 添加一個元素
coll.add("world");// 給集合 添加一個元素
System.out.println(coll.get(1));//world 獲取指定位置的元素
3. 修改指定位置的元素
// 修改指定位置的元素
// index 要修改元素的索引值
// element 此對象會替換掉原來集合中制定索引位置的元素對象
E set(int index,E element)
案例:
List coll = new ArrayList<>();//創(chuàng)建一個集合
coll.add("hello");//給集合中 添加一個元素
coll.add("world");// 給集合 添加一個元素
System.out.println(coll);//[hello, world]
coll.set(1, "java");// 把 1 位置的 元素 設置成 java
System.out.println(coll);//[hello, java]
4. 獲得專用迭代器
// 獲得專用迭代器
ListIterator listIterator()
案例:
List coll = new ArrayList<>();//創(chuàng)建一個集合
coll.add("hello");//給集合中 添加一個元素
coll.add("world");// 給集合 添加一個元素
ListIterator listIterator = coll.listIterator();//獲取 迭代器
while(listIterator.hasNext()){// 如果有下一個 返回 true
//獲取下一個元素
Object next = listIterator.next();
System.out.println((String)next);
}
五它褪、ListIterator接口
1. ListIterator概述及使用
系列表迭代器,允許程序員按任一方向遍歷列表翘悉、迭代期間修改列表茫打,并獲得迭代器在列表中的當前位置。
ListIterator接口的成員方法:
// 是否有上一個元素
boolean hasPrevious()
// 獲取上一個元素
E previous()
使用以上兩個方法時 先使用 迭代器 正向遍歷(使位置移動到 最后一個), 無法循環(huán)獲取 因為位置就在第一個的前面
六妖混、ConcurrentModificationException(異常)簡介及處理
1. 簡介及處理
ConcurrentModificationException: 不允許這種修改時老赤,拋出此異常
?*為什么出現(xiàn)此異常:
在迭代時候, 使用集合對象去修改集合中的元素出現(xiàn)的這種異常;
在迭代過程中 修改集合元素時 迭代器不知道集合被修改發(fā)生錯誤制市。
ArrayList<Integer> list=new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
//把指針移到集合最后
ListIterator<Integer> it = list.listIterator();
list.add(7);//當獲取listIterator對象后修改了集合
while(it.hasNext()){
//如果再使用it對象獲取元素,導致ConcurrentModificationException異常
System.out.println(it.next());
}
運行效果:
如何去解決此異常:
使用 ListIterator 迭代 集合 并且 使用 ListIterator 對象去修改 集合元素
ListIterator 提供了 對應的 添加方法
ArrayList<Integer> list=new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
//把指針移到集合最后
ListIterator<Integer> it = list.listIterator();
// list.add(7);
while(it.hasNext()){
System.out.println(it.next());
}
it.add(7);//
//逆向遍歷列表
while(it.hasPrevious()){
System.out.println(it.previous());
}
八抬旺、ArrayList 類
1. ArrayList類的概述
底層數(shù)據(jù)結構是數(shù)組,查詢快祥楣,增刪慢
線程不安全开财,效率高汉柒,有序的,可重復
2. ArrayList的使用
構造方法摘要 :
//構造一個初始容量為 10 的空列表。
ArrayList()
//構造一個包含指定 collection 的元素的列表责鳍,這些元素是按照該 collection 的迭代器返回它們的順序排列的碾褂。
ArrayList(Collection<? extends E> c)
//構造一個具有指定初始容量的空列表。
ArrayList(int initialCapacity)
常用方法參考List(使用方式幾乎一致)
//創(chuàng)建 ArrayList 集合 對象
ArrayList arr = new ArrayList();
arr.add("hello1");// 給集合添加一個元素
arr.add("hello2");// 給集合添加一個元素
3. ArrayList的常用案例-添加整數(shù)遍歷集合
ArrayList<Integer> list=new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
//把指針移到集合最后
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
4. ArrayList的常用案例-添加字符串遍歷集合
ArrayList<String> list=new ArrayList<>();
list.add("張三");
list.add("李四");
list.add("王五");
list.add("趙六");
//把指針移到集合最后
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
5. ArrayList的常用案例-添加對象遍歷集合
Student.java
public class Student {
private String name;
private int age;
private String sex;
public Student(String name, int age, String sex) {
super();
this.name = name;
this.age = age;
this.sex = sex;
}
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;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
}
Test.java
public class Test {
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<Student>();
list.add(new Student("張三", 18, "男"));
list.add(new Student("李四", 11, "女"));
list.add(new Student("王五", 19, "男"));
list.add(new Student("趙六", 21, "女"));
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
九历葛、Vector 類
1. Vector類概述及使用
底層數(shù)據(jù)結構是數(shù)組正塌,查詢快,增刪慢, 線程安全恤溶,效率低
*Vector類特有功能 :
1. 添加
//添加一個元素
//把obj對象添加進入集合 等效add(E e)方法
public void addElement(E obj)
案例
//創(chuàng)建 Vector 集合 對象 在集合中存入 字符串
Vector v = new Vector();
System.out.println(v);//[]
v.add("hello");
System.out.println(v);//[hello]
2. 獲取指定位置的元素
//獲取指定位置的元素
//index 制定位置索引值
//返回值為得到制定索引位置的集合元素
public E elementAt(int index)
案例
//創(chuàng)建 Vector 集合 對象 在集合中存入 字符串
Vector v = new Vector();
v.add("hello");
v.add("world");
v.add("java");
// 獲取 1 這個位置的 元素 并輸出在控制臺
System.out.println(v.get(1));//world
3. 獲取指定位置的元素
// 獲取Vector 迭代器
public Enumeration elements()
案例
//創(chuàng)建 Vector 集合 對象 在集合中存入 字符串
Vector v = new Vector();
v.add("hello");
v.add("world");
v.add("java");
//獲取迭代器
Enumeration elements = v.elements();
//有沒有下一個元素 與 ListIterator 用法一樣
while(elements.hasMoreElements()){
// 獲取下一個元素
Object nextElement = elements.nextElement();
System.out.println((String)nextElement);
}
*Vector的常用案例:
案例1:Vector存入String對象
// 創(chuàng)建 Vector 集合 對象 在集合中存入 字符串
Vector v = new Vector();
v.add("hello");
v.add("world");
v.add("java");
// 普通for遍歷
for (int i = 0; i < v.size(); i++) {
// 多態(tài) 向下轉(zhuǎn)型
System.out.println((String) v.get(i));
}
案例2:Vector存入自定義對象
Vector v = new Vector();
v.add(new Student("趙云",18,"男"));// 給集合添加一個元素
v.add(new Student("張飛",19,"女"));// 給集合添加一個元素
v.add(new Student("關于",20,"男"));// 給集合添加一個元素
v.add(new Student("劉備",21,"女"));// 給集合添加一個元素
//普通for遍歷 集合
for (int i = 0; i < v.size(); i++) {
//獲取的時候 獲取的是 object對象 父類接收子類 多態(tài)
Object object = v.get(i);// 集合中的一個元素 是一個 Student 對象
//多態(tài) 向下轉(zhuǎn)型
Student s = (Student)object;
System.out.println(s.getName()+"---"+s.getAge());
}
十乓诽、 LinkedList類
LinkedList類概述
底層數(shù)據(jù)結構是鏈表,查詢慢咒程,增刪快
線程不安全鸠天,效率高
LinkedList類的特有功能
1. 在集合的開始位置插入一條數(shù)據(jù)
//在集合的開始位置插入一條數(shù)據(jù)
//e表示要插入集合最開始位置的元素
public void addFirst(E e)
//在集合的末尾添加元素
//e表示要添加進入集合中的元素
addLast(E e)
案例
//創(chuàng)建 LinkedList 對象
LinkedList link = new LinkedList();
link.add("hello");//添加一個元素
link.add("world");
System.out.println(link);//[hello, world]
link.addFirst("java");// 在第一個位置插入 一個元素
System.out.println(link);//[java, hello, world]
link.addLast("lvoe");//在集合最后的位置加入一個 元素
System.out.println(link);//[java, hello, world, lvoe]
2. 獲取集合的第一個元素
//獲取集合的第一個元素
public E getFirst()
//獲取集合的最后一個元素
public E getLast()
案例
//創(chuàng)建 LinkedList 對象
LinkedList link = new LinkedList();
link.add("hello");//添加一個元素
link.add("world");
link.add("java");
//獲取集合中 第一個元素 hello
System.out.println(link.getFirst());
//獲取集合中 最后一個元素 java
System.out.println(link.getLast());
3. 獲取集合的第一個元素
//刪除集合中的第一個元素
public E removeFirst()
//刪除集合中的最后一個元素
public E removeLast()
案例
//創(chuàng)建 LinkedList 對象
LinkedList link = new LinkedList();
link.add("hello");//添加一個元素
link.add("world");
link.add("java");
//移除第一個元素 并得到該元素
Object removeFirst = link.removeFirst(); System.out.println(removeFirst);// hello
System.out.println(link);// [world, java]
//移除最后一個元素 并得到該元素
Object removeLast = link.removeLast(); System.out.println(removeLast);//java
System.out.println(link);//[world]
案例一、LinkedList類的常用案例-首尾添加
LinkedList<Integer> list = new LinkedList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.addFirst(0);//在最前面添加
list.addLast(5);//在最后添加 等價add()方法
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
案例二孵坚、 LinkedList類的常用案例-指定位置插
LinkedList<Integer> list = new LinkedList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(1,12);//在索引為1的位置插入一個12
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
案例三粮宛、 LinkedList類的常用案例-首尾刪除
LinkedList<Integer> list = new LinkedList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
System.out.println("-------------刪除以后效果------------------");
list.removeFirst();//刪除第一個元素
list.removeLast();//刪除最后一個元素
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}