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