socal_javaguy socal_javaguy - 2 months ago 7
Java Question

Java 8 - List Results are Printing Not Actual Content

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) { = name;
this.age = age;

public String getName() {
return name;

public void setName(String 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) {

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:


What am I possibly doing wrong?


You are missing a toString implementation in your Person class.