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
  • 5,031
  • 5
  • 37
  • 64
AdHominem
  • 1,204
  • 3
  • 13
  • 32

0 Answers0