在java

时间:2018-02-25 13:45:23

标签: java lambda

我有一个接受一些功能接口的方法,它在使用lambda表达式调用时工作正常。

然而,当我从提供lambda表达式内联的main方法尝试相同的语法时(而不是像第一种情况那样定义它的实例并调用方法),它给了我一个语法错误。我错过了什么?

这是代码

    package test.lambdas;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

public class Person {

    private String givenName;
    private String surName;
    private int age;

    public String getGivenName() {
        return givenName;
    }
    public Person setGivenName(String givenName) {
        this.givenName = givenName;
        return this;
    }
    public String getSurName() {
        return surName;
    }
    public Person setSurName(String surName) {
        this.surName = surName;
        return this;
    }
    public int getAge() {
        return age;
    }
    public Person setAge(int age) {
        this.age = age;
        return this;
    }
    public String getGender() {
        return gender;
    }
    public Person setGender(String gender) {
        this.gender = gender;
        return this;
    }
    public String geteMail() {
        return eMail;
    }
    public Person seteMail(String eMail) {
        this.eMail = eMail;
        return this;
    }
    public String getPhone() {
        return phone;
    }
    public Person setPhone(String phone) {
        this.phone = phone;
        return this;
    }
    public String getAddress() {
        return address;
    }
    public Person setAddress(String address) {
        this.address = address;
        return this;
    }
    private String gender;
    private String eMail;
    private String phone;
    private String address;

    public String toString(){
        return givenName;
    }


    static void print(List<Person> l, Predicate<Person> p, Function<Person, String> f, Consumer<String> c){
        for(Person p1 : l){
            if(p.test(p1)){
                c.accept(f.apply(p1));
            }
        }
    }

    public static List<Person> getPersons(){
        List<Person> lp = new ArrayList<>();
        Person p = new Person();
        p.setGivenName("X").setSurName("Y").setAge(15).seteMail("test@test.com").setGender("Male").setAddress("Test Location");
        Person p1 = new Person();
        p1.setGivenName("A").setSurName("B").setAge(34).seteMail("test1@test.com").setGender("Male").setAddress("Test Location1");
        Person p2 = new Person();
        p2.setGivenName("M").setSurName("N").setAge(33).seteMail("test2@test.com").setGender("Male").setAddress("Test Location2");      
        lp.add(p);lp.add(p1);lp.add(p2);        
        return lp;
    }

    public static void main(String[] args) {
        List<Person> l = getPersons();

        Predicate<Person> p = p1 -> p1.getAge() > 18;
        Function<Person, String> f = p2 -> p2.geteMail();
        Consumer<String> c = p3 -> System.out.println("Email  "+p3+" is a major");

        print(l, p, f, c); //this works fine

        //lets do it inline
        for(Person p1 : l){     
            c.accept(f.apply(p1 -> p1.getAge() > 18)); //this throws error detailed below   
        }
    }
}

该行

c.accept(f.apply(p1 -> p1.getAge() > 18));

抛出以下错误

  1. at f.apply =&gt;函数类型中的方法apply(Person)不适用于参数((p1) - &gt; {})

  2. (在p1 - &gt; p1.getAge()&gt; 18)=&gt;此表达式的目标类型必须是功能接口

    c.accept()接受打印出来的String。 f.apply()接受一个Person并返回一个字符串(他的电子邮件),其中c.accept打印出来。我知道我尝试向f.apply()提供(p1 - &gt; p1.getAge()&gt; 18)是错误的。我的问题是如何重新内联静态打印方法或不可能这样做?

1 个答案:

答案 0 :(得分:1)

变化:

c.accept(f.apply(p1));

为:

f

这是因为函数Person使print不是函数。

修改

如果你想复制l.stream().filter(p).forEach(p1 -> c.accept(f.apply(p1))); 方法内联的内容,那么你可以这样做:

fun ambigousTypeEval(sport: String) = when (sport) {
  "cricket" -> -1
  else -> "I like $sport"
}