博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java8 stream基础
阅读量:4538 次
发布时间:2019-06-08

本文共 2073 字,大约阅读时间需要 6 分钟。

List<Integer> list = new ArrayList<Integer>();

list.add(2);
list.add(4);
list.add(0);
list.add(100);
System.out.println("----------------stream---------------------");
list.stream().filter(e -> e > 4).forEach(System.out::println);
Stream<Integer> list1 = list.stream().filter(e -> e > 0);
Iterator<Integer> iter = list1.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
System.out.println("--------------filter 条件查询--------------------");
list = list.stream().filter(e -> e > 2).collect(Collectors.toList());
list.forEach(System.out::println);
List<Student> studentlist=new ArrayList<Student>();
studentlist.add(new Student("A",12));
studentlist.add(new Student("B",5));
studentlist.add(new Student("C",16));
studentlist.add(new Student("D",2));
System.out.println("取出集合中对象指定属性");
studentlist.stream().map(Student::getName).forEach(System.out::println);
System.out.println("-----------------------------按对象属性正序---------------------------------------------");
studentlist.stream().sorted(Comparator.comparing(Student::getAge)).forEach(System.out::println);
System.out.println("-----------------------------按对象属性倒序---------------------------------------------");
studentlist.stream().sorted(Comparator.comparing(Student::getAge).reversed()).forEach(System.out::println);
studentlist=studentlist.stream().sorted(Comparator.comparing(Student::getAge).reversed()).collect(Collectors.toList());
System.out.println("----------------------------stream 转化为集合输出----------------------------------------------");
studentlist.forEach(System.out::print);

class Student{

private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Student(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}

转载于:https://www.cnblogs.com/coderdxj/p/9020006.html

你可能感兴趣的文章
AJAX入门介绍
查看>>
[算法竞赛入门]第一章_算法概述
查看>>
SQL反模式笔记3——主键规范
查看>>
简单粗暴,微生物生态研究中常用数据库简介--转载
查看>>
Oracle -操作数据库
查看>>
c - 给分数分级别
查看>>
chrome 调试
查看>>
luoguP2774 方格取数问题
查看>>
tcp/ip协议各层的理解与
查看>>
python中的setdefault()方法
查看>>
转 VSFTP用户权限管控
查看>>
poj2420 A Star not a Tree? 模拟退火
查看>>
微信小程序--登录授权,一键获取用户微信手机号并登录
查看>>
[转载] C#面向对象设计模式纵横谈——13. Proxy代理模式
查看>>
JqueryEasyUI浅谈---视频教程公布
查看>>
ASP.NET导出Excel,打开时提示“您尝试打开文件'XXX.xls'的格式与文件扩展名指定文件不一致”...
查看>>
Javaweb之 servlet 开发详解1
查看>>
Restore IP Addresses
查看>>
DWR框架简单应用
查看>>
KMP 学习心得-----转
查看>>