前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布

lambda

原创
作者头像
用户6019926
修改2024-01-04 17:24:05
1090
修改2024-01-04 17:24:05
举报
文章被收录于专栏:lambda用例
代码语言:java
复制

public class MyListDemo {
    
    public static void main(String[] args) {
        List<BigDecimal> lis = new ArrayList<BigDecimal>();
        lis.add(new BigDecimal(123.23));
        lis.add(new BigDecimal(12.1));
        lis.add(new BigDecimal(122.2));
        lis.add(new BigDecimal(3.23));
        lis.add(new BigDecimal(23.23));
//        BigDecimal 计算
        BigDecimal nu = lis.stream().reduce(BigDecimal.ZERO, BigDecimal :: add);
        System.out.println("BigDecimal : " + nu);
        List<Double> list = new ArrayList<Double>();
        list.add(12.1);
        list.add(12.2);
        list.add(12.3);
        list.add(12.4);
        Optional<Double> num = list.stream().reduce((x,y)->x+y);
//        基本数字类型计算
        Double sum2=list.stream().reduce((double) 0,(x,y)->x+y);
        System.out.println("double : " + num);
        System.out.println(sum2);
        
        List<String> lists = new ArrayList<>();
        lists.add("ddd");
        lists.add("aaa");
        lists.add("bbb");
        lists.add("nnn");
        lists.add("ddd");
//        遍历
        lists.forEach(e -> {
            System.out.println("遍历 : " + e);
        });
        // 去重
        List<String> aa = lists.stream().distinct().collect(Collectors.toList());    
        System.out.println("去重:" + aa.toString());
//        排序
        List<String> sa = lists.stream().sorted().collect(Collectors.toList());
        System.out.println("排序:" + sa.toString());
//        转大写           
        List<String> ss = lists.stream().map(String::toUpperCase).collect(Collectors.toCollection(ArrayList::new));
        System.out.println("转大写:" + ss.toString());
        
        List<User> li = new ArrayList<>();
        li.add(new User("123", 321.0));
        li.add(new User("3", 321.1));
        li.add(new User("23", 321.2));
        double NUM = li.stream().map(User :: getNum).reduce(0.0, (X,Y) -> (X + Y));
         System.out.println("实体类属性计算:" + NUM);
         
        List<Double> numList = li.stream().map(User :: getNum).collect(Collectors.toList());
        System.out.println("得到user中num属性集合"+ numList.toString());
        
        Map<String,Object> maps = li.stream().collect(Collectors.toMap(User :: getKey, User :: getNum));
        System.out.println("list转map值为某个属性: " + maps.toString());
        
        Map<String,Object> maps1 = li.stream().collect(Collectors.toMap(User :: getKey, user -> user));
        System.out.println("list转map值为实体类: " + maps1.toString());
        
        List<Map<String,Object>> l = new ArrayList<>();
        Map<String,Object> ma = new HashMap<String, Object>();
        ma.put("123", 321);
        Map<String,Object> ma1 = new HashMap<String, Object>();
        ma1.put("123", 21);
        Map<String,Object> ma2 = new HashMap<String, Object>();
        ma2.put("123", 1);
        Collections.addAll(l, ma,ma1,ma2);
        
//        l.stream().map(str -> str.get("123"));
//        取某个key 值为键,当前map为值得到新集合
        Map<Object, Object> ls = l.stream().collect(Collectors.toMap( map -> map.get("123"),key -> key));
        System.out.println("list集合得到一个新的map: " + ls.toString());
//    根据实体类某个值去重得到信息list
        List<User> lt = new ArrayList<User>();
        lt.add(new User("1", 23.9));
        lt.add(new User("1", 22.9));
        lt.add(new User("2", 23.8));
        lt.add(new User("3", 23.1));
        List<User> unilt = lt.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User::getKey))), ArrayList::new));
        System.out.println("根据实体类中的某个值进行去重:" + unilt.toString());
        //        条件过滤
        List lsd = lt.stream().filter(user -> user.getKey().equals("1")).collect(Collectors.toList());
        System.out.println(lsd.toString());
        // 多条件过滤
                
        long count = lt .stream().filter( e-> e.getKey().equals("1")).filter(e -> e.getNum() == 23.9).count();
        System.out.print(count);
        
        Map<String,Double> map = new HashMap<>();
        map.put("1_U",1.1);
        map.put("5_U",1.5);
        map.put("2_U",1.2);
        map.put("3_U",1.3);
        map.put("4_U",1.4);
        // 取值最大
        Optional<Double> value = map.values().stream().max(Comparator.comparing(Double::doubleValue));
        Double v = value.get();
        System.out.println(v);
        // 取值最小的
        Optional<Double> ma = map.entrySet().stream().map(k -> k.getValue()).min(Comparator.comparing(Double::doubleValue));
        System.out.println(ma.get());
        // 根据最大的值返回key
        Optional<Map.Entry<String, Double>> m = map.entrySet().stream().filter(k -> k.getValue() == v).findAny();
        System.out.println(m.get().getKey());
    }
    }
}

代码语言:java
复制

package javaDemo.threadLocal;

public class User {

    private String key;
    
    private Double num;
    
    public User(String key,Double num) {
        this.key = key;
        this.num = num;
    }
         
    /**
     * @return key
     */
    
    public String getKey() {
        return key;
    }

    
    /**
     * @param paramtheparamthe{bare_field_name} to set
     */
    
    public void setKey(String key) {
        this.key = key;
    }

    
    /**
     * @return num
     */
    
    public Double getNum() {
        return num;
    }

    
    /**
     * @param paramtheparamthe{bare_field_name} to set
     */
    
    public void setNum(Double num) {
        this.num = num;
    }


    @Override
    public String toString() {
        return "User [key=" + key + ", num=" + num + "]";
    }    
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
作者已关闭评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档