/**
* 使用.map()一般是对原来数据集进行操作后结果存放到另一个新创建的集合中,然后调用.collect方法将计算结果存放到新结果集合中
* .filter()是对输入及进行过滤,将满足filter()方法中条件数据返回;
* .map() 是对输入集进行函数运算,并且必须有return语句,将对输入集进行函数运算后结果返回
* list.stream().map(执行业务方法).collect(Collector.toList())
*/

1.map
    public static void mapMergeAboutList(){
        List<String> lists= Arrays.asList("one","two","three","four","five","six");
        List<String> listList=lists.stream().filter(a->a.equalsIgnoreCase("six")).collect(Collectors.toList());
        System.out.println("====lists===="+listList);
        List<String> lsits1=lists.stream().map(String::toUpperCase).collect(Collectors.toList());
        System.out.println("=====lsits1======"+lsits1);
        List result=lists.stream().map(a->{
            if(a.equalsIgnoreCase("two"))
                a=a+"def";
            return a;
        }).collect(Collectors.toList());
        System.out.println("====result===="+result);
    }
结果
====lists====[six]
=====lsits1======[ONE, TWO, THREE, FOUR, FIVE, SIX]
====result====[one, twodef, three, four, five, six]


2.filter
下边代码Collector.tomap必须时在filter()后使用
    public static void main(String[] args) {
        ListToMapTest();
    }

    public static void ListToMapTest(){
        Apple apple1=new Apple();
        Apple apple2=new Apple();
        apple1.setColor("red");
        apple1.setWeight(3.0);
        apple2.setColor("green");
        apple2.setWeight(5.0);
        List<Apple> appleList=Arrays.asList(apple1,apple2);
        Map<String,Double> mapResult=appleList.stream().filter(a->a.getColor()!="yellow")
                .collect(Collectors.toMap(((a)->a.getColor()),(a)->a.getWeight()));
        System.out.println("mapResult:"+mapResult);
    }

result:
mapResult:{red=3.0, green=5.0}

 

posted on 2020-06-11 17:03  colorfulworld  阅读(487)  评论(0编辑  收藏  举报