程序员一代码搬运工
【Java学习心得和Java学习技术个人总结】

jdk1.8表达式学习

package com.sdream.ls.client;

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author liyongqiang
 * @title: Test1
 * @description: TODO
 * @date 2019/9/209:43
 */
public class Test1 {

    public static void main(String[] args) {
        List<User> list=new ArrayList<>();
        list.add(new  User("1","张三","一年级",95));
        list.add(new  User("2","李四","二年级",96));
        list.add(new  User("3","王五","三年级",97));
        list.add(new  User("4","朱留","四年级",100));

        //遍历
        list.forEach((o)-> System.out.println(""+o.toString()));
        //遍历
        list.forEach(o-> System.out.println(""+o.toString()));
        //转 hashSet
        Set<User> set1=list.stream().collect(Collectors.toSet());
        //转  hashSet
        Set<String> set2=list.stream().map(User::getName).collect(Collectors.toSet());

        //转treeSet
        TreeSet<String> set3=list.stream().map(User::getName).collect(Collectors.toCollection(TreeSet::new));

        //转 map
        Map<String,String> map1=list.stream().collect(Collectors.toMap(User::getId,User::getLevel));

        //转map
        Map<String,String> map2=list.stream().collect(Collectors.toMap(User::getId,User::getName,(token, token2) -> token));

        //分组
        Map<String,List<User>> map3=list.stream().collect(Collectors.groupingBy(User::getName));

        //分组count

        Map<String,Long> map4=list.stream().collect(Collectors.groupingBy(User::getName,Collectors.counting()));

        //分组求和

        Map<String,Integer>map5=list.stream().collect(Collectors.groupingBy(User::getName,Collectors.summingInt(User::getScore)));


        //过滤条件
        List<User> temp1=list.stream().filter(o->"1".equals(o.getId())).collect(Collectors.toList());

        //多个过滤条件
        Predicate<User> scoFilter=(o)->(o.getScore()>25);
        Predicate<User> clsFilter=(o)->("三年级".equals(o.getLevel()));
        List<User> temp2=list.stream().filter(scoFilter).filter(clsFilter).collect(Collectors.toList());

        //取前多少个
        List<User> temp3=list.stream().limit(2).collect(Collectors.toList());


        //升序排序
        List<User>temp4=list.stream().sorted((u1,u2)->(u1.getScore()-u2.getScore())).collect(Collectors.toList());

        //降序排序
        List<User>temp5=list.stream().sorted((u1,u2)->(u2.getScore()-u1.getScore())).collect(Collectors.toList());

        //升序 第一个
        User user1=list.stream().max((u1,u2)->(u1.getScore()-u2.getScore())).get();


        //降序第一个
        User user2=list.stream().min((u1,u2)->(u1.getScore()-u2.getScore())).get();


        //求和
        int  sum=list.parallelStream().mapToInt(p->p.getScore()).sum();

        IntSummaryStatistics stats=list.stream().mapToInt((x)->x.getScore()).summaryStatistics();

        System.out.printf("最大的数字"+stats.getMax());
        System.out.printf("最小的数字"+stats.getMin());
        System.out.printf("所有数字的综合"+stats.getSum());
        System.out.printf("所有数字的平均值"+stats.getAverage());







    }


}
class User{
    private  String id;
    private String name;
    private  String  level;
    private   int   score;

    public User(String id, String name, String level, int score) {
        this.id = id;
        this.name = name;
        this.level = level;
        this.score = score;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", level='" + level + '\'' +
                ", score=" + score +
                '}';
    }
}
赞(0)
未经允许不得转载:程序员一代码搬运工 » jdk1.8表达式学习
分享到: 更多 (0)