Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find max length in list of string using streams in Java?

I have a class like below:

public class A
{
    String name;
    String getName(){return name;}
}

And I also have a list like below:

List<A> list_a = new ArrayList<>();
//add n objects into list_a

Right now I would like to find the max length of object which is in list_a using streams in Java. I have created code like below:

final int max_len = list_a.stream().max(Comparator.comparingInt(A::getName::length));

But it does not work, I mean it is something bad with syntax. Could you help me with this? Thank you.

like image 417
Dawid Kowalski Avatar asked Dec 07 '22 13:12

Dawid Kowalski


1 Answers

What you are using isn't lambda. Lambda looks like (arguments) -> action. What you have in A::getName is method reference, but additional ::length is not part of its syntax.

Instead of A::getName::length you can use lambda like a -> a.getName().length().

But your code has yet another problem. Code

list_a.stream()
      .max(Comparator.comparingInt(A::getName::length));

is handling streams of A and max method called on Stream<A> will result in Optional<A> not int. It is Optional because there is a chance that list_a can be empty which means that there will be no valid result.

If you want to get OptionalInt you would need to map Stream<A> to Stream<String> and then map it to Stream of ints first. Then you can call its max() method and get:

OptionalInt maxOpt  = list_a.stream()
                            .map(A::getName)
                            .mapToInt(String::length)
                            .max();

When you already have OptionalInt you can use it to check if value there isPresent() and get it via getAsInt(). You can also use orElse(defaultValueIfEmpty) like

int max = maxOpt.orElse(-1); //will return value held by maxOpt, or -1 if there is no value
like image 96
Pshemo Avatar answered Mar 15 '23 18:03

Pshemo