# 集合: **总结:** image-20250417165922953 其中Collection接口中又分为**set**接口和**list**接口 ## Collection: image-20250417165713561 **注意事项:** remove and contains方法依赖于底层equals方法 ## 集合通用遍历方法: ### 1.迭代器 2.增强for循环 3.forEach 首先获取迭代器: `Iterator 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 c = new ArrayList<>(); c.add(new Student("小鸟游六花",15)); c.add(new Student("小鸟游七花",15)); c.add(new Student("小鸟游八花",15)); //迭代器循环 Iterator 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集合特点以及方法 image-20250425155604336 ## 并发修改异常: image-20250425154628294 image-20250425155717100 ## List集合的5种遍历方式 1. Iterator迭代器 image-20251221153329358 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迭代器 # 数据结构:栈 队列 链表 数组 image-20250425163323615 ## 其中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集合 image-20251220175506487 ## 数据结构:树 树的知识点总结: ![image-20251012170947546](https://gitee.com/icecat2233/picture/raw/master/20251012171010346.png) ## 三种二叉树 ![image-20250509183934916](https://gitee.com/icecat2233/picture/raw/master/20250509183936223.png) ## 平衡二叉树 image-20250509183446340 ## 红黑树(重难点) 红黑树是一种特殊的平衡二叉树,但并不是完全平衡的平衡二叉树,效率最高 **添加树的情况** ![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 降序** image-20250510170600150 image-20250510170513793 两种方式比较: image-20250510170052265 # HashSet ## 基本运用: 特点:去重(hashCode方法和equals方法共同运用) 遍历:迭代器,增强for循环,foreach方法 ## **HashSet组成:** image-20251220165000000 ## **JDK8版本以后Hashset的添加过程(面试常问重点!)** ### **2**步骤过程: **将数值调用hashCode方法得到原始哈希值,然后将原始哈希值向右移动16位与原始哈希值进行异或操作,得到的数值与数组长度作取模运算得到位置。** image-20251220165202216 ## 如何提高HashSet查询过程: image-20251220165543271 ## 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 alist = new TreeSet<>(); Collections.addAll(alist,new Student("张三",14),new Student("八奈见杏菜",16),new Student("七奈见杏菜",16)); System.out.println(alist); //二分查找 ArrayList ts = new ArrayList<>(); Collections.addAll(ts,"2","3","4"); System.out.println(Collections.binarySearch(ts, "2")); //洗牌 ArrayList 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集合: image-20251220201801335 ## 常用API: image-20251220201929102 ## MAP特点: ![image-20251220202017714](https://gitee.com/icecat2233/picture/raw/master/20251220202019112.png) ![image-20251220203224248](https://gitee.com/icecat2233/picture/raw/master/20251220203226325.png) ## Map的三种遍历方式 ### 1:entrySet方法 image-20251220205848087 ```java HashMap m = new HashMap<>(); m.put("杏菜", "15岁"); m.put("佳树", "14岁"); m.put("温水", "15岁"); m.put("梦子", "15岁"); //用entrySet方法获取到所有键值对象 Set> entrySet = m.entrySet(); //遍历set集合获取到每一个键值对儿对象 for (Map.Entry stringStringEntry : entrySet) { //通过键值对对象获取每一个数值 System.out.println(stringStringEntry.getKey() + "---" + stringStringEntry.getValue()); } ``` ### 2:foreach方法: ```Java hs.forEach(new BiConsumer>() { @Override public void accept(String key, List 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)