stream排序、筛选、截取、统计等操作
程序员文章站
2022-04-21 10:33:32
简单举例介绍比较方便不复杂的Stream流方法,提升编程效率public class StreamTest {public static void main(String[] args) {Listmlist = new ArrayList();TestData data;//字段名为name,time,agedata = new TestData("大毛", "2020-01-01",21);ml...
简单举例介绍比较方便不复杂的Stream流方法,提升编程效率
public class StreamTest {
public static void main(String[] args) {
List<TestData>mlist = new ArrayList<TestData>();
TestData data;
//字段名为name,time,age
data = new TestData("大毛", "2020-01-01",21);
mlist.add(data);
data = new TestData("二毛", "2020-02-08",12);
mlist.add(data);
data = new TestData("三毛", "",23);
mlist.add(data);
data = new TestData("四毛", "2020-03-03",17);
mlist.add(data);
List<TestData> squaresList = mlist.stream()
//下面是筛选
.filter(o -> !o.getTime().contains("08"))
.filter(o -> !o.getTime().isEmpty())
//下面是排序
.sorted(Comparator.comparing(TestData::getTime).reversed())
.sorted((o1,o2) -> o1.getAge()-o2.getAge())
//下面这一行把list列表数据返回,不写就是一个布尔类型结果返回
.collect(Collectors.toList())
//下面是截取,只取结果种的前两条数据
.subList(0,2);
}
}
本文地址:https://blog.csdn.net/whlqunzhu/article/details/107692996