如何找到ArrayList
中BigIntegers
的最小和最大元素。
我尝试过的是:
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);
还有其他想法吗?
答案 0 :(得分:3)
在不比较intValue
的情况下获取最小值和最大值是可行的。您应该使用自然顺序:
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 :(得分:1)
简单地做
# The search algorithm is actually only applied a filter on the objects array.
# data is all items in database
data = realm.objects(CustomObject.self).filter(filterPredicate(parentID: id, keyword: keyword, colorIndex: colorIndexes, isActionOnly: True)).sorted(by: descSorting)
private func filterPredicate(parentID: Int?, keyword: String?, colorIndex: [Int]?, isActionOnly: Bool = false) -> InspirationFilterClosure {
return { item in
if item.isDeleted { return false }
if let id = parentID, item.parentID != id { return false }
else if item.parentID < 0 { return false }
if isActionOnly, !item.isAction { return false }
if let indexes = colorIndex, !indexes.contains(-1), !indexes.contains(item.colorIndex) { return false }
if let keyword = keyword, !keyword.isEmpty {
return item.content.range(of: keyword, options: .caseInsensitive) != nil
}
return true
}
}
答案 2 :(得分:0)
答案是:
<ul class="navbar-nav ml-auto">
答案 3 :(得分: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);