惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
G
Google Developers Blog
B
Blog
Engineering at Meta
Engineering at Meta
阮一峰的网络日志
阮一峰的网络日志
The Register - Security
The Register - Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 叶小钗
The Cloudflare Blog
The Hacker News
The Hacker News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
雷峰网
雷峰网
F
Fortinet All Blogs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Hackread – Cybersecurity News, Data Breaches, AI and More
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
A
About on SuperTechFans
量子位
Recorded Future
Recorded Future
博客园 - 三生石上(FineUI控件)
H
Help Net Security
Help Net Security
Help Net Security
P
Palo Alto Networks Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Troy Hunt's Blog
W
WeLiveSecurity
V
Vulnerabilities – Threatpost
T
The Exploit Database - CXSecurity.com
Know Your Adversary
Know Your Adversary
Apple Machine Learning Research
Apple Machine Learning Research
Scott Helme
Scott Helme
N
News | PayPal Newsroom
AWS News Blog
AWS News Blog
D
DataBreaches.Net
Blog — PlanetScale
Blog — PlanetScale
MongoDB | Blog
MongoDB | Blog
B
Blog RSS Feed
腾讯CDC
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
TaoSecurity Blog
TaoSecurity Blog
GbyAI
GbyAI
Y
Y Combinator Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
D
Docker

博客园 - happy刘艺

关于SKILL idea解压包安装问题解决 转载:java :: Java中的双冒号操作符 转:Git的使用--如何将本地项目上传到Github(两种简单、方便的方法) 转:springboot 与swagger整合出现Unable to infer base url.This is common when using dynamic的解决办法 转:Swagger2自动生成接口文档和Mock模拟数据 InfluxDB springboot application.properties文件加载顺序 maven surefire插件与testng spring-boot项目学习路径 转:Java中Lambda表达式的使用 RPC之Thrift 介绍及java实例 class对象的getResource()方法 Eclipse开发,编译,打包常见问题总结------持续更新 mysql查找包含某个字符串的记录 性能测试入门(零)测试前言 性能测试入门(八)jmeter--PerfMon(性能监控工具)插件安装与部署 jmeter--PerfMon(性能监控工具)插件使用详解 性能测试入门(七)jmeter分布式测试
collection 与stream与lambd表达式的结合使用
happy刘艺 · 2019-06-03 · via 博客园 - happy刘艺

Arraylist除了常用的add,get,set,remove,contains,size,indexof,sublist这些常用的增删改方法,还有已下

1、void java.util.ArrayList.forEach(Consumer<? super String> action)
Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. Unless otherwise specified by the implementing class, actions are performed in the order of iteration (if an iteration order is specified). Exceptions thrown by the action are relayed to the caller
对list中的元素,遍历执行action
2、void java.util.ArrayList.sort(Comparator<? super String> c)

Sorts this list according to the order induced by the specified Comparator

按照comparator的compare指定的排序规则。排序集合中内容

New Comparator<String>(){

@Override  //重写compare方法的实现(正数,0,负数分别对应大于,等于,小于)

          public int compare(String s1, String s2) { 

              return (s1.compareTo(s2)); //按字典顺序比较两个字符串character,字典顺序越往后的index越大。Eg: “a”.compareTo(“c”):-2; “a”.compareTo(“ab”):-1; “d”.compareTo(“ab”): 3;

              }}

3、Stream<String> java.util.Collection.stream()
Returns a sequential Stream with this collection as its source
4、Stream<String> java.util.stream.Stream.filter(Predicate<? super String> predicate)

Returns a stream consisting of the elements of this stream that match the given predicate.

过滤出符合这些predicate条件的集合

Eg: Stream.filter((p) -> (p.getSalary() > 1400))  就是过滤出这些工资大于1400的数据

sorted(Comparator<? super T> comparator)

min(Comparator<? super T> comparator)

max(Comparator<? super T> comparator);

limit(long maxSize);

截止以上我们对集合以及stream的一些常用用法有了个大概的了解。下面我们再来学习下,如何结合lambda表达式来使用这些常用的集合以及stream的方法。

1、首先了解下什么是lambda表达式

Lambda表达式的语法

基本语法:

 (parameters) -> expression

(parameters) ->{ statements; }

下面是Java lambda表达式的简单例子:

   // 1. 不需要参数,返回值为 5 

( ) -> 5 

   // 2. 接收一个参数(数字类型),返回其2倍的值 

x -> 2 * x 

   // 3. 接受2个参数(数字),并返回他们的差值 

(x, y) -> x – y 

   // 4. 接收2个int型整数,返回他们的和 

(int x, int y) -> x + y 

   // 5. 接受一个 string 对象,并在控制台打印,不返回任何值(看起来像是返回void) 

(String s) -> System.out.print(s)

2、可以使用lambda表达式,代替实现ConsumerComparatorPredicate等对象

1)  lambda表达式,代替实现Consumer

arraylist.foreach(p->p+2)

2)  lambda表达式,代替实现Comparator

Array.sort(arraylist, New Comparator<String>(){ public int compare(String s1, String s2) {                return (s1.compareTo(s2));        }}

)

替换成

Array.sort(arraylist,(s1,s2)->(s1.compareTo(s2)));

     3) lambda表达式,代替实现Predicate

phpProgrammers.stream().filter((p) -> (p.getSalary() > 1400))