博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java8新特性之Stream详解二
阅读量:4164 次
发布时间:2019-05-26

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

       最近在公司的项目中常用Stream结合Lambda表达式来操作集合,使得项目整体代码简洁和整齐;并且上一章也讲了一些关于Stream的常用操作,比如:map()、filter()、concat()、reduce()、max()、min()、distinct()等常用操作。这篇我就分享一下Stream中List、Set、Map之间的转换操作:

准备工作:新建一个Course类,包含name、credit两个属性,并重写hashCode()、equals()方法:

class Course {    private String name;    private Integer credit;    public Course(String name, Integer credit) {        this.name = name;        this.credit = credit;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getCredit() {        return credit;    }    public void setCredit(Integer credit) {        this.credit = credit;    }    @Override    public int hashCode() {        return name.hashCode();    }    @Override    public boolean equals(Object obj) {        Course stu = null;        if (obj != null && obj instanceof Course) {            stu = (Course) obj;            if (this.name.equals(stu.getName()) && this.credit.equals(stu.getCredit())) {                return true;            }        }        return false;    }}

一、Stream将List转换为Map集合

/** * Stream将List转换为Map集合 */@Testpublic void listToMap() {    List
stuList = Arrays.asList( new Course("Android", 10), new Course("Java", 8), new Course("Structure", 13), new Course("IOS", 12), new Course("Guava", 16), new Course("OS", 6)); Map
nameAgeMap = stuList.stream(). collect(Collectors.toMap(Course::getName, Course::getCredit)); nameAgeMap.entrySet().stream().forEach(x -> System.out.println(x.getKey() + ", " + x.getValue())); //Function.identity()指代本身,相当于 x -> x Map
nameStuMap = stuList.stream(). collect(Collectors.toMap(Course::getName, Function.identity())); nameStuMap.entrySet().stream().forEach(x -> System.out.println(x.getKey() + "," + x.getValue().getCredit()));}

执行结果展示:(两次结果展示一致)

Guava, 16

Java, 8
OS, 6
IOS, 12
Structure, 13
Android, 10

注意:collect(Collectors.toMap(Course::getName,Function.identity())) 得到的是<name,course>Map集合。

查看Function.identity()底层实现原理:t -> t 返回对象本身

static 
Function
identity() { return t -> t;}

二、Stream将List转换为Set集合

@Test    public void listToSet() {        List
integerList = Arrays.asList(1, 2, 3, 4, 5, 1, 2, 3, 4, 5); Set
integerSet = integerList.stream(). collect(Collectors.toSet()); integerSet.stream().forEach(System.out::print); System.out.println(""); List
courseList = Arrays.asList( new Course("Android", 10), new Course("Java", 8), new Course("Android", 10), new Course("IOS", 12), new Course("Guava", 16), new Course("Android", 6)); //排序并去重 List
courList = courseList.stream(). sorted(Comparator.comparing(Course::getName)). distinct(). collect(Collectors.toList()); courList.stream().forEach(c -> System.out.println(c.getName() + "," + c.getCredit())); //只去重Set Set
courSet = courseList.stream(). sorted(Comparator.comparing(Course::getName)). collect(Collectors.toSet()); courSet.stream().forEach(c -> System.out.println(c.getName() + "," + c.getCredit())); }

结果展示:

12345

Android,10
Android,6
Guava,16
IOS,12
Java,8
Guava,16
Java,8
IOS,12
Android,10
Android,6

注意:对象去重需要重写类的hashCode()、equals()方法,给出去重排序、去重的List转换为Set集合。

Stream操作集合的功能十分强大,结合Lambda表达式使得代码简洁大方、便捷,还有很多StreamAPI需要讨论,后续待学希望一起讨论进步......

转载地址:http://ncxxi.baihongyu.com/

你可能感兴趣的文章
工作流审批平台-审批流程-指定审批部门
查看>>
商务智能-系统概述-数据图形方式
查看>>
软件项目管理系统-项目管理-模块定义-开发内容
查看>>
工作流审批平台-审批功能
查看>>
商务智能-基本方法-特征与角度
查看>>
软件项目管理系统-项目管理-模块定义-开发笔记
查看>>
工作流审批平台-业务申请-申请书一览
查看>>
商务智能-基本方法-数据钻取
查看>>
python爬虫---requests库+xpath爬取和保存豆瓣电影top250电影数据,一看就会
查看>>
爬虫真的可以接单吗?爬虫实践(一)
查看>>
matplotlib画图(一)---柱状图和饼状图
查看>>
matplotlib画图基础篇(一)
查看>>
python爬虫基础---pyquery解析
查看>>
python爬虫---scrapy入门(一个实例来了解一下scrapy爬虫流程)
查看>>
Debug与Release版本的区别
查看>>
VS2008 Unicode和多字节编码区别
查看>>
SHELLEXECUTEINFO 和 ShellExecuteEx的使用
查看>>
在VC中调用WebService
查看>>
VC++调用webservice (不使用.NET的托管技术)
查看>>
VS2008非托管c++访问webservice服务
查看>>