jdk8新特性Lambda表达式

@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);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值