jdk8新特性Lambda表达式

不羁岁月 提交于 2020-02-01 01:09:41
@FunctionalInterface
interface IntNumber {

    int count(int i);
    default int add(int x,int y){
       return x + y;
    }
    
}
public class LambdaDemo{
    public static void main(String[] args){

        IntNumber i1 = (i)-> i*2;
        System.out.println(i1.count(100));
        

    }
}

 

@FunctionalInterface 注解表示一个函数接口

#表达式

#()内是所需的参数,返回值为i*2

(100) -> i*2;

#default add方法,default关键字在jdk8中新增加的,底层默认实现

#静态方法的引用,表达式 

引用静态方法:  类名称::static方法名称;

例:

//P描述参数,R描述返回值
@FunctionalInterface
interface IFunction<P,R>{
    public R change(P p);
}

public class JavaDemo {

    public static void main(String[] args){
        IFunction<Integer,String> fun = String :: valueOf;
        System.out.println(fun.change(100).length());
    }
}

引用某个实例对象的方法:  实例化对象::普通方法;

例:

@FunctionalInterface
interface IFunction<R>{
    public R change();
}

public class JavaDemo {

    public static void main(String[] args){
        IFunction<String> fun = "like" :: toUpperCase;
        System.out.println(fun.change());
    }
}

引用特定类型的方法:  特定类型::普通方法;

例:

@FunctionalInterface
interface IFunction<P>{
    public int compare(P p1,P p2);
}

public class JavaDemo {

    public static void main(String[] args){
        IFunction<String> fun = String :: compareTo;
        System.out.println(fun.compare("A","a"));
    }
}

引用构造方法:  类名称 :: new;

例:

class Person{
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "姓名:"+ this.name +"," + "年龄" + this.age;
    }
}
@FunctionalInterface
interface IFunction<R>{
    public R create(String name,int age);
}

public class JavaDemo {

    public static void main(String[] args){
        IFunction<Person> fun = Person :: new;
        System.out.println(fun.create("A",1));
    }
}

 

 

public class LambdaDemo{
    public static void main(String[] args){
        
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        //使用原始的变量方式
        for(String str : list){
            System.out.println(str);
        }
        //使用Lambda表达式
        list.forEach((str) -> System.out.println(str));
        //倒序排序
        list.stream().sorted((a,b)->b.compareTo(a)).forEach(System.out::println);
        
    }
}
public class Person {
    private String name;
    private Integer age;
    private String country;
    private char sex;

    public Person(String name, Integer age, String country, char sex) {
        this.name = name;
        this.age = age;
        this.country = country;
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", country='" + country + '\'' +
                ", sex=" + sex +
                '}';
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }
}

 

public static void main(String[] args){
    List<Person> personList = new ArrayList<>();
    personList.add(new Person("欧阳雪",18,"中国",'F'));
    personList.add(new Person("Tom",24,"美国",'M'));
    personList.add(new Person("Harley",22,"英国",'F'));
    personList.add(new Person("向天笑",20,"中国",'M'));
    personList.add(new Person("李康",22,"中国",'M'));
    personList.add(new Person("小梅",20,"中国",'F'));
    personList.add(new Person("何雪",21,"中国",'F'));
    personList.add(new Person("李康",22,"中国",'M'));


    //根据年龄倒序来排序输出
    personList.stream().sorted((p,a) -> a.getAge().compareTo(p.getAge())).forEach(System.out::println);
    //输出所有中国人
    personList.stream().filter((person -> person.getCountry().equals("中国"))).forEach(System.out::println);
    //输出所有中国人前两个
    personList.stream().filter((person -> person.getCountry().equals("中国"))).limit(2).forEach(System.out::println);
    //获取年龄最大的人
    Optional<Person> optional = personList.stream().max((p1, p2) -> p1.getAge().compareTo(p2.getAge()));
    System.out.println("年龄最大:"+optional);
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!