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

推荐订阅源

Engineering at Meta
Engineering at Meta
博客园_首页
H
Help Net Security
WordPress大学
WordPress大学
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
B
Blog
I
InfoQ
SecWiki News
SecWiki News
T
Tailwind CSS Blog
Spread Privacy
Spread Privacy
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
N
Netflix TechBlog - Medium
P
Palo Alto Networks Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Vercel News
Vercel News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
Kaspersky official blog
M
MIT News - Artificial intelligence
S
Schneier on Security
T
Threat Research - Cisco Blogs
F
Fortinet All Blogs
Cyberwarzone
Cyberwarzone
Scott Helme
Scott Helme
aimingoo的专栏
aimingoo的专栏
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
The Cloudflare Blog
Recent Announcements
Recent Announcements
Security Latest
Security Latest
G
GRAHAM CLULEY
IT之家
IT之家
Y
Y Combinator Blog
The Last Watchdog
The Last Watchdog
腾讯CDC
Google DeepMind News
Google DeepMind News
V
V2EX
S
Securelist
TaoSecurity Blog
TaoSecurity Blog
B
Blog RSS Feed
S
SegmentFault 最新的问题
博客园 - 叶小钗
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
Project Zero
Project Zero
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
F
Full Disclosure

博客园 - 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))