0

This code returns the length of the longest String in a list of Strings. I have two solutions of which one works and the other doesn't

Correct

System.out.println(listOfConsecutiveOneStrings.stream()
    .max(Comparator.comparing(String::length))
    .max(Comparator.comparing(String::length))
    .map(onesSequence -> onesSequence.length())
    .get());

Wrong

System.out.println(listOfConsecutiveOneStrings.stream()
    .max(Comparator.comparing(String::length))
    .map(onesSequence -> onesSequence.length())
    .max(Integer::max)
    .get());

Why is the first snippet correct and the second one incorrect?

randers
  • 4,652
  • 4
  • 31
  • 61
AdHominem
  • 1,067
  • 2
  • 12
  • 32

0 Answers0