美文网首页
java8 stream的一些方法

java8 stream的一些方法

作者: 尉昌达 | 来源:发表于2020-01-16 17:09 被阅读0次
    List<Fruit> fruitList = Arrays.asList(
                    new Fruit("apple", 6,0),
                    new Fruit("apple", 6,1),
                    new Fruit("banana", 7,1),
                    new Fruit("banana", 7,1),
                    new Fruit("banana", 7,1),
                    new Fruit("grape",8,2));
    

    1.filter

    从集合中筛选给定条件的子集

    fruitList.stream().filter(t->t.getType()==1).collect(Collectors.toList());
    

    2.map

    从集合中取值组成集合

    List<String> collect = fruitList.stream().map(Fruit::getName).distinct().collect(Collectors.toList());
    
    //结果
    [apple, banana, grape]
    

    3.toMap

    userEntity根据id分组

    Map<Long, UserEntity> userMap = userEntities.stream().collect(Collectors.toMap(UserEntity::getUserId, Function.identity()));
    

    4.reduce

    //给初值,在累加求和
    List<Integer> ints = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
            System.out.println("ints sum is:" + ints.stream().reduce(1, (sum, item) -> sum + item));
    //结果
    56
    

    5.groupingBy

    把fruitList先按name分组,并按名称排序,在此基础上继续对type分组

    Map<String, Map<Integer, List<Fruit>>> collect = fruitList.stream().collect(Collectors.groupingBy(Fruit::getName, TreeMap::new, Collectors.groupingBy(Fruit::getType)));
            collect.forEach((name,value)->{
                value.forEach((type,value2)->{
                    System.out.println(name+"  "+type+"   "+value2);
                });
            });
    
    //运行结果
    apple  0   [Fruit{name='apple', price=6.0, type=0}]
    apple  1   [Fruit{name='apple', price=6.0, type=1}]
    banana  1   [Fruit{name='banana', price=7.0, type=1}, Fruit{name='banana', price=7.0, type=1}, Fruit{name='banana', price=7.0, type=1}]
    grape  2   [Fruit{name='grape', price=8.0, type=2}]
    

    groupBy,toMap,join

    //groupBy分组后,取自己需要的值
    List<Person> list = new ArrayList();
            list.add(new Person(1, "haha",10));
            list.add(new Person(2, "rere",10));
            list.add(new Person(4, "rere",13));
            list.add(new Person(3, "fefe",12));
          
            HashMap<Integer, Map<Integer, String>> collect = list.stream().collect
                    (Collectors.groupingBy(Person::getYear, HashMap::new, Collectors.toMap(Person::getId, Person::getName)));
            Map<Integer, List<Person>> collect2 = list.stream().collect(Collectors.groupingBy(Person::getYear));
    //        List<Person> collect = list.stream().sorted((o1,o2)->o2.getYear()-o1.getYear()).limit(20).collect(Collectors.toList());
            String collect1 = list.stream().map(Person::getName).collect(Collectors.joining(","));
            System.out.println(collect);
            System.out.println(collect2);
            System.out.println(collect1);
    
    //运行结果
    {10={1=haha, 2=rere}, 12={3=fefe}, 13={4=rere}}
    {10=[Person{id=1, name='haha', year=10}, Person{id=2, name='rere', year=10}], 12=[Person{id=3, name='fefe', year=12}], 13=[Person{id=4, name='rere', year=13}]}
    haha,rere,rere,fefe
    

    对groupby中的内容拼接

    Map<String, List<Fruit>> collect = fruitList.stream().collect(Collectors.groupingBy(t -> t.getName() + t.getPrice()));
    
    //结果
    {grape8.0=[Fruit{name='grape', price=8.0, type=2}], apple6.0=[Fruit{name='apple', price=6.0, type=0}, Fruit{name='apple', price=6.0, type=1}], banana7.0=[Fruit{name='banana', price=7.0, type=1}, Fruit{name='banana', price=7.0, type=1}, Fruit{name='banana', price=7.0, type=1}]}
    
    List<Item> items = Arrays.asList(
        new Item("apple",10),
        new Item("banana",20),
        new Item("orange",10),
        new Item("watermelon",10),
        new Item("papaya",20),
        new Item("apple",10),
        new Item("banana",10),
        new Item("apple",20)
    );
    
    Map<String,Long> counting = items.stream().collect(Collectors.groupingBy(Item::getName,Collectors.counting()));
    System.out.println(counting);
    
    Map<String,Integer> sum = items.stream().collect(Collectors.groupingBy(Item::getName,Collectors.summingInt(Item::getQty)));
    System.out.println(sum);
    
    //输出
    {
      papaya=1,banana=2,apple=3,orange=1,watermelon=1
    }
    {
      papaya=20,banana=30,apple=40,orange=10,watermelon=10
    }
    

    6.Collectors.mapping

    在group分组后,取出每组对象中需要的值到list中

    Map<String, List<Double>> collect1 = fruitList.stream().collect(Collectors.groupingBy(Fruit::getName, TreeMap::new, Collectors.mapping(Fruit::getPrice, Collectors.toList())));
    
    //运行结果
    {apple=[6.0, 6.0], banana=[7.0, 7.0, 7.0], grape=[8.0]}
    

    7.排序

    //上架的销量前20的货品
            List<GoodsEntity> collect = this.list(wrapper.eq("status",1)).stream().sorted(Comparator.comparing(GoodsEntity::getSalesVolume).reversed()).limit(20).collect(Collectors.toList());
    
    //dataForms根据日期从小到大排序
    Collections.sort(dataForms, (o1, o2) -> {
                Date dt1 = DateUtils.stringToDate(o1.getDate(),"yyyy-MM-dd");
                Date dt2 = DateUtils.stringToDate(o2.getDate(),"yyyy-MM-dd");
                if(dt1.getTime()<dt2.getTime()){
                    return -1;
                }else {
                    return 1;
                }
            });
    

    相关文章

      网友评论

          本文标题:java8 stream的一些方法

          本文链接:https://www.haomeiwen.com/subject/xvmfzctx.html