Files
Java-Basic-and-Advanced/集合-数据结构相关.md
2026-01-07 16:14:38 +08:00

308 lines
9.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 集合:
**总结:**
<img src="https://gitee.com/icecat2233/picture/raw/master/20250417165937735.png" alt="image-20250417165922953" style="zoom:50%;" />
其中Collection接口中又分为**set**接口和**list**接口
## Collection:
<img src="https://gitee.com/icecat2233/picture/raw/master/20250417165721995.png" alt="image-20250417165713561" style="zoom:50%;" />
**注意事项:**
remove and contains方法依赖于底层equals方法
## 集合通用遍历方法:
### 1.迭代器 2.增强for循环 3.forEach
首先获取迭代器:
`Iterator<Student> it = c.iterator();`
其次判断循环是否还有元素
`hasNext()方法` `next()方法`
注意next()方法只调用一次,要不然会引起指针异常
### 迭代器源码解析:
![image-20250420172303418](https://gitee.com/icecat2233/picture/raw/master/20250420172317333.png)
### **三种方法演示:**
```java
public class Test2 {
public static void main(String[] args) {
Collection<Student> c = new ArrayList<>();
c.add(new Student("小鸟游六花",15));
c.add(new Student("小鸟游七花",15));
c.add(new Student("小鸟游八花",15));
//迭代器循环
Iterator<Student> it = c.iterator();
while (it.hasNext()){
Student stu = it.next();
System.out.println(stu.getName()+"---"+stu.getAge());
}
//增强for循环
for (Student stu:c){
System.out.println(stu);
}
//forEach遍历
c.forEach(student -> System.out.println(student));
}
}
```
# List集合
## list集合特点以及方法
<img src="https://gitee.com/icecat2233/picture/raw/master/20250425155619805.png" alt="image-20250425155604336" style="zoom:67%;" />
## 并发修改异常:
<img src="https://gitee.com/icecat2233/picture/raw/master/20250425154636875.png" alt="image-20250425154628294" style="zoom: 80%;" />
<img src="https://gitee.com/icecat2233/picture/raw/master/20250425155719001.png" alt="image-20250425155717100" style="zoom:80%;" />
## List集合的5种遍历方式
1. Iterator迭代器
<img src="https://gitee.com/icecat2233/picture/raw/master/20251221153337614.png" alt="image-20251221153329358" style="zoom:50%;" />
2. 增强for循环
```Java
//增强for循环
//for(元素类型,起名 : 遍历的集合)
for (Student stu : c) {
System.out.println(stu);
}
```
3. foreach
```Java
//foreach循环
c.forEach(stu -> System.out.println(stu));
```
4. 普通for循环
5. ListIterator迭代器
# 数据结构:栈 队列 链表 数组
<img src="https://gitee.com/icecat2233/picture/raw/master/20250425163326284.png" alt="image-20250425163323615" style="zoom: 67%;" />
## 其中Arraylist集合扩容机制为
1. 在初始化时长度为0
2. 调用添加方法add时长度为10
3. 此后长度超过10时会将数组长度增加原来的1/2
#### **源码分析****此为长度还未超出10时的源码过程**
![image-20250425165520793](https://gitee.com/icecat2233/picture/raw/master/20250425165535349.png)
#### **此为超出10时源码过程**
![image-20250425165743512](https://gitee.com/icecat2233/picture/raw/master/20250425170258008.png)
## LinkedList特有方法
removeFirst() removeLast()
# Set集合
<img src="https://gitee.com/icecat2233/picture/raw/master/20251220175508010.png" alt="image-20251220175506487" style="zoom:67%;" />
## 数据结构:树
树的知识点总结:
![image-20251012170947546](https://gitee.com/icecat2233/picture/raw/master/20251012171010346.png)
## 三种二叉树
![image-20250509183934916](https://gitee.com/icecat2233/picture/raw/master/20250509183936223.png)
## 平衡二叉树
<img src="https://gitee.com/icecat2233/picture/raw/master/20250509183513481.png" alt="image-20250509183446340" style="zoom: 50%;" />
## 红黑树(重难点)
红黑树是一种特殊的平衡二叉树,但并不是完全平衡的平衡二叉树,效率最高
**添加树的情况**
![image-20250509201524220](https://gitee.com/icecat2233/picture/raw/master/20250509201525478.png)
## TreeSet集合
**排序和取出顺序示例:**
![image-20251217162402752](https://gitee.com/icecat2233/picture/raw/master/20251217162411298.png)
**自然排序示例:**
![image-20250510170405367](https://gitee.com/icecat2233/picture/raw/master/20250510170406417.png)
比较器排序示例:
### **注意:**
**this - 0 正序 o-this 降序**
**o1 - o2 正序o2 - o1 降序**
<img src="https://gitee.com/icecat2233/picture/raw/master/20250510170601190.png" alt="image-20250510170600150" style="zoom: 67%;" />
<img src="https://gitee.com/icecat2233/picture/raw/master/20250510170515008.png" alt="image-20250510170513793" style="zoom:50%;" />
两种方式比较:
<img src="https://gitee.com/icecat2233/picture/raw/master/20250510170101423.png" alt="image-20250510170052265" style="zoom:67%;" />
# HashSet
## 基本运用:
特点去重hashCode方法和equals方法共同运用
遍历迭代器增强for循环foreach方法
## **HashSet组成**
<img src="https://gitee.com/icecat2233/picture/raw/master/20251220165008373.png" alt="image-20251220165000000" style="zoom:67%;" />
## **JDK8版本以后Hashset的添加过程面试常问重点**
### **2**步骤过程:
**将数值调用hashCode方法得到原始哈希值然后将原始哈希值向右移动16位与原始哈希值进行异或操作得到的数值与数组长度作取模运算得到位置。**
<img src="https://gitee.com/icecat2233/picture/raw/master/20251220165204636.png" alt="image-20251220165202216" style="zoom:67%;" />
## 如何提高HashSet查询过程
<img src="https://gitee.com/icecat2233/picture/raw/master/20251220165544826.png" alt="image-20251220165543271" style="zoom:67%;" />
## LinkedHashSet集合
## 特点:
**有序**,不重复,无索引,保证存取顺序。
# 集合总结:
![image-20251220171433114](https://gitee.com/icecat2233/picture/raw/master/20251220171435072.png)
# 小知识点:可变参数
可变参数用在形参中可以接受多个数据。
例:`int... 名称`
传输参数非常灵活方便可以不传参数可以传1个或多个也可以传数组
**方法本质就是一个数组**
# Collections集合工具类
![image-20251220173953237](https://gitee.com/icecat2233/picture/raw/master/20251220173954809.png)
![image-20251220174214148](https://gitee.com/icecat2233/picture/raw/master/20251220174215755.png)
示例:
```java
//多数据添加
TreeSet<Student> alist = new TreeSet<>();
Collections.addAll(alist,new Student("张三",14),new Student("八奈见杏菜",16),new Student("七奈见杏菜",16));
System.out.println(alist);
//二分查找
ArrayList<String> ts = new ArrayList<>();
Collections.addAll(ts,"2","3","4");
System.out.println(Collections.binarySearch(ts, "2"));
//洗牌
ArrayList<Student> blist = new ArrayList<>();
Collections.shuffle(blist);
System.out.println(alist);
//从集合中找最值
System.out.println(Collections.max(ts));
System.out.println(Collections.min(ts));
System.out.println(Collections.min(alist));
System.out.println(Collections.max(alist));
```
# MAP集合
<img src="https://gitee.com/icecat2233/picture/raw/master/20251220201802981.png" alt="image-20251220201801335" style="zoom:67%;" />
## 常用API
<img src="https://gitee.com/icecat2233/picture/raw/master/20251220201935518.png" alt="image-20251220201929102" style="zoom:67%;" />
## MAP特点
![image-20251220202017714](https://gitee.com/icecat2233/picture/raw/master/20251220202019112.png)
![image-20251220203224248](https://gitee.com/icecat2233/picture/raw/master/20251220203226325.png)
## Map的三种遍历方式
### 1entrySet方法
<img src="https://gitee.com/icecat2233/picture/raw/master/20251220205849706.png" alt="image-20251220205848087" style="zoom:50%;" />
```java
HashMap<String, String> m = new HashMap<>();
m.put("杏菜", "15岁");
m.put("佳树", "14岁");
m.put("温水", "15岁");
m.put("梦子", "15岁");
//用entrySet方法获取到所有键值对象
Set<Map.Entry<String, String>> entrySet = m.entrySet();
//遍历set集合获取到每一个键值对儿对象
for (Map.Entry<String, String> stringStringEntry : entrySet) {
//通过键值对对象获取每一个数值
System.out.println(stringStringEntry.getKey() + "---" + stringStringEntry.getValue());
}
```
### 2foreach方法
```Java
hs.forEach(new BiConsumer<String, List<String>>() {
@Override
public void accept(String key, List<String> value) {
System.out.print(key+"=");
List list = value;
for (int i = 0; i < list.size()-1; i++) {
System.out.print(list.get(i)+",");
}
System.out.println(list.get(value.size()-1));
}
});
```
### 3:调用keyset方法获取所有键值
![image-20251221174612093](https://gitee.com/icecat2233/picture/raw/master/20251221174613322.png)