【问题标题】:How to find the min and max element of an array list of big integers?如何找到大整数数组列表的最小和最大元素?
【发布时间】:2019-08-31 18:37:37
【问题描述】:

如何找到ArrayListBigIntegers 的最小和最大元素。

我试过的是:

import java.math.BigInteger; 
import java.io.*;
import java.util.*;

public class HelloWorld{

     public static void main(String []args){
         BigInteger i1 = new BigInteger("4343345345345");
         BigInteger i2 = new BigInteger("4343453345345345");
         BigInteger i3 = new BigInteger("4343453345");

        List<BigInteger> list = new ArrayList<>();

        list.add(i1);
        list.add(i2);
        list.add(i3);

       BigInteger max = list.stream().max(Comparator.comparing(BigInteger::intValue())).orElseThrow(NoSuchElementExcep::new);        

        BigInteger min = list.stream().min(Comparator.comparing(BigInteger::intValue())).orElseThrow(NoSuchElementExcep::new);

      System.out.println(max.intValue());
      System.out.println(min.intValue());
     }
}

但这给了我以下错误:

HelloWorld.java:20: error: ')' expected
        BigInteger min = list.stream().min(Comparator.comparing(BigInteger::intValue())).orElseThrow(NoSuchElementExcep::new);
                                                                                    ^
HelloWorld.java:20: error: ';' expected
        BigInteger min = list.stream().min(Comparator.comparing(BigInteger::intValue())).orElseThrow(NoSuchElementExcep::new);
                                                                                     ^
HelloWorld.java:20: error: illegal start of expression
        BigInteger min = list.stream().min(Comparator.comparing(BigInteger::intValue())).orElseThrow(NoSuchElementExcep::new);
                                                                                      ^
HelloWorld.java:20: error: ';' expected
        BigInteger min = list.stream().min(Comparator.comparing(BigInteger::intValue())).orElseThrow(NoSuchElementExcep::new);
                                                                                       ^
HelloWorld.java:20: error: illegal start of expression
        BigInteger min = list.stream().min(Comparator.comparing(BigInteger::intValue())).orElseThrow(NoSuchElementExcep::new);
                                                                                        ^
HelloWorld.java:20: error: ';' expected
        BigInteger min = list.stream().min(Comparator.comparing(BigInteger::intValue())).orElseThrow(NoSuchElementExcep::new);
                                                                                                    ^
HelloWorld.java:20: error: not a statement
        BigInteger min = list.stream().min(Comparator.comparing(BigInteger::intValue())).orElseThrow(NoSuchElementExcep::new);
                                                                                                     ^
HelloWorld.java:20: error: ';' expected
        BigInteger min = list.stream().min(Comparator.comparing(BigInteger::intValue())).orElseThrow(NoSuchElementExcep::new);

还有其他想法吗?

【问题讨论】:

  • BigInteger 自然具有可比性。您应该使用natural order 进行比较。如果您的值对于整数来说太大了,那么尝试与 intValue() 进行比较是没有意义的。

标签: java arraylist collections java-8 java-stream


【解决方案1】:

在不比较intValues 的情况下获取最小值和最大值确实有效。您应该使用自然排序:

BigInteger max = list.stream().max(BigInteger::compareTo).orElseThrow(NoSuchElementExcep::new);        
BigInteger min = list.stream().min(BigInteger::compareTo).orElseThrow(NoSuchElementExcep::new);

什么也不是方式,你正在打印值:

System.out.println(max.intValue());
System.out.println(min.intValue());

您正在调用intValue 方法,但数量超过了int (32b) 的容量。你应该使用:

System.out.println(max);
System.out.println(min);

【讨论】:

  • 它给出了错误:java:20: error: method max in interface Stream&lt;T&gt; cannot be applied to given types; BigInteger max = list.stream().max().orElseThrow(NoSuchElementException::new); ^ required: Comparator&lt;? super BigInteger&gt; found: no arguments reason: actual and formal argument lists differ in length where T is a type-variable: T extends Object declared in interface Stream
  • 试试max(Comparator.naturalOrder())
  • @khelwood 我刚刚用BigInteger 的比较器更正了,但它是等价的,谢谢;)
  • 我用naturalOrder() 比较器做到了。
【解决方案2】:

简单地做

BigInteger max = list.stream().max(BigInteger::compareTo).get();
BigInteger min = list.stream().min(BigInteger::compareTo).get();

【讨论】:

    【解决方案3】:

    答案是:

    import java.io.*;
    import java.util.*;
    
    
    public class HelloWorld{
    
         public static void main(String []args){
             BigInteger i1 = new BigInteger("4343345345345");
             BigInteger i2 = new BigInteger("4343453345345345");
             BigInteger i3 = new BigInteger("22");
    
            List<BigInteger> list = new ArrayList<>();
    
            list.add(i1);
            list.add(i2);
            list.add(i3);
    
    
            System.out.println(Collections.min(list, Comparator.naturalOrder()));
    
         }
    }
    

    【讨论】:

    • 更简单:Collections.min(list)
    【解决方案4】:
    BigInteger max = list.stream().max(Comparator.comparing(val -> new BigInteger(String.valueOf(val))))
                .orElseThrow(NoSuchElementException::new);
    BigInteger min = list.stream().min(Comparator.comparing(val -> new BigInteger(String.valueOf(val))))
                .orElseThrow(NoSuchElementException::new);
    

    【讨论】:

      猜你喜欢
      • 2013-12-15
      • 2015-12-17
      • 2020-09-22
      • 2015-01-05
      • 1970-01-01
      • 1970-01-01
      • 2022-11-17
      • 2021-12-25
      • 1970-01-01
      相关资源
      最近更新 更多