博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java 8 Streams
阅读量:4542 次
发布时间:2019-06-08

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

public class Employee {    private Long id;    private String name;    private Integer age;    public Employee() {    }    public Employee(Long id, String name, Integer age) {        this.id = id;        this.name = name;        this.age = age;    }    public Long getId() {        return id;    }    public void setId(Long id) {        this.id = id;    }    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;    }    @Override    public String toString() {        return new ToStringBuilder(this)                .append("id", id)                .append("name", name)                .append("age", age)                .toString();    }}
public class MyTest {    public static void main(String[] args) {        Employee employee1 = new Employee(11L, "hello1", 21);        Employee employee2 = new Employee(12L, "hello2", 22);        Employee employee3 = new Employee(13L, "hello3", 23);        Employee employee4 = new Employee(14L, "hello4", 24);        List
employees = Lists.newArrayList(); employees.add(employee1); employees.add(employee2); employees.add(employee3); employees.add(employee4); List
ems = employees.stream().filter(e -> e.getId() >= 12).collect(toList()); for (Employee e : ems) { System.out.println(e); } List
ems1 = employees.stream().filter(e -> e.getId() >= 12).map(Employee::getName).collect(toList()); for (String s : ems1) { System.out.println(s); } /** * @apiNote * The following will accumulate strings into an ArrayList: *
{
@code * List
asList = stringStream.collect(Collectors.toList()); * }
* *

The following will classify {

@code Person} objects by city: *

{
@code * Map
> peopleByCity * = personStream.collect(Collectors.groupingBy(Person::getCity)); * }
* *

The following will classify {

@code Person} objects by state and city, * cascading two {
@code Collector}s together: *

{
@code * Map
>> peopleByStateAndCity * = personStream.collect(Collectors.groupingBy(Person::getState, * Collectors.groupingBy(Person::getCity))); * }
*/ Map
> empById = ems.stream().collect(Collectors.groupingBy(Employee::getId)); List
emps = empById.get(12L); for (Employee e : emps) { System.out.println(e); } List
strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl"); String mergedString = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", ")); System.out.println("合并字符串: " + mergedString); }}

 参考文献

Introduction to Java 8 Streams

A Guide to Streams in Java 8: In-Depth Tutorial with Examples

转载于:https://www.cnblogs.com/parkdifferent/p/10676608.html

你可能感兴趣的文章
每日一字:悟
查看>>
CentOS7.6安装稳定版Nginx
查看>>
LeetCode 1002. Find Common Characters (查找常用字符)
查看>>
建立隐藏管理员用户
查看>>
android设置图文提醒功能
查看>>
ajax跨域提交
查看>>
完成登录与注册页面的前端
查看>>
Mac下source tree 下的安装
查看>>
Q学习原理及例子
查看>>
rpmbuild 源码打包clickhouse,附带打好的rpm包下载地址
查看>>
软件体系结构原理、方法与实践总结
查看>>
2017-2018-1 《程序设计与数据结构》第3周学习总结
查看>>
一些基础语法
查看>>
360多万条信息把一台服务器快拖卡了
查看>>
Git详解之六 Git工具
查看>>
等高布局display:table
查看>>
onunload与onbeforeunload事件解析 ...
查看>>
Openjudge-计算概论(A)-取石子游戏
查看>>
python-装饰器
查看>>
(4)获取servlet常用api
查看>>