无法使用从Stream和Lambda从另一个类导入的方法

时间:2018-05-18 14:52:06

标签: java methods lambda import java-stream

奇怪的是:我无法使用导入的Class中实现的方法。 我有一个名为“Person.java”的类,它包含一个名为“isFrom()”的实现方法,如下所示:

import java.util.List;
import java.util.function.Function;

public class Person implements Comparable<Person> {
  private String name;
  private String role;
  private String city;
  private LocalDate birthdate;

  public Person(String name, String role, String city, LocalDate birthdate) {
    super();
    this.name = name;
    this.role = role;
    this.city = city;
    this.birthdate = birthdate;
  }

  public boolean isFrom(String name) {
    return city.equals(name);
  }

  public String getRole() {
    return role;
  }

  @Override
  public String toString() {
    return "Stakeholder [name=" + name + ", role=" + role + ", city=" + city + ", birthdate=" + birthdate + "]";
  }

  public boolean isOlderThan(Person other) {
    return birthdate.isBefore(other.birthdate);
  }

  public boolean isYoungerThan(Person other) {
    return birthdate.isAfter(other.birthdate);
  }

  /** 
   * Questa è una funzione di ordine superiore (higher-order function) perché restituisce una funzione
   * 
   */
  public static Function<Person, LocalDate> toBirthdate() {
    return p -> p.birthdate;
  }

  public LocalDate getBirthdate() {
    return birthdate;
  }

  public String getName() {
    return name;
  }

  @Override
  public int compareTo(Person o) {
    return name.compareTo(o.name);
  }

  public String getCity() {
    return city;
  }

  public static List<Person> makeList() {
    List<Person> persons = new ArrayList<>();
    persons.add(new Person("Paolo Bianchi", "Tecnico", "Milano", LocalDate.of(1960, 4, 15)));
    persons.add(new Person("Mario Rossi", "Tecnico", "Milano", LocalDate.of(1981, 3, 25)));
    persons.add(new Person("Lucia Verdi", "Tecnico", "Milano", LocalDate.of(1955, 5, 1)));
    persons.add(new Person("Marzia Gentile", "Commerciale", "Milano", LocalDate.of(1969, 6, 3)));
    persons.add(new Person("Anna De Rosa", "Responsabile", "Milano", LocalDate.of(1974, 4, 22)));
    persons.add(new Person("Paola De Blasi", "Tecnico", "Torino", LocalDate.of(1967, 8, 11)));
    persons.add(new Person("Maria Abate", "Tecnico", "Torino", LocalDate.of(1977, 10, 21)));
    persons.add(new Person("Federico Cacciari", "Commerciale", "Torino", LocalDate.of(1962, 12, 17)));
    persons.add(new Person("Arturo Pace", "Commerciale", "Roma", LocalDate.of(1979, 10, 19)));
    persons.add(new Person("Giovanni Tomassini", "Commerciale", "Genova", LocalDate.of(1974, 9, 8)));
    return persons;
  }

}

因此,我创建了另一个名为“InternalIterator.java”的类,目的是尝试沿前一个类的方法“makeList()”创建的集合使用Streams。在流中我尝试调用“isFrom()”方法,但它就像是找不到方法:

  

无法解析方法'isFrom(java.lang.String)

这很奇怪,因为我在Person类中实现了该方法。第二类的代码如下所示:

import java.util.List;
import ardea.project.Person;
public class InternalIterator {
    public static void main(String[] args) {
        List persons = Person.makeList();

        long count = persons.stream()
                .filter(p -> p.isFrom("Milano"))
                .count();
        System.out.println("Persone di Milano: " + count);

    }
}

我无法弄清楚,我也尝试Person.isFrom(),但无所事事

2 个答案:

答案 0 :(得分:2)

原因是您使用的是原始类型

List persons = Person.makeList();

因此当您从persons创建流时,该类型将被推断为 Object

相反,做

List<Person> persons = Person.makeList();

推荐阅读:

What is a raw type and why shouldn't we use it?

答案 1 :(得分:1)

使用此:List<Person> persons = Person.makeList();