简体   繁体   English

使用 Java Streams 从字符串数列表中查找最大数

[英]Find Max number from List of String numbers using Java Streams

I am trying to get max number among List of Strings.我正在尝试获取字符串列表中的最大数量。

My code is:我的代码是:

List<String> list = new ArrayList<String>();
list.add("00657");
list.add("00632");
list.add("00656");
list.add("01125");

Integer maxNum = list.stream()
                   .mapToInt(Integer::intValue)
                   .max();

Above code is giving an error.上面的代码给出了一个错误。 Error is: Uncompilable source code - Erroneous sym type: java.util.stream.Stream.mapToInt.max.错误是:无法编译的源代码 - 错误的符号类型:java.util.stream.Stream.mapToInt.max I should get 1125 as a result.结果我应该得到1125 I tried different ways but none of them is working.我尝试了不同的方法,但没有一个有效。

You need to parse the String s as Integer s which can be done using parseInt :您需要将String解析为Integer ,这可以使用parseInt来完成:

List<String> list = new ArrayList<>();
list.add("00657");
list.add("00632");
list.add("00656");
list.add("01125");

int maxNum = list.stream()
        .mapToInt(Integer::parseInt)
        .max()
        .orElse(-1);

Because there are no guarantees your stream is not going to be empty, this is why the orElse is needed.因为不能保证您的 stream 不会为空,这就是需要orElse的原因。

mapToint does not map any object (such as String ) to an integer. mapToint不会 map 任何 object(例如String )到 integer。 It is used to map Integer type streams to int stream.它用于 map Integer类型流到int stream。

You need to first map the String numbers to Integer using map with parseInt and then map the Integer to int . You need to first map the String numbers to Integer using map with parseInt and then map the Integer to int .

Use the following piece of code:使用以下代码:

  Integer maxNum = list.stream()
                        .map(Integer::parseInt)
                        .mapToInt(Integer::intValue)
                       .max()
                       .getAsInt();

Hope this helps!希望这可以帮助!

@Abdusoli mapToInt can map only int type of data so you have to parse it into int first and final result is OptionalInt so it should be convert into Int. @Abdusoli mapToInt 可以 map 仅 int 类型的数据,因此您必须首先将其解析为 int,最终结果是 OptionalInt,因此应将其转换为 Int。 You can try both the way你可以尝试两种方式

First approach第一种方法

Integer maxNum = list.stream()
                .mapToInt(Integer::parseInt)
                .max()
                .getAsInt();
        System.out.println(maxNum);

Second approach第二种方法

Integer maxNum = list.stream()
                .map(Integer::parseInt)
                .mapToInt(Integer::intValue)
                .max().getAsInt();
        System.out.println(maxNum);

intValue is not a static method,you should use Integer::parseInt instead. intValue不是 static 方法,您应该使用Integer::parseInt代替。 Also, max() return OptionalInt not Integer , you can use max().getAsInt();此外, max()返回OptionalInt而不是Integer ,您可以使用max().getAsInt(); or max().orElse();max().orElse();

Such as

Integer maxNum = list.stream().mapToInt(Integer::parseInt).max().getAsInt();
   Integer maxNum = list.stream()
            .map(Integer::valueOf).mapToInt(v-> v)
            .max().orElseThrow(NoSuchElementException::new);

You can try this one instead of intValue change to valueOf你可以试试这个,而不是把 intValue 改为 valueOf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM