• 匿名内部类转换lambdas取代,以极大地提高源代码的可读性


    myCollection.stream().map(new Mapper<String,String>() {
      public String map(String input) {
        return new StringBuilder(input).reverse().toString();
      }
    });
    
    Predicate<String> isEmpty = new Predicate<String> {
        boolean test(String myString) {
            return myString.isEmpty();
        }
    }
    
    myCollection.stream().map(input -> new StringBuilder(input).reverse().toString());
    
    Predicate<String> isEmpty = myString -> myString.isEmpty();

     例子:

    public static List<Integer> getDiffNum(int i) {
            List<Integer> rs = new ArrayList<>();
            Integer[] numsTmp = nums.clone();
            for (int j = 0; j < i; j++) {
                int len = rand.nextInt(numsTmp.length);
                int tN = numsTmp[len];
                rs.add(tN);
                List<Integer> asList = transferArrayToList(numsTmp);
                asList.remove(len);
                Integer[] intes =new Integer[asList.size()];
                numsTmp = asList.toArray(intes);
            }
            rs.sort(new Comparator<Integer>() {
    
                @Override
                public int compare(Integer a, Integer b) {
                    return a.compareTo(b);
                }
                
            });
            return rs;
        }

    转换后:

    public static List<Integer> getDiffNum(int i) {
            List<Integer> rs = new ArrayList<>();
            Integer[] numsTmp = nums.clone();
            for (int j = 0; j < i; j++) {
                int len = rand.nextInt(numsTmp.length);
                int tN = numsTmp[len];
                rs.add(tN);
                List<Integer> asList = transferArrayToList(numsTmp);
                asList.remove(len);
                Integer[] intes =new Integer[asList.size()];
                numsTmp = asList.toArray(intes);
            }
            rs.sort((a,b) -> a.compareTo(b));
            return rs;
        }
  • 相关阅读:
    DataGridView 实现,折叠的Tree效果
    DEV 总结
    EWS:邮箱的一个开放的接口服务
    socket,模拟服务器、客户端通信
    在ASP.NET Core中构建路由的5种方法
    扩展方法、泛型、委托,的小案例
    操作Work、Excel、PDF
    d3实现家族树
    大数据时代的图表可视化利器——highcharts,D3和百度的echarts
    函数防抖与节流
  • 原文地址:https://www.cnblogs.com/zyf-yxm/p/13497897.html
Copyright © 2020-2023  润新知