当前位置:网站首页>Simple use of stream
Simple use of stream
2022-06-25 11:40:00 【Duxiaolie】
List of articles
Preface
I don't understand a piece of code written by my colleagues , But I feel familiar !
One 、 Sample code
public class StreamTest{
@Test
public void contextLoads() {
Map<String, Integer> items = new HashMap<>();
items.put("a", 1);
items.put("b", 2);
items.put("c", 3);
items.put("d", 4);
items.put("e", 5);
items.put("f", 6);
for (Map.Entry<String, Integer> entry : items.entrySet()) {
// System.out.println(entry);
System.out.print(entry.getKey());
System.out.println(entry.getValue());
}
// java8
items.forEach((k, v) -> System.out.println(k + "==" + v));
items.forEach((k, v) -> {
System.out.print(k);
System.out.println(v);
if ("f".equals(k)) {
System.out.println("hello f");
}
});
}
@Test
public void Test02() {
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
list.add("f");
// for (String s : list) {
// System.out.print(s);// abcdef
// }
// for (int i = 0; i < list.size(); i++) {
// System.out.print(i);// 012345
// }
// jdk8
/** * lambda expression */
// list.forEach(s -> System.out.print(s)); // abcdef It doesn't make much sense to use it alone
// list.forEach(System.out::print); // abcdef It doesn't make much sense to use it alone
list.forEach(str-> {
if ("c".equals(str)){
System.out.println("Hello c");
}
});
/** * stream Form of flow */
Stream<String> b = list.stream().filter(s -> s.contains("b"));
System.out.println(b); // [email protected]
Stream<String> stream = list.stream().filter(String::isEmpty);
System.out.println(stream); // [email protected]
System.err.println("===========");
// Meaning : If the conditions are met, output , similar lambda+if sentence
list.stream().filter(str ->str.contains("b")).forEach(System.out::println); // b
// Use in work
List<User> userlist = new ArrayList<User>();
// Traverse List, Collect a number . Commonly used in : Subquery . Batch modify order status
List<BigDecimal> ids = userlist.stream().map(user -> user.getId())
.collect(Collectors.toList());
List<String> names = userlist.stream().map(User::getName)
.collect(Collectors.toList());
}
}
class User{
private BigDecimal id;
private String name;
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
summary
1, Learn how to use stream Stream collection List Properties of objects in .
List<String> names = userlist.stream().map(User::getName)
.collect(Collectors.toList());
2, To study the Collectors frequently-used 20 A way . link
边栏推荐
- 杭州/北京内推 | 阿里达摩院招聘视觉生成方向学术实习生(人才计划)
- 贝叶斯
- 翌圣生物冲刺科创板:25%收入来自新冠产品销售 拟募资11亿
- Handler、Message、Looper、MessageQueue
- Spark runs wordcount (case 1)
- Detailed explanation of Flink checkpoint specific operation process and summary of error reporting and debugging methods
- Sentinel integrated Nacos data source
- Niuke.com: Candy distribution
- Data Lake survey
- 数据库系列:MySQL索引优化总结(综合版)
猜你喜欢

Translation of meisai C topic in 2022 + sharing of ideas

Dragon Book tiger Book whale Book gnawing? Try the monkey book with Douban score of 9.5

基於Minifilter框架的雙緩沖透明加解密驅動 課程論文+項目源碼

Detailed explanation of Flink checkpoint specific operation process and summary of error reporting and debugging methods

牛客网:分糖果问题

SQL injection vulnerability (type chapter)

Research on parallel computing architecture of meteorological early warning based on supercomputing platform

Niuke.com: host scheduling

Jincang KFS data cascade scenario deployment

Query method and interrupt method to realize USART communication
随机推荐
try-catch-finally
翌圣生物冲刺科创板:25%收入来自新冠产品销售 拟募资11亿
Jincang KFS data centralized scenario (many to one) deployment
SQL注入漏洞(类型篇)
How to realize the rich text editor function of mobile terminal
Niuke.com: host scheduling
基于C语言的图书信息管理系统 课程论文+代码及可执行exe文件
Big endian and little endian
時創能源沖刺科創板:擬募資11億 年營收7億淨利反降36%
How gaussdb counts the response time of user SQL
Flink batch key points (personal translation)
Whole process of web page request
CFCA安心签接入
Use of Presto visualization client-yanagishima20.0
Jincang database kingbasees plug-in identity_ pwdexp
Solution to the timeout scenario of Flink streaming computing (official live broadcast)
How PHP extracts image addresses from strings
Keywords serializable serialization and deserialization
Niuke: rotation array
仿真与烧录程序有哪几种方式?(包含常用工具与使用方式)