好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java8使用流的filter来筛选数据的实现

本文主要介绍了java8使用流的filter来筛选数据的实现,分享给大家,具体如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

package chapter1;

 

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

import static java.util.stream.Collectors.groupingBy;

 

public class stream {

   //比如说 你需要在一个列表中 筛选出所以重量大于150的苹果,然后按照颜色分组

   //按常规的筛选写法 就是在循环里面 迭代筛选

 

   public static void main(String[] args) {

     List<Apple> appleList = new ArrayList<>();

 

     //常规写法

     Map<String, List<Apple>> AppMap = new HashMap<>();

     for (Apple apple : appleList) {

       if (apple.getWeight() > 150 ) { //如果重量大于150

         if (AppMap.get(apple.getColor()) == null ) { //该颜色还没分类

           List<Apple> list = new ArrayList<>(); //新建该颜色的列表

           list.add(apple); //将苹果放进去列表

           AppMap.put(apple.getColor(),list); //将列表放到map中

         } else { //该颜色分类已存在

           AppMap.get(apple.getColor()).add(apple); //该颜色分类已存在,则直接放进去即可

         }

       }

     }

 

     //如上方式 就可以筛选出来所有的150克大小以上的苹果,并按颜色分类

 

 

 

     //方式二 使用java8提供的流api实现 这种叫内部迭代

     Map<String, List<Apple>> AppMap2=appleList.stream().filter((Apple a)->a.getWeight()> 150 ) //筛选出大于150的

         .collect(groupingBy(Apple::getColor)); //按颜色分组 最后得到map

    

 

   }

 

 

   class Apple {

 

     private String color; //颜色

     private Integer weight; //重量

 

     public String getColor() {

       return color;

     }

 

     public void setColor(String color) {

       this .color = color;

     }

 

     public Integer getWeight() {

       return weight;

     }

 

     public void setWeight(Integer weight) {

       this .weight = weight;

     }

   }

}

到此这篇关于java8使用流的filter来筛选数据的实现的文章就介绍到这了,更多相关java8 filter筛选数据内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/qq_20009015/article/details/84891638

查看更多关于java8使用流的filter来筛选数据的实现的详细内容...

  阅读:27次