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

9.0 KiB
Raw Blame History

集合:

总结:

image-20250417165922953

其中Collection接口中又分为set接口和list接口

Collection:

image-20250417165713561

注意事项:

remove and contains方法依赖于底层equals方法

集合通用遍历方法:

1.迭代器 2.增强for循环 3.forEach

首先获取迭代器:

Iterator<Student> it = c.iterator();

其次判断循环是否还有元素

hasNext()方法 next()方法

注意next()方法只调用一次,要不然会引起指针异常

迭代器源码解析:

image-20250420172303418

三种方法演示:

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集合特点以及方法

image-20250425155604336

并发修改异常:

image-20250425154628294 image-20250425155717100

List集合的5种遍历方式

  1. Iterator迭代器

    image-20251221153329358
  2. 增强for循环

    //增强for循环
    //for(元素类型,起名 : 遍历的集合)
    for (Student stu : c) {
        System.out.println(stu);
    }
    
  3. foreach

    //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

此为超出10时源码过程

image-20250425165743512

LinkedList特有方法

removeFirst() removeLast()

Set集合

image-20251220175506487

数据结构:树

树的知识点总结:

image-20251012170947546

三种二叉树

image-20250509183934916

平衡二叉树

image-20250509183446340

红黑树(重难点)

红黑树是一种特殊的平衡二叉树,但并不是完全平衡的平衡二叉树,效率最高

添加树的情况

image-20250509201524220

TreeSet集合

排序和取出顺序示例:

image-20251217162402752

自然排序示例:

image-20250510170405367

比较器排序示例:

注意:

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

小知识点:可变参数

可变参数用在形参中可以接受多个数据。

例:int... 名称

传输参数非常灵活方便可以不传参数可以传1个或多个也可以传数组

方法本质就是一个数组

Collections集合工具类

image-20251220173953237

image-20251220174214148

示例:

//多数据添加
        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集合

image-20251220201801335

常用API

image-20251220201929102

MAP特点

image-20251220202017714

image-20251220203224248

Map的三种遍历方式

1entrySet方法

image-20251220205848087
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方法

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