Java集合collection map stream流

27 篇文章 0 订阅
6 篇文章 0 订阅

Java 容器

集合

collection单例集合:

常见的数据结构:

  1. 栈:

先进后出 后进先出

3. 队列:

先进先出 后进后出

  1. 数组:

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

  1. 链表:

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

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

  1. 查找二叉树:

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

  1. 平衡树:

CRUD较快

  1. 红黑树:

综合性能都很好

list系列

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

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

set系列

LinkedHashset 有序 不重复 无索引

HashSet 无序 不重复 无索引

TreeSet 排序 不重复 无索引

Set<Student> set = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.age-o1.age; //降序
//return o1.age-o2.age; //升序
}
});
集合工具类 Collections
Collections.addAll(set, s1, s2, s3)
Collections.sort(list);
......

可变参数

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));
}

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

HashMap

LinkedHashMap

entrySet把map集合转化为set集合
Set<Map.Entry<Integer, String>> entries = map.entrySet();

for (Map.Entry<Integer, String> entry : entries) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
不可变集合

1.不可变List

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

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

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

  1. 不可变map

Map<Integer, String> map1 = Map.of(1, "a", 2, "b");

Stream流体系

更好更简单操作集合

List<String> list2 = new ArrayList<>();

Collections.addAll(list1, "aab", "bbc", "cccccd", "aaae", "ea");

Collections.addAll(list1, "111", "223", "4443d", "23ae", "34211a");

Stream<String> stream1 = list1.stream();
Stream<String> 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

获取数组流

int[] arr = {1, 1, 1};
Stream<int[]> stream3 = Stream.of(arr);
IntStream stream4 = Arrays.stream(arr);
stream流收集

恢复到数组 / 集合中去

流只能使用一次

List<String> list = stream1.collect(Collectors.toList());

Set<String> set = stream1.collect(Collectors.toSet());

数组流直接toArray()
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NoBug.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值