I have a list of transactions, integer. I need to create range to display that list in a bar chart. I am trying to compute upper bound of that range based on the maximum value from the list. Eg: If the list is {8, 9 , 91, 45}, then 1. calculate max as 91. 2. calculate range, lower bound always 0 but upper bound can be 100.
我有一个事务列表,整数。我需要创建范围以在条形图中显示该列表。我试图根据列表中的最大值计算该范围的上限。例如:如果列表是{8,9,91,45},则1.将max计算为91. 2.计算范围,下限始终为0但上限可以为100。
Any Ideas how to do that?
任何想法如何做到这一点?
1 个解决方案
#1
1
Assuming that you're using Java 8, use a java.util.Stream
:
假设您使用的是Java 8,请使用java.util.Stream:
List<Integer> list = Arrays.asList(8, 9 , 91, 45); //make your list
int max = list
.stream() //get a stream for the list
.mapToInt((i) -> i) //Get an intstream
.max() //Get the max value
.getAsInt(); //Unwrap the optional
Since any number greater than or equal to the max is an upper bound, we can just use the max as one:
由于任何大于或等于max的数字都是上限,我们可以使用max作为一个:
int upperBound = max;
If, as @JohnColeman suggested, you need upperBound
to be a multiple of 10, just do:
如果像@JohnColeman建议的那样,你需要将upperBound设为10的倍数,那么就这样做:
if (max % 10 == 0) {
upperBound = max; //if already a multiple of 10, leave as is
} else {
upperBound = max + (10 - max % 10); //Otherwise, add the amount which will make it a multiple of 10.
}
Since you want the lower bound to be 0, that's easy:
由于您希望下限为0,这很容易:
int lowerBound = 0;
However, if you want the lower bound to be equal to the min, just use another stream:
但是,如果您希望下限等于min,只需使用另一个流:
int min = list
.stream() //get a stream for the list
.mapToInt((i) -> i) //Get an intstream
.min() //Get the min value
.getAsInt(); //Unwrap the optional
int lowerBound = min;
Again, you can make this a multiple of 10 if that's what you want:
同样,如果这是你想要的,你可以使它成为10的倍数:
if (min % 10 == 0) {
lowerBound = min; //if already a multiple of 10, leave as is
} else {
upperBound = min - min % 10; //Subtract it mod 10
}
Further Reading:
- A great tutorial on Java 8 Streams
- The JavaDoc for
java.util.Stream
- And the JavaDoc for
IntStream
, which I got usingmapToInt
- Info on the
%
(modulo) operator
关于Java 8 Streams的精彩教程
java.util.Stream的JavaDoc
和IntDream的JavaDoc,我使用mapToInt
关于%(modulo)运算符的信息
#1
1
Assuming that you're using Java 8, use a java.util.Stream
:
假设您使用的是Java 8,请使用java.util.Stream:
List<Integer> list = Arrays.asList(8, 9 , 91, 45); //make your list
int max = list
.stream() //get a stream for the list
.mapToInt((i) -> i) //Get an intstream
.max() //Get the max value
.getAsInt(); //Unwrap the optional
Since any number greater than or equal to the max is an upper bound, we can just use the max as one:
由于任何大于或等于max的数字都是上限,我们可以使用max作为一个:
int upperBound = max;
If, as @JohnColeman suggested, you need upperBound
to be a multiple of 10, just do:
如果像@JohnColeman建议的那样,你需要将upperBound设为10的倍数,那么就这样做:
if (max % 10 == 0) {
upperBound = max; //if already a multiple of 10, leave as is
} else {
upperBound = max + (10 - max % 10); //Otherwise, add the amount which will make it a multiple of 10.
}
Since you want the lower bound to be 0, that's easy:
由于您希望下限为0,这很容易:
int lowerBound = 0;
However, if you want the lower bound to be equal to the min, just use another stream:
但是,如果您希望下限等于min,只需使用另一个流:
int min = list
.stream() //get a stream for the list
.mapToInt((i) -> i) //Get an intstream
.min() //Get the min value
.getAsInt(); //Unwrap the optional
int lowerBound = min;
Again, you can make this a multiple of 10 if that's what you want:
同样,如果这是你想要的,你可以使它成为10的倍数:
if (min % 10 == 0) {
lowerBound = min; //if already a multiple of 10, leave as is
} else {
upperBound = min - min % 10; //Subtract it mod 10
}
Further Reading:
- A great tutorial on Java 8 Streams
- The JavaDoc for
java.util.Stream
- And the JavaDoc for
IntStream
, which I got usingmapToInt
- Info on the
%
(modulo) operator
关于Java 8 Streams的精彩教程
java.util.Stream的JavaDoc
和IntDream的JavaDoc,我使用mapToInt
关于%(modulo)运算符的信息