0

Something strange: I cannot use a method implemented in an imported Class. I have the class called "Person.java" which contains an implemented method called "isFrom()", as you can see below:

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

}

Thus, I have created another class called "InternalIterator.java" with the purpose to try to use Streams along the set created by method "makeList()" of the previous class. Inside the stream I try to call "isFrom()" method but it is like if it doesn't find the method:

Cannot resolve method 'isFrom(java.lang.String)

And that's strange, because I have implemented that method in Person class. The code of this second class is represented below:

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

    }
}

I can't figure out, I also tryng with Person.isFrom() but nothing to do

Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
Giacomo Brunetta
  • 1,409
  • 3
  • 18
  • 38

2 Answers2

2

The reason is that you are using a raw type.

List persons = Person.makeList();

and hence when you create a stream out of persons the type is inferred as an Object.

Instead, do

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

Recommended reading:

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

Thiyagu
  • 17,362
  • 5
  • 42
  • 79
  • Oh! Solved!! Thank you! I couldn't figure it out because that's an example of a e-learning guide..thus the code represented in this example is not true – Giacomo Brunetta May 18 '18 at 14:56
1

Use this :List<Person> persons = Person.makeList();

Abder KRIMA
  • 3,418
  • 5
  • 31
  • 54