java8 function lambda 函数式编程

package com.java8.test;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * hashmap
 * concurrentHashMap
 * jvm
 */
public class Test1 {

    //引用自:
    //https://www.bilibili.com/video/av62117143

    /**
     * only focus on use
     * base on abstraction
     */

    /**
     * hashmap
     * hashcode方法
     * equals方法
     *
     * 数组+链表
     * java8:数组+链表+红黑树
     *
     * concurrentHashMap
     * 1.8以前分段
     * 1.8以后取消分段,采用cas,因为段不太好评定
     * 数组+链表+红黑树
     */

    /**
     * java8以前 hotspot
     * 栈 堆(永久区或者叫方法区:几乎不会被回收)
     *
     * hotspot
     * oracle jrocket
     * ibm J9
     * taobao jvm
     *
     * java8以后,去掉永久区
     * 改为元空间 metaSpace
     * 采用的为物理内存,oom变少了
     *
     */

    /**
     * lambda
     * 将代码像数据一样传递
     * data is not different from procedure
     * 数据和过程(代码)没有区别,参考sicp
     *
     * 使用lambda表达式,解决匿名内部类的使用
     *
     * 例子2:
     * 过滤器
     * 解决方式一:使用设计模式,策略涉及模式
     * 解决方式二:匿名内部类
     * 解决方式三:lambda表达式,lambda与Function(函数)
     * 解决方式四:使用stream api(内含lambda表达式),lambda与Stream
     * 方式三和四的本质都是函数式编程,function,即函数为第一等公民
     *
     * java中的
     * (i) -> i的表达式
     * 相当于js里面的
     * x => x的表达式
     * 这是个匿名函数,可以给这个函数起个名字
     * f = x => x的表达式
     * java中给匿名函数起名字
     * Function<Employ, Boolean> f = (e) -> e.getSalary() > 5000;
     * Function f = (e) -> e.getSalary() > 5000;
     * js中调用
     * f(x)
     * java中调用
     * f.apply(e)
     *
     */
    public void testLambda(){
        TreeSet<Integer> ts = new TreeSet<>((i, j) -> Integer.compare(i, j));

        Comparator<Integer> com = (i, j) -> Integer.compare(i, j);
        TreeSet<Integer> ts2 = new TreeSet<>(com);
    }

    public interface MyPredicate<T> {
        public boolean test(T t);
    }

    public class Employ{
        private String name;
        private int salary;

        public String getName() {
            return name;
        }

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

        public int getSalary() {
            return salary;
        }

        public void setSalary(int salary) {
            this.salary = salary;
        }
    }

    public List<Employ> filterEmploy(List<Employ> employs, MyPredicate<Employ> myPredicate){
        List<Employ> r = new ArrayList<>();
        for (Employ employ : employs){
            if (myPredicate.test(employ)){
                r.add(employ);
            }
        }
        return r;
    }

    public void test2(){
        List<Employ> list = new ArrayList<>();
        List<Employ> r = filterEmploy(list, (e) -> e.getSalary() > 5000);
        System.out.println(r);

        //(e) -> e.getSalary() > 5000即为匿名函数
        //可以给他起个名字
        Function<Employ, Boolean> f = (e) -> e.getSalary() > 5000;
        List<Employ> r2 = filterEmploy(list, (MyPredicate<Employ>) f);
        System.out.println(r2);
        //使用Function,调用时为apply
        //f.apply()

        //->的返回值为一个函数
        MyPredicate<Employ> pre = (e) -> e.getSalary() > 5000;
        List<Employ> r3 = filterEmploy(list, pre);
        System.out.println(r3);
        //使用具体的函数,调用时为具体的方法
        //pre.test()
    }

    /**
     * 函数式接口:只有一个抽象方法的接口,
     * 为什么是只有一个?
     * 因为在真正的函数式语言中,如js,scheme等,f为一个函数,不需要类的支撑
     * java中函数需要类的支撑,也就是一个类是一个方法
     * f为"一个"函数,所以类(接口)中只有一个抽象方法,才叫函数式接口
     * 对比:https://blog.csdn.net/yilu_beiyu/article/details/122811729中
     * map filter accumulate,就相当于一个map,filter等方法
     *
     *
     * 一个类中有多个方法,每个方法也可以用function来表示,核心还是方法function,类和接口不是核心
     *
     *
     * 可以加@FunctionalInterface修饰,也可以不加,
     * 因为该注解只是起到检查作用
     *
     * 函数定义:
     * js:=>或者是{}
     * java: ->或者是{}
     *
     * () -> ...
     * (i) -> ... ;;只有一个参数,小括号可以省略不写
     * (i, j) -> ...
     */


    //接口中的方法不能引用
    //当然可以说是因为接口中方法没有实现,所以不能引用
//    public interface BiTestFuncInterface<T> {
//        public void test(T t);
//
//        public String toStr(T t);
//    }
//
//    public void testBiFuncInterface(String s, BiTestFuncInterface<String>::test){
//
//    }

    /**
     * base on abstraction
     * 内置的方法(函数)
     * Consumer:该方法用于消费元素
     * Supplier:该方法用于产生元素
     * Function:该方法就是普通的方法
     * Predicate:该方法用于前置判断
     *
     */
    public void testAll(){
        testConsumer(1000d, (i) -> System.out.println("i is " + i));
        testSupplier(10, () -> new Random().nextInt(100));

    }
    public void testConsumer(Double money, Consumer<Double> c){
        c.accept(money);
    }
    public void testSupplier(int count, Supplier<Integer> s){
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < count; i++){
//            list.add(s.get());
            Integer integer = s.get();
            list.add(integer);
        }
    }

    //需求:用于处理字符串
    public String strHandler(String str, Function<String, String> fun){
        return fun.apply(str);
    }

    //需求:将满足条件的字符串,放入集合中
    public List<String> filterStr(List<String> list, Predicate<String> pre){
        List<String> strList = new ArrayList<>();
        for (String str : list) {
            if(pre.test(str)){
                strList.add(str);
            }
        }
        return strList;
    }


    public void test3(){
        /**
         * 相当于js里面的
         * var f = ...
         * java中
         * ::可以引用方法
         * ->可以定义方法
         *
         * 数组也可以使用::new
         * 
         */
        Consumer<String> con = System.out::println;
        Consumer<String> con2 = (s) -> System.out.println(s);

    }


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值