Using Java 8, I was checking out some of its new features...
Created the following class:
public class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Created the PersonApp class:
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import static java.util.Comparator.comparing;
public class PersonApp {
public static void printSorted(List<Person> people, Comparator<Person> comparator) {
people.stream()
.sorted(comparator)
.forEach(System.out::println);
}
public static void main(String[] args) {
List<Person> people = new ArrayList<>();
people.add(new Person("Sara", 12));
people.add(new Person("Mark", 43));
people.add(new Person("Bob", 12));
people.add(new Person("Jill", 64));
printSorted(people, comparing(Person::getAge).thenComparing(Person::getName));
}
}
When I run this class, I get the following instead of the values I wanted to see:
Person@682a0b20
Person@3d075dc0
Person@214c265e
Person@448139f0
What am I possibly doing wrong?