关于 Stream ,Functional Interface 的一点记录

stream对于集合操作的便捷度提升:

 import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; public class ActiveMac { public static void main(String[] args) {
List<Human> humans = new ArrayList<>();
humans.add(new Human("Daniel", 23, "Google"));
humans.add(new Human("Max", 33, "Microsoft"));
humans.add(new Human("Jenny", 18, "Google"));
humans.add(new Human("Alex", 28, "Facebook"));
humans.add(new Human("Charles", 34, "Twitter"));
humans.add(new Human("Roy", 31, "Microsoft")); // 对集合内符合条件的计数
long nums = humans.stream().filter(human -> (human.getAge() > 20)).count();
System.out.println("nums:" + nums); // 对集合内符合条件的筛选输出
List<Human> nameContE = humans.stream().filter(human -> human.getName().contains("e")).collect(Collectors.toList());
System.out.println("nameContainsE:" + nameContE.toString()); // 对集合内元素中元素进行操作
List<Integer> doubleAge = humans.stream().map(human -> human.getAge() * 2).collect(Collectors.toList());
System.out.println("doubleAge:" + doubleAge.toString()); // 对集合内元素分组
Map<String, List<Human>> group = humans.stream().collect(Collectors.groupingBy(Human::getCompany));
System.out.println(group.toString());
}
} class Human {
private String name;
private Integer age;
private String company; public Human(String name, Integer age, String company) {
super();
this.name = name;
this.age = age;
this.company = company;
} 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 String getCompany() {
return company;
} public void setCompany(String company) {
this.company = company;
} @Override
public String toString() {
return name + "-" + age + "-" + company;
}
}

新旧方法的对比:

        // 1.对集合内符合条件的计数
long nums = humans.stream().filter(human -> (human.getAge() > 20)).count();
System.out.println(nums); // 2.对集合内符合条件的筛选输出
List<Human> nameContE = humans.stream().filter(human -> human.getName().contains("e")).collect(Collectors.toList());
System.out.println(nameContE.toString()); // 3.对集合内元素中元素进行操作
List<Integer> doubleAge = humans.stream().map(human -> human.getAge() * 2).collect(Collectors.toList());
System.out.println(doubleAge.toString()); // 4.对集合内元素分组
Map<String, List<Human>> group = humans.stream().collect(Collectors.groupingBy(Human::getCompany));
System.out.println(group.toString()); //
int num = 0;
for (Human h : humans) {
if (h.getAge() > 20) {
num++;
}
}
System.out.println(num); //旧方法-循环
//
List<Human> eResult = new ArrayList<>();
for (Human h : humans) {
if (h.getName().contains("e")) {
eResult.add(h);
}
}
System.out.println(eResult.toString()); //
List<Integer> dounleA = new ArrayList<>();
for (Human h : humans) {
Integer newAge = h.getAge() * 2;
dounleA.add(newAge);
}
System.out.println(dounleA.toString()); //
Map<String, List<Human>> maps = new HashMap<>();
for (Human h : humans) {
List<Human> hs = new ArrayList<>();
String key = h.getCompany();
if (maps.containsKey(key)) {
hs = maps.get(key);
}
hs.add(h);
maps.put(key, hs);
}
System.out.println(maps.toString()); }

输出结果方面并不存在差异

新方法-stream
5
[Daniel-23-Google, Jenny-18-Google, Alex-28-Facebook, Charles-34-Twitter]
[46, 66, 36, 56, 68, 62]
{Google=[Daniel-23-Google, Jenny-18-Google], Twitter=[Charles-34-Twitter], Microsoft=[Max-33-Microsoft, Roy-31-Microsoft], Facebook=[Alex-28-Facebook]} 旧方法-循环
5
[Daniel-23-Google, Jenny-18-Google, Alex-28-Facebook, Charles-34-Twitter]
[46, 66, 36, 56, 68, 62]
{Google=[Daniel-23-Google, Jenny-18-Google], Twitter=[Charles-34-Twitter], Microsoft=[Max-33-Microsoft, Roy-31-Microsoft], Facebook=[Alex-28-Facebook]}

其他的下次记录

关于Java8:StreamAPI的一点记录的更多相关文章

  1. 对Integer类中的私有IntegerCache缓存类的一点记录

    对Integer类中的私有IntegerCache缓存类的一点记录 // Integer类有内部缓存,存贮着-128 到 127. // 所以,每个使用这些数字的变量都指向同一个缓存数据 // 因此可 ...

  2. 从symbol link和hard link 到 unlink函数的一点记录

    之前一直对Linux的文件类型中的 “l” 类型的了解不是很深入,最近经过“圣经”指点,略知一二,在此先记录一下,以便以后查阅,之后会对文件和目录.文件I/O这部分再扩充. 首先需明确,Unix在查阅 ...

  3. 关于删除MySQL Logs的一点记录

    五一前,一个DBA同事反馈,在日常环境中删除一个大的slow log文件(假设文件大小10G以上吧),然后在MySQL中执行flush slow logs,会发现mysqld hang住. 今天尝试着 ...

  4. 关于类属性值校验的一点记录 【知识点Attribute】

    好久没有进来了,之前励志坚持写博客,记录自己在做代码搬运工这段历程中点滴,可是仅仅只坚持了几天,就放弃了!果然是,世上无难事,只要肯放弃!哈哈……闲话不多说,开始进入正题,给自己留点笔记,避免将来老了 ...

  5. 【.Net】关于内存缓存的一点记录

    引言 最近看了内存缓存的一些介绍和用法,在此做个简单记录. MemoryCache 类 MemoryCache 类是.Net 4.0推出的类库,主要是为了方便在Winform和Wpf中构建缓存框架的. ...

  6. 关于db2的一点记录

    近期听搞db2的兄弟说:db2数据库软件的license 不区分平台(os). 先记下来.像db2这么高大上的软件,接触的机会是比較少的. 另外:db2 的license是须要打的,不打的话,超过一段 ...

  7. Nodejs Promise的一点记录

    项目需要,看了点nodejs,其中比较难理解的就是Promise了,记录一下学习bluebird提供的Promise实现. Promise.promisifyAll(obj)方法 作用:把对象的方法属 ...

  8. 关于strlen误用的一点记录

    今天帮一个朋友查一个错误,是运行时报vector iterator incompatible,一般这种问题是向量和迭代器的类型不兼容,或者是进行迭代器判等时前后向量的结构发生变化,如erase操作之后 ...

  9. Android游戏之平台接入的一点记录

    最近手头有需要接入多个渠道的工作,我负责的是Android方面的接入,一般来说,渠道是非常多的,每一个渠道调用的接口都不一致,如果每一个渠道都要自己去弄回非常的耗时,所以网上会有一些接入的中间件提供商 ...

随机推荐

  1. phpstudy 2016 切换Nginx+php7.0版本所需运行库 vc14 + 安装redis拓展

    去微软官方下载vc14的运行库 链接:https://www.microsoft.com/en-us/download/details.aspx?id=48145 32位运行库 安装成功 切换版本成功 ...

  2. CF1119C Ramesses and Corner Inversion

    题目地址:CF1119C Ramesses and Corner Inversion 将两个矩阵异或起来,为 \(1\) 的位置就是需要修改的位置 注意到每一次操作都会导致两行和两列上有两个数被修改 ...

  3. yum安装软件报错Segmentation fault处理

    yum安装软件报错Segmentation fault处理 在使用yum 更新软件时提示:Segmentation fault 中文错误提示: 段错误 [root@CMS-BAK:/usr/local ...

  4. JavaScript高级程序设计(读书笔记)(一)

    本笔记汇总了作者认为“JavaScript高级程序设计”这本书的前七章知识重点,仅供参考. 第一章 JavaScript简介 JavaScript发展简史: 1995年,JavaScript诞生 19 ...

  5. jdbc连接sqlserver,mysql,oracle

    class xxx{ private static String port = "1433"; private static String ip = "192.168.2 ...

  6. Visual Studio 2015 NuGet Update-Package 失败/报错:Update-Package : Unable to load the service index for source https://api.nuget.org/v3/index.json.

    起因 为了用VS2015 community中的NuGet获取Quartz,在[工具]-[NuGet包管理器]-[程序包管理器控制台]中执行 Install-Package Quartz. 却报如下错 ...

  7. 【MySql】常用方法总结

    将一个字段分组,统计每组重复个数,并排序 SELECT Customer, OrderDate, count(*) as Num FROM `all_orders` GROUP BY Customer ...

  8. Common Vulnerability Scoring System CVSS

    1.Generating a Shell payload using msfvenom 2.web intrusion Test in fact in the websecurity ,the web ...

  9. Excel—单元格引用

    EXCEL的引用有3种:相对引用.绝对引用.混合引用 相对引用.绝对引用比较简单,就是要么拖拽后变,要么拖拽后不变 混合引用有点难度,凡是需要向右拖拽再向下拖拽的记住一定是混合引用 要将拖拽后依然不变 ...

  10. PID控制算法的简单分析和仿真!

    PID算法简单剖析如下: 1.首先我们来看一下PID系统的基本组成模块: 如图所示,图中相关参数的表示如下: r(t):系统实际上需要的输出值,这是一个标准值,在我们设定了之后让这个系统去逼近的一个值 ...