如何找到大整数数组列表的min和max元素?

问题描述 投票:-1回答:4

如何找到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);

还有其他想法吗?

java arraylist java-8 biginteger
4个回答
3
投票

在没有比较intValues的情况下获得min和max确实有效。你应该使用自然顺序:

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);

1
投票

简单地做

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

0
投票

答案是:

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()));

     }
}

0
投票
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);

© www.soinside.com 2019 - 2024. All rights reserved.