当前位置:网站首页>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
边栏推荐
- ARM64汇编的函数有那些需要注意?
- Jincang KFS data cascade scenario deployment
- How TCP handles exceptions during three handshakes and four waves
- 4 life distributions
- SQL注入漏洞(繞過篇)
- Crawler scheduling framework of scratch+scratch+grammar
- 寿命分布 4种
- Two ways of redis persistence -- detailed explanation of RDB and AOF
- GaussDB others内存比较高的场景
- 芯片的发展史和具体用途以及结构是什么样的
猜你喜欢

Recommend a virtual machine software available for M1 computer

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

按钮多次点击造成结果

Course paper + code and executable EXE file of library information management system based on C language

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

Niuke.com: Candy distribution

A difficult mathematical problem baffles two mathematicians

Use of Presto visualization client-yanagishima20.0

西山科技冲刺科创板:拟募资6.6亿 郭毅军夫妇有60%表决权

CMU puts forward a new NLP paradigm - reconstructing pre training, and achieving 134 high scores in college entrance examination English
随机推荐
時創能源沖刺科創板:擬募資11億 年營收7億淨利反降36%
Vulnérabilité à l'injection SQL (contournement)
Shichuang energy rushes to the scientific innovation board: it plans to raise 1.1 billion yuan, with an annual revenue of 700million yuan and a 36% decrease in net profit
牛客网:分糖果问题
How to use the markdown editor
Detailed explanation of spark specification
Big Endian 和 Little Endian
CMU提出NLP新范式—重构预训练,高考英语交出134高分
Openfeign uses
Handler、Message、Looper、MessageQueue
建造者模式
Is industrial securities a state-owned enterprise? Is it safe to open an account in industrial securities?
Spark runs wordcount (case 2)
寿命分布 4种
ARM64特有一些的汇编指令
Getting started with Apache Shenyu
Manually rollback abnormal data
记一次给OpenHarmony提交代码的过程
SQL注入漏洞(类型篇)
基於Minifilter框架的雙緩沖透明加解密驅動 課程論文+項目源碼