I am trying to use Java Streams to collects all the Strings of the greatest length from my list:
List<String> strings = Arrays.asList("long word", "short", "long wwww", "llll wwww", "shr");
List<String> longest = strings.stream()
            .sorted(Comparator.comparingInt(String::length).reversed())
            .takeWhile(???)
            .collect(Collectors.toList());
I would like my longest to contain {"long word", "long wwww", "llll wwww"}, because those are the Strings that have the greatest lengths. In case of only having one of the Strings with greatest length, I am obviously expecting the resulting List to contain only that element.
I tried to first sort them in order to have the greatest length appear in the first element, but I am unable to retrieve the length of the first element in the stream. I could try something like peek():
static class IntWrapper {
    int value;
}
public static void main(String[] args) throws IOException {
    List<String> strings = Arrays.asList("long word", "short", "long wwww", "llll wwww", "shr");
    IntWrapper wrapper = new IntWrapper();
    List<String> longest = strings.stream()
            .sorted(Comparator.comparingInt(String::length).reversed())
            .peek(s -> {
                if (wrapper.value < s.length()) wrapper.value = s.length();
            })
            .takeWhile(s -> s.length() == wrapper.value)
            .collect(Collectors.toList());
    System.out.println(longest);
}
but it's... ugly? I don't like the introduction of dummy wrapper (thank you, effectively final requirement) or the peek() hack.
Is there any more elegant way to achieve this?
 
     
     
     
     
    