zoukankan      html  css  js  c++  java
  • java内部类->lambda表达式->stream流

    一、内部类

      成员内部类

    public class A01inclass {
        public static void main(String[] args) {
            // 成员方法内访问成员内部类
            A01inclass a01inclass = new A01inclass();
            a01inclass.method();
    
            // 直接访问成员内部类
            A01inclass.InClass inClass = new A01inclass().new InClass();
            inClass.InClassmethod();
        }
    
        private String name = "林志玲";
    
        public void method() {
            InClass inClass = new InClass();
            System.out.println(inClass.name);
        }
    
        public class InClass {
            private String name = "佟丽娅";
    
            public void InClassmethod() {
                String name = "林允儿";
                //成员内部类访问局部变量
                System.out.println(name);
                //成员内部类访问成员内部类的成员变量
                System.out.println(this.name);
                //成员内部类访问成员变量
                System.out.println(A01inclass.this.name);
            }
        }
    }

       局部内部类

    public class A01inclass {
        public static void main(String[] args) {
            A01inclass a01inclass = new A01inclass();
            a01inclass.method();
        }
    
        public void method() {
            int num = 0;
            class Inclass {
                public void inmethod() {
                    System.out.println(num);//内部类中引用局部变量,该局部变量必须为final修饰的,final可省略不写
                }
            }
            new Inclass().inmethod();
        }
    }

      匿名内部类 

    二、lambda表达式

    public class Practice {
        public static void main(String[] args) {
            Integer[] arr = {5, 8, 3, 4, 9, 2, 7, 6, 1};
            ArrayList<Integer> list = new ArrayList<>();
            Collections.addAll(list, arr);
            //lambda表达式条件:必须为有且只有一个抽象方法的接口
            //参数只有一个时可省略【()】
            //方法体只有一行时,可省略【{}、return、;】,【{}、return、;】必须同时省略
            //当可由上下文推导出参数类型时,参数类型可省略
            Collections.sort(list, (a, b) -> b - a);
            System.out.println(list);
        }
    }

    三、函数式接口

    @FunctionalInterface//判断是否为函数式接口(只有一个抽象方法的接口)
    public interface Myinterface {
        void method();
    }
    public class A01funinterface {
        public static void main(String[] args) {
            //函数式接口常作为方法的参数或者返回值
    
            //函数式接口对象名 == lambda表达式
            //函数式接口对象名.方法(参数) == lambda表达式的方法体(方法体中引用参数)
    
            //双冒号(::)语法:
            // arg->(对象、类、super、this).方法(arg) == (对象、类、super、this)::方法
            // arg->new 类(arg) == 类::new
    
            A01funinterface obj = new A01funinterface();
    
    //        System.out.println(obj.supfun(() -> "你好"));
    
    //        obj.confun("你好", arg -> System.out.println(arg), arg -> System.out.println(arg));
    
    //        System.out.println(obj.perfun("你好", arg -> "你好".equals(arg), arg -> !"你好".equals(arg)));
    
            System.out.println(obj.funfun("abcdefg", arg -> arg.length()));
        }
    
        public String supfun(Supplier<String> sup) {
            return sup.get();
        }
    
        public void confun(String str, Consumer<String> con1, Consumer<String> con2) {
            con1.andThen(con2).accept(str);
        }
    
        public boolean perfun(String str, Predicate<String> pre1, Predicate<String> pre2) {
    //        return pre1.and(pre2).test(str);
            return pre1.or(pre2).test(str);
        }
    
        public Integer funfun(String str, Function<String, Integer> fun) {
            return fun.apply(str);
        }
    }

    四、流式编码思想(stream)

    public class A02stream {
        public static void main(String[] args) {
            //延迟方法(返回仍然是Stream):filter、map、limit、skip
            //终结方法:forEach、count
    
            List<String> strli = new ArrayList<>();
            strli.add("林志玲");
            strli.add("孟美岐");
            strli.add("黄婷婷");
            strli.add("程潇");
            strli.add("周洁琼");
            strli.add("鞠婧祎");
    
            strli.stream()
                    .map(arg -> arg + arg)//映射元素
                    .limit(5)//截取之前元素
                    .skip(1)//截取之后元素
                    .filter(arg -> arg.startsWith(""))//过滤元素
                    .forEach(arg -> System.out.println(arg));//遍历元素
    
            System.out.println(strli.stream().count());//获取元素个数
    
            List<Integer> intli = new ArrayList<>();
            intli.add(1);
            intli.add(2);
            intli.add(3);
    
            Stream<? extends Serializable> stream = Stream.concat(strli.stream(), intli.stream());//合并两个stream
            stream.forEach(arg -> System.out.println(arg));
        }
    }
  • 相关阅读:
    HDU 1058 Humble Numbers
    HDU 1421 搬寝室
    HDU 1176 免费馅饼
    七种排序算法的实现和总结
    算法纲要
    UVa401 回文词
    UVa 10361 Automatic Poetry
    UVa 537 Artificial Intelligence?
    UVa 409 Excuses, Excuses!
    UVa 10878 Decode the tape
  • 原文地址:https://www.cnblogs.com/linding/p/13520510.html
Copyright © 2011-2022 走看看