Skip to content

Commit 05a0e29

Browse files
committed
add stream流
1 parent ac29eae commit 05a0e29

12 files changed

+150
-2
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package Stream;
2+
3+
import lombok.Data;
4+
5+
/**
6+
* @author huanfuan
7+
* @version 1.0
8+
* @date 2019/8/8 0008 下午 12:53
9+
*/
10+
@Data
11+
public class Property {
12+
13+
String name;
14+
// 距离,单位:米
15+
Integer distance;
16+
// 销量,月售
17+
Integer sales;
18+
// 价格,这里简单起见就写一个级别代表价格段
19+
Integer priceLevel;
20+
public Property(String name, int distance, int sales, int priceLevel) {
21+
this.name = name;
22+
this.distance = distance;
23+
this.sales = sales;
24+
this.priceLevel = priceLevel;
25+
}
26+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package Stream;
2+
3+
import java.util.*;
4+
import java.util.stream.Collectors;
5+
6+
/**
7+
* @author huanfuan
8+
* @version 1.0
9+
* @date 2019/8/8 0008 下午 12:54
10+
*/
11+
public class streamTest01 {
12+
public static void main(String[] args) {
13+
Property p1 = new Property("叫了个鸡", 1000, 500, 2);
14+
Property p2 = new Property("张三丰饺子馆", 2300, 1500, 3);
15+
Property p3 = new Property("永和大王", 580, 3000, 1);
16+
Property p4 = new Property("肯德基", 6000, 200, 4);
17+
List<Property> properties = Arrays.asList(p1, p2, p3, p4);
18+
19+
Collections.sort(properties,(x,y) ->x.distance.compareTo(y.distance));
20+
String name = properties.get(0).name;
21+
System.out.println("距离我最近的店铺是:" + name);
22+
23+
//stream操作
24+
String name2=properties.stream().
25+
sorted(Comparator.comparingInt(x ->x.distance))
26+
.findFirst()
27+
.get().name;
28+
System.out.println("距离我最近的店铺是:" + name);
29+
30+
/*获取所有店铺的名称*/
31+
List<String> names = properties.stream()
32+
.map(p -> p.name)
33+
.collect(Collectors.toList());
34+
35+
/*获取每个店铺的价格等级*/
36+
Map<String, Integer> map = properties.stream()
37+
.collect(Collectors.toMap(Property::getName, Property::getPriceLevel));
38+
39+
40+
/*所有价格等级的店铺列表*/
41+
Map<Integer, List<Property>> priceMap = properties.stream()
42+
.collect(Collectors.groupingBy(Property::getPriceLevel));
43+
}
44+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package Stream;
2+
3+
import java.util.*;
4+
5+
/**
6+
* @author huanfuan
7+
* @version 1.0
8+
* @date 2019/8/8 0008 下午 2:07
9+
*/
10+
public class streamTest02 {
11+
public static void main(String[] args) {
12+
List<List<String>> lists = new ArrayList<>();
13+
lists.add(Arrays.asList("apple", "click"));
14+
lists.add(Arrays.asList("boss", "dig", "qq", "vivo"));
15+
lists.add(Arrays.asList("c#", "biezhi"));
16+
17+
lists.stream()
18+
.flatMap(Collection::stream)
19+
.filter(str -> str.length()>2)
20+
.count();
21+
22+
23+
24+
}
25+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package Stream;
2+
3+
import java.util.Arrays;
4+
import java.util.Comparator;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
8+
/**
9+
* @author huanfuan
10+
* @version 1.0
11+
* @date 2019/8/8 0008 下午 2:13
12+
* Stream并行流
13+
*/
14+
public class streamTest03 {
15+
public static void main(String[] args) {
16+
Property p1 = new Property("叫了个鸡", 1000, 500, 2);
17+
Property p2 = new Property("张三丰饺子馆", 2300, 1500, 3);
18+
Property p3 = new Property("永和大王", 580, 3000, 1);
19+
Property p4 = new Property("肯德基", 6000, 200, 4);
20+
List<Property> properties = Arrays.asList(p1, p2, p3, p4);
21+
22+
23+
/*筛选出价格等级小于4,按照距离排序的2个店铺名*/
24+
properties.stream()
25+
.filter(p -> p.priceLevel<4)
26+
.sorted(Comparator.comparingInt(Property::getDistance))
27+
.map(Property::getName)
28+
.limit(2)
29+
.collect(Collectors.toList());
30+
31+
32+
/*调用 parallelStream 方法即能并行处理*/
33+
properties.parallelStream()
34+
.filter(p -> p.priceLevel<4)
35+
.sorted(Comparator.comparingInt(Property::getDistance))
36+
.map(Property::getName)
37+
.limit(2)
38+
.collect(Collectors.toList());
39+
40+
}
41+
}

java-Test/src/main/java/lambdaTest01.java renamed to java-Test/src/main/java/lambda/lambdaTest01.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package lambda;
2+
13
import java.util.Arrays;
24
import java.util.List;
35

java-Test/src/main/java/lambdaTest03.java renamed to java-Test/src/main/java/lambda/lambdaTest03.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
package lambda; /**
22
* @author huanfuan
33
* @version 1.0
44
* @date 2019/8/8 0008 上午 10:18

java-Test/src/main/java/lambdaTest06.java renamed to java-Test/src/main/java/lambda/lambdaTest06.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package lambda;
2+
13
import java.util.Arrays;
24
import java.util.List;
35

java-Test/src/main/java/lambdaTest07.java renamed to java-Test/src/main/java/lambda/lambdaTest07.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package lambda;
2+
13
import java.util.List;
24
import java.util.stream.Collectors;
35

java-Test/src/main/java/lambdaTest08.java renamed to java-Test/src/main/java/lambda/lambdaTest08.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
package lambda; /**
22
* @author huanfuan
33
* @version 1.0
44
* @date 2019/8/8 0008 上午 11:19

java-Test/src/main/java/lambdaTest09.java renamed to java-Test/src/main/java/lambda/lambdaTest09.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package lambda;
2+
13
import java.util.Arrays;
24
import java.util.List;
35
import java.util.stream.Collectors;

0 commit comments

Comments
 (0)