简体   繁体   English

如何找到大整数数组列表的最小和最大元素?

[英]How to find the min and max element of an array list of big integers?

How can I find the min and max element of an ArrayList of BigIntegers .如何找到BigIntegersArrayList的最小和最大元素。

What I've tried is:我试过的是:

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

But this gives me the following errors:但这给了我以下错误:

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

Any other ideas?还有其他想法吗?

Getting min and max does work without comparing intValue s.在不比较intValue的情况下获取 min 和 max 确实有效。 You should use the natural ordering:您应该使用自然顺序:

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

What also doesn't is the way, you're printing the values:也不是这样,您正在打印值:

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

You're calling intValue method, but the number exceed the capacity of int (32b).您正在调用intValue方法,但数字超出了int (32b) 的容量。 You should rather use:你应该使用:

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

Simply do简单地做

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

The answer is:答案是:

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

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

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

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