JAVA stream流操作

1.根据某一项去重

List<String> collect = contactRelationList.stream().map(ContactRelation::getRelPersonId).distinct().collect(Collectors.toList());

 2.多条件去重

 List<ContactMethod> collect1 = contactMethodList1.stream().
                collect(collectingAndThen(toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getType() + ";" + o.getContactInfo()))), ArrayList::new));
3.list转map
 // 联系人转以id为key,名称为value的map
        Map<String, String> contactMap = contactList.stream().collect(Collectors.toMap(Contact::getId, Contact::getContact));

4. 以某一个条件做字符串逗号拼接

String pointPositionId = pointPositionList.stream().map(PointPosition::getId).collect(Collectors.joining(","));
5.使用Stream流把list1和list2根据属性menu合并一个list集合
// 使用Stream流把list1和list2根据属性menu合并一个list集合
List<QuotePlatformEntity> collect = platformList.stream().map(platform -> {
    quoteCommercialCategoryEntities.stream().filter(commercialCategoryEntity -> Objects.equals(platform.getCategoryId(), commercialCategoryEntity.getId())).forEach(commercialCategory -> platform.setCategory(commercialCategory.getName()));
    return platform;
}).collect(Collectors.toList());
6.  使用Stream流根据商业类别id分组
Map<String,List <QuotePlatformEntity>> collect1 = platformList.stream().collect(Collectors.groupingBy(QuotePlatformEntity::getCategoryId));

7.分组方式(根据对象中的某个字段的前6位):


Map<String, List<QuoteProjectEntity>> map = quoteProjectEntityList.stream().collect(
                Collectors.groupingBy(
                        quoteProjectEntity-> quoteProjectEntity.getCode().substring(0, 6)
                ));

8.使用 stream 从 List 对象中获取某列数据

//使用 stream 从 List 对象中获取某列数据

List<String> collect = list.stream().map(Student::getName).collect(Collectors.toList())

9. 使用 stream 从 List 对象排序-升序

升序:list.stream().sorted(Comparator.comparing(实体::get属性)).collect(Collectors.toList());

 10.使用 stream 从 List 对象排序-降序

降序:list.stream().sorted(Comparator.comparing(实体::get属性).reversed()).collect(Collectors.toList());

11.  一个list集合内有name,需要判断list中是否有name有叫张三的人,如果有返回true 

list.stream().filter(m->m.getName().equals("张三")).findAny().isPresent();

12. (stream)从数组或集合中找到符合条件的就返回该对象

nodesVOList.stream().filter(e -> e.getId().equals(chanceStageModel.getContactId())).findAny().get()

13. list 就是 想要 转换的数组

//list 就是 想要 转换的数组
String[] title = list.stream().map(User::getName).toArray(String[]::new);

14.list根据某个字段去重之后依旧返回该list

Map<Object, Boolean> map = new HashMap<>();
reportList = reportList.stream().filter(i -> map.putIfAbsent(i.getId(), Boolean.TRUE) == null).collect(Collectors.toList());

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java StreamJava 8 中引入的一种新的数据处理方式,它可以让你以一种声明式的方式来处理数据集合。Stream API 可以极大地简化 Java 程序的编写工作,使代码更加简洁、清晰,同时也可以提高程序的性能。 下面是 Java Stream 的常用操作: 1. 创建 Stream:可以通过集合、数组、文件等方式创建 Stream。 2. 中间操作:对 Stream 进行转换、过滤等操作,但不会执行任何操作。 3. 终止操作:对 Stream 进行计算、打印等操作,会执行 Stream操作。 4. 聚合操作:对 Stream 进行聚合操作,如求和、求最大值、求平均值等。 下面是一些常用的 Stream 操作示例: 1. 创建 Stream ``` List<String> list = Arrays.asList("Java", "Python", "C++", "JavaScript"); Stream<String> stream = list.stream(); // 通过集合创建 Stream IntStream intStream = IntStream.range(0, 10); // 创建一个 IntStream ``` 2. 中间操作 ``` stream.filter(s -> s.startsWith("J")) // 过滤出以 J 开头的字符串 .map(String::toUpperCase) // 将字符串转为大写 .sorted() // 排序 .distinct() // 去重 .limit(2); // 取前两个元素 ``` 3. 终止操作 ``` stream.forEach(System.out::println); // 遍历打印 Stream 中的元素 stream.count(); // 计算 Stream 中的元素个数 stream.max(String::compareTo); // 找出 Stream 中的最大值 stream.min(String::compareTo); // 找出 Stream 中的最小值 stream.reduce((a, b) -> a + b); // 将 Stream 中的所有元素累加起来 stream.collect(Collectors.toList()); // 将 Stream 转为 List ``` 4. 聚合操作 ``` IntStream intStream = IntStream.of(1, 2, 3, 4, 5); intStream.sum(); // 求和 intStream.average(); // 求平均值 intStream.max(); // 求最大值 intStream.min(); // 求最小值 ``` 这些仅是 Java Stream 的一部分常用操作,还有很多其他的操作方式。通过使用 Stream API,可以大大简化代码的编写,提高程序的性能和可读性。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值