当前位置:网站首页>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
边栏推荐
- Big endian and little endian
- Database Series: MySQL index optimization summary (comprehensive version)
- Detailed explanation of spark specification
- 时创能源冲刺科创板:拟募资11亿 年营收7亿净利反降36%
- Niuke.com: Candy distribution
- Double tampon transparent cryptage et décryptage basé sur le cadre minifilter
- Source code analysis of AQS & reentrantlock
- Spark runs wordcount (case 1)
- Leetcode 1249. Remove invalid brackets (awesome, finally made)
- Builder pattern
猜你喜欢

Ladder Side-Tuning:预训练模型的“过墙梯”

Vulnérabilité à l'injection SQL (contournement)

Shichuang Energy sprint Technology Innovation Board: le chiffre d'affaires annuel prévu de 1,1 milliard de RMB est de 0,7 milliard de RMB, en baisse de 36%

C disk uses 100% cleaning method

Démarrer avec Apache shenyu

Redis6 note02 configuration file, publish and subscribe, new data type, jedis operation

基于超算平台气象预警并行计算架构研究

Why distributed IDS? What are the distributed ID generation schemes?

Spark runs wordcount (case 2)

Research on parallel computing architecture of meteorological early warning based on supercomputing platform
随机推荐
手机上股票开户安全吗?找谁可以开户啊?
Spark history server performance improvement (I) -- Application List
Arrays. asList()
Redis6笔记02 配置文件,发布和订阅,新数据类型,Jedis操作
Nacos installation and use
Idea local launch Flink task
仿真与烧录程序有哪几种方式?(包含常用工具与使用方式)
C disk uses 100% cleaning method
Comment TCP gère - t - il les exceptions lors de trois poignées de main et de quatre vagues?
基于OpenStreetMap+PostGIS的地理位置系统 论文文档+参考论文文献+项目源码及数据库文件
SystemVerilog (XIII) - enumerate data types
Gaussdb cluster maintenance case set - slow SQL execution
Golden sun education listed in the U.S.: a small cap medium cap stock with a market value of USD 360million
Dynamic programming to solve stock problems (Part 1)
Spannable and editable, spannablestring and spannablestring
数据库系列:MySQL索引优化总结(综合版)
9 cases where elements cannot be located
Semaphore source code analysis
Countdownlatch source code analysis
SQL injection vulnerability (type chapter)