Java – math. In lambda expressions toIntExact?

I'm learning lambda expressions

I did this:

final static List<String> friends = Arrays.asList("Brian","Nate","Neal","Raju","Sara","Scott");

public static int countFriendsStartWithN() {
    return  Math.toIntExact(friends
            .stream()
            .filter(name -> name.startsWith("N"))
            .count());
}

The call to the count method returns a primitive long, but I want an int. I use math Tointexact takes the long value as int

Can I get int values directly in lambda expressions?

Solution

No, you cannot fit the call to tointexact to your method call chain, the flow pipeline This is because count is a terminal operation and returns a long primitive on which there is no method call Terminal operation is the operation that ends the flow pipeline and produces results (or side effects)

So I believe the best thing you can do is put up with the existing code With all due respect, it doesn't matter

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>