• Java集合collection map stream流


    Java 容器

    集合

    collection单例集合:

    常见的数据结构:

    1. 栈:

    先进后出 后进先出

    3. 队列:
    
    • 1

    先进先出 后进后出

    1. 数组:

    数组中元素地址连续 长度固定 使用索引查找数据速度快(不使用索引查找指定元素还是慢 需要遍历) 增删速度慢

    1. 链表:

    单向链表 a -> b -> c 增删相对数组较快

    双向链表 a <-> b <-> c 对收尾进行增删改查操作很快

    1. 查找二叉树:

    在不出现一边长一边短的情况下CRUD较快

    1. 平衡树:

    CRUD较快

    1. 红黑树:

    综合性能都很好

    list系列

    ArrayList: 基于数组实现 根据索引查询速度较快 增删相对较慢

    LinkedList: 基于双链表实现 增删收尾元素非常快

    set系列

    LinkedHashset 有序 不重复 无索引

    HashSet 无序 不重复 无索引

    TreeSet 排序 不重复 无索引

    Set set = new TreeSet<>(new Comparator() {
    @Override
    public int compare(Student o1, Student o2) {
    return o2.age-o1.age; //降序
    //return o1.age-o2.age; //升序
    }
    });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    集合工具类 Collections

    Collections.addAll(set, s1, s2, s3)
    Collections.sort(list);
    ......
    
    • 1
    • 2
    • 3

    可变参数

    get(new int[]{2,1,4},1,2,3); //调用
    
    //定义可变参数方法
    public static void get(int[] arr,int...nums) {
    System.out.println(Arrays.toString(nums));
    System.out.println(Arrays.toString(arr));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Map双列集合(键值对集合)

    HashMap

    LinkedHashMap

    entrySet把map集合转化为set集合

    Set> entries = map.entrySet();
    
    for (Map.Entry entry : entries) {
    System.out.println(entry.getKey());
    System.out.println(entry.getValue());
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    不可变集合

    1.不可变List

    List list1 = List.of("1","2");

    1. 不可变Set 不能有重复值

    Set set1 = Set.of("1", "2");

    1. 不可变map

    Map map1 = Map.of(1, "a", 2, "b");

    Stream流体系

    更好更简单操作集合

    List list2 = new ArrayList<>();
    
    Collections.addAll(list1, "aab", "bbc", "cccccd", "aaae", "ea");
    
    Collections.addAll(list1, "111", "223", "4443d", "23ae", "34211a");
    
    Stream stream1 = list1.stream();
    Stream stream2 = list2.stream();
    
    Stream.concat(stream1,stream2).forEach(s -> System.out.print(s+"  "));
    
    list1.stream().filter(s ->
    s.startsWith("a")).limit(1).forEach(s -> System.out.println(s+"  "));
    输出:aab  bbc  cccccd  aaae  ea  111  223  4443d  23ae  34211a
    输出:  aab   aaae
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    获取数组流

    int[] arr = {1, 1, 1};
    Stream stream3 = Stream.of(arr);
    IntStream stream4 = Arrays.stream(arr);
    
    • 1
    • 2
    • 3

    stream流收集

    恢复到数组 / 集合中去

    流只能使用一次

    List list = stream1.collect(Collectors.toList());
    
    Set set = stream1.collect(Collectors.toSet());
    
    数组流直接toArray()
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • 相关阅读:
    Object的常用方法
    Kubernetes上的gRPC负载均衡服务
    WPF 2024 金九银十 最新 高级 架构 面试题 C#
    面试官:Zookeeper集群怎么搭建?
    竞赛 基于机器视觉的二维码识别检测 - opencv 二维码 识别检测 机器视觉
    icon免费网址
    C++ vector容器
    内存泄漏了~
    学习笔记 | 音视频 | 推流项目框架及细节
    uniapp vue3 使用pinia存储 并获取数据
  • 原文地址:https://blog.csdn.net/qq_61672548/article/details/126162565