package com.ehualu.data.business.listTest.service;
import com.ehualu.data.business.listTest.model.PersonModel;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ListTestService {
public static void main(String[] args) {
ArrayList<String> strList = new ArrayList<>();
strList.add("张三");
strList.add("李四");
strList.add("张三");
strList.add("王五");
// 找寻list中包含三的元素
List<String> strListColl = strList.stream()
.filter((a) -> a.startsWith("张三"))
.collect(Collectors.toList());
// System.out.println("strListColl.size() = " + strListColl.size());
// System.out.println("strListColl.toString() = " + strListColl.toString());
// 利用stream流遍历list集合
// strList.stream().forEach((a) -> System.out.println("a = " + a));
// 过滤掉不符合条件的元素 fiter 新的集合将会只保留符合条件的元素
// List<String> fiterList = strList.stream().filter((a) -> a.equals("李四")).collect(Collectors.toList());
// fiterList.stream().forEach((a) -> System.out.println("a = " + a));
ArrayList<String> strInteger = new ArrayList<>();
strInteger.add("1");
strInteger.add("2");
strInteger.add("3");
strInteger.add("4");
strInteger.add("5");
// 将list集合中的String类型元素 转换成Integer类型元素
List<Integer> strToIntList = strInteger.stream().map(a -> Integer.parseInt(a)).collect(Collectors.toList());
//strToIntList.stream().forEach((a) -> System.out.println("a = " + a));
// 统计元素个数 与 list自带的size()方法一致 但 size()方法返回的为int类型
int size = strInteger.size();
// System.out.println("strInteger.size() = " + size);
// stream().count()返回的为long类型 如果后续需要操作 则需转换数据类型
long count = strInteger.stream().count();
// System.out.println("strInteger.stream().count() = " + count);
// 截取集合中前几条数据
List<String> limitCollect = strInteger.stream().limit(3).collect(Collectors.toList());
// limitCollect.stream().forEach((a) -> System.out.println("a = " + a));
// 跳过前几条数据 保留后面的数据
List<String> skipList = strInteger.stream().skip(3).collect(Collectors.toList());
// skipList.stream().forEach((a) -> System.out.println("a = " + a));
// 实现两个流数据的合并 前置位集合数据在前,后置位集合数据在后
List<String> concatList = Stream.concat(strInteger.stream(), strList.stream()).collect(Collectors.toList());
// concatList.stream().forEach((a) -> System.out.println("a = " + a));
// 利用stream流 对实体集合进行操作
ArrayList<PersonModel> personModels = new ArrayList<>();
// 构建实体集合数据
PersonModel personModel = new PersonModel("张三", "30");
PersonModel personModel2 = new PersonModel("李四", "25");
PersonModel personModel3 = new PersonModel("王五", "27");
PersonModel personModel4 = new PersonModel("张三", "28");
personModels.add(personModel);
personModels.add(personModel2);
personModels.add(personModel3);
personModels.add(personModel4);
// 实体类集合利用stream流进行遍历
// personModels.stream().forEach((a) -> System.out.println("a = " + a.toString()));
// 遍历数据
// personModels.stream().forEach(System.out::println);
// 利用fitler过滤数据 获取集合中符合条件的元素
List<PersonModel> nameFilterList = personModels.stream().filter(a -> a.getName().equals("张三")).collect(Collectors.toList());
// nameFilterList.stream().forEach(System.out::println);
// 获取名为李四的信息 没有的话就返回一个新的学生对象 可自定义学生对象的属性列表
PersonModel personModel1 = personModels.stream().filter(a -> a.getName().equals("九")).findAny().orElse(new PersonModel("暂无此学生", null));
// System.out.println("personModel1 = " + personModel1);
// 根据姓名 去除姓名重复的数据
List<String> distinctfirstList = personModels.stream().map(PersonModel::getName).distinct().collect(Collectors.toList());
// collect.stream().forEach(System.out::println);
ArrayList<PersonModel> distinctSecondList = personModels.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<PersonModel>(
Comparator.comparing(PersonModel::getName))), ArrayList::new
));
// collect.stream().forEach(System.out::println);
// 根据集合中的元素排序 正序
List<PersonModel> sortedAgeList = personModels.stream().sorted(Comparator.comparing(PersonModel::getAge)).collect(Collectors.toList());
// sortedAgeList.stream().forEach(System.out::println);
// 根据集合中的元素排序 倒序
List<PersonModel> resortedAgeList = personModels.stream().sorted(Comparator.comparing(PersonModel::getAge).reversed()).collect(Collectors.toList());
// resortedAgeList.stream().forEach(System.out::println);
// 根据姓名进行分组 根据姓名分组会将条件作为key值放置在map集合中,而value的值则会是对应的实体对象并包含所有属性
Map<String, List<PersonModel>> groupByNameList = personModels.stream().collect(Collectors.groupingBy(PersonModel::getName));
/*Set<Map.Entry<String, List<PersonModel>>> entries = groupByNameList.entrySet();
for (Map.Entry<String, List<PersonModel>> entry : entries) {
System.out.println("entry.getKey()+\" === \"+entry.getValue() = " + entry.getKey() + " === " + entry.getValue());
}*/
}
}
list集合中stream流的使用
最新推荐文章于 2024-07-16 16:42:02 发布
该代码示例展示了如何使用JavaStreamAPI进行集合操作,包括过滤(filter)、转换(map)、统计(count)、截取(limit)、跳过(skip)、合并(concat)以及对实体类集合的处理,如过滤(filter)、分组(groupingBy)和排序(sorted)等。
1061

被折叠的 条评论
为什么被折叠?



