-7

HI I have sth like this:

          case 9:

            vehicleList.sort(Comparator.comparingInt(Vehicle::getMileage));
           // System.out.println(here);
            break;

How to print result of that sorting?

Mannualyy
  • 13
  • 3
  • 2
    `System.out.println(vehicleList);` – Unmitigated Apr 15 '21 at 15:25
  • 2
    Since simply printing the list seems so very obvious, perhaps you need this too: [How do I print my Java object without getting “SomeType@2f92e0f4”?](https://stackoverflow.com/q/29140402/5221149) – Andreas Apr 15 '21 at 15:27

2 Answers2

1
vehicleList.forEach(v -> System.out.println(v.getMileage));
gantoin
  • 158
  • 1
  • 8
0

Depending on how you want it output, you can either output the entire list like this:

    System.out.println(vehicleList);

This will call toString() on every element of vehicleList and display it in a single line. If you want each entry on its own line, you can put it into a loop:

    for(Vehicle vehicle : vehicleList)
    {
        System.out.println(vehicle);
    }

As mentioned by Andreas, in order to avoid getting a meaningless string of characters when you output your Vehicle, make sure to implement a toString() method in the class that returns a String.

Hawkeye5450
  • 672
  • 6
  • 18