当前位置:网站首页>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
边栏推荐
- SQL injection vulnerability (type chapter)
- 贝叶斯
- Crawler scheduling framework of scratch+scratch+grammar
- Nacos installation and use
- Two ways of redis persistence -- detailed explanation of RDB and AOF
- 2022 mathematical modeling competition time and registration fee
- 基于Minifilter框架的双缓冲透明加解密驱动 课程论文+项目源码
- 芯片的发展史和具体用途以及结构是什么样的
- Countdownlatch source code analysis
- Flink deeply understands the graph generation process (source code interpretation)
猜你喜欢

Apache ShenYu 入門

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

Xishan technology rushes to the scientific innovation board: it plans to raise 660million yuan. Guoyijun and his wife have 60% of the voting rights

時創能源沖刺科創板:擬募資11億 年營收7億淨利反降36%

4 life distributions

牛客网:主持人调度

Spark history server performance improvement (I) -- Application List

Yisheng biological sprint scientific innovation board: 25% of the revenue comes from the sales of new crown products, and it is planned to raise 1.1 billion yuan

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

Data Lake survey
随机推荐
Spannable 和 Editable、SpannableString 和 SpannableString
贝叶斯
Handler、Message、Looper、MessageQueue
Introduction to JVM principle
JVM shutdown hook details
Design and implementation of university laboratory goods management information system based on SSH
Nacos installation and use
时创能源冲刺科创板:拟募资11亿 年营收7亿净利反降36%
Specific meanings of node and edge in Flink graph
Leetcode 1249. Remove invalid brackets (awesome, finally made)
反应c语言程序结构特点的程序
CFCA安心签接入
子类A继承父类B, A a = new A(); 则父类B构造函数、父类B静态代码块、父类B非静态代码块、子类A构造函数、子类A静态代码块、子类A非静态代码块 执行的先后顺序是?
Double tampon transparent cryptage et décryptage basé sur le cadre minifilter
Flink partition policy
Very important very important very important very important very important very important very important very important very important
CMU提出NLP新范式—重构预训练,高考英语交出134高分
redis的dict的扩容机制(rehash)
Some assembly instructions specific to arm64
Ladder Side-Tuning:预训练模型的“过墙梯”