当前位置:网站首页>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
边栏推荐
- C disk uses 100% cleaning method
- 时创能源冲刺科创板:拟募资11亿 年营收7亿净利反降36%
- Big Endian 和 Little Endian
- Course paper + code and executable EXE file of library information management system based on C language
- CMU提出NLP新范式—重构预训练,高考英语交出134高分
- 元素定位不到的 9 种情况
- Jincang database kingbasees plug-in identity_ pwdexp
- How to use the markdown editor
- Handler、Message、Looper、MessageQueue
- Double buffer transparent encryption and decryption driven course paper + project source code based on minifilter framework
猜你喜欢

SQL injection vulnerability (bypass)

Redis6笔记02 配置文件,发布和订阅,新数据类型,Jedis操作

Deeply understand Flink SQL execution process based on flink1.12

Query method and interrupt method to realize USART communication

Ladder side tuning: the "wall ladder" of the pre training model

Jincang KFS data cascade scenario deployment

C disk uses 100% cleaning method

Data Lake survey

Manually rollback abnormal data

Introduction to JVM principle
随机推荐
西山科技冲刺科创板:拟募资6.6亿 郭毅军夫妇有60%表决权
Detailed explanation of spark specification
建造者模式
ThingsPanel 发布物联网手机客户端(多图)
SQL注入漏洞(类型篇)
Dynamic programming to solve stock problems (Part 1)
SQL injection vulnerability (bypass)
try-catch-finally
CMU puts forward a new NLP paradigm - reconstructing pre training, and achieving 134 high scores in college entrance examination English
Course paper + code and executable EXE file of library information management system based on C language
try-catch-finally
How gaussdb counts the response time of user SQL
基于OpenStreetMap+PostGIS的地理位置系统 论文文档+参考论文文献+项目源码及数据库文件
基于C语言的图书信息管理系统 课程论文+代码及可执行exe文件
GC
Under what circumstances will Flink combine operator chains to form operator chains?
Kingbasees plug-in DBMS of Jincang database_ session
Spark runs wordcount (case 2)
A difficult mathematical problem baffles two mathematicians
Source code analysis of AQS & reentrantlock