s-day07- 传统操作集合和Stream流操作集合的对比
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package com.inmind.stream07;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/*
|
||||
传统操作集合和Stream流操作集合的对比
|
||||
|
||||
需求:
|
||||
1. 首先筛选所有姓张的人;
|
||||
2. 然后筛选名字有三个字的人;
|
||||
3. 最后进行对结果进行打印输出。
|
||||
|
||||
|
||||
*/
|
||||
public class Demo12 {
|
||||
public static void main(String[] args) {
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("张无忌");
|
||||
list.add("周芷若");
|
||||
list.add("赵敏");
|
||||
list.add("张强");
|
||||
list.add("张三丰");
|
||||
//1. 首先筛选所有姓张的人;
|
||||
ArrayList<String> zList = new ArrayList<>();
|
||||
for (String s : list) {
|
||||
if (s.startsWith("张")) {
|
||||
zList.add(s);
|
||||
}
|
||||
}
|
||||
System.out.println(zList);
|
||||
|
||||
//2. 然后筛选名字有三个字的人;
|
||||
ArrayList<String> newList = new ArrayList<>();
|
||||
for (String s : zList) {
|
||||
if (s.length() == 3) {
|
||||
newList.add(s);
|
||||
}
|
||||
}
|
||||
System.out.println(newList);
|
||||
//3. 最后进行对结果进行打印输出。
|
||||
for (String s : newList) {
|
||||
System.out.println(s);
|
||||
}
|
||||
System.out.println("------------------以下代码为Stream流写法--------------------");
|
||||
list.stream().filter(s->s.startsWith("张")).filter(s->s.length() == 3).forEach(s-> System.out.println(s));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user