Edge case
range(Integer.MIN_VALUE, Integer.MAX_VALUE) == 0; // true
This occurs due to overflowing. To handle this, you will need to update your method return type and temporary variables as long
.
Java 8
It's trivial to get statistics from a LongStream
via LongSummaryStatistics
in order to retrieve the maximum and minimum values.
After adding an Optional
and a healthy amount of using method references, a shorter Java 8 solution can be just one return
statement:
private static long getRange(Collection<Integer> collection) {
return Optional.of(collection)
.filter(c -> !c.isEmpty())
.map(Collection::stream)
.map(s -> s.mapToLong(Integer::longValue))
.map(LongStream::summaryStatistics)
.map(i -> i.getMax() - i.getMin() + 1)
.orElse(0L);
}
The use of Collection
is in the same vein as @Simon @Simon's answer, to handle more kinds of 'ranges' instead of just ArrayList
. On that note, the method should be taking in a List
so that it can work with other implementations of that interface as well.
Edge case
range(Integer.MIN_VALUE, Integer.MAX_VALUE) == 0; // true
This occurs due to overflowing. To handle this, you will need to update your method return type and temporary variables as long
.
Java 8
It's trivial to get statistics from a LongStream
via LongSummaryStatistics
in order to retrieve the maximum and minimum values.
After adding an Optional
and a healthy amount of using method references, a shorter Java 8 solution can be just one return
statement:
private static long getRange(Collection<Integer> collection) {
return Optional.of(collection)
.filter(c -> !c.isEmpty())
.map(Collection::stream)
.map(s -> s.mapToLong(Integer::longValue))
.map(LongStream::summaryStatistics)
.map(i -> i.getMax() - i.getMin() + 1)
.orElse(0L);
}
The use of Collection
is in the same vein as @Simon's answer, to handle more kinds of 'ranges' instead of just ArrayList
. On that note, the method should be taking in a List
so that it can work with other implementations of that interface as well.
Edge case
range(Integer.MIN_VALUE, Integer.MAX_VALUE) == 0; // true
This occurs due to overflowing. To handle this, you will need to update your method return type and temporary variables as long
.
Java 8
It's trivial to get statistics from a LongStream
via LongSummaryStatistics
in order to retrieve the maximum and minimum values.
After adding an Optional
and a healthy amount of using method references, a shorter Java 8 solution can be just one return
statement:
private static long getRange(Collection<Integer> collection) {
return Optional.of(collection)
.filter(c -> !c.isEmpty())
.map(Collection::stream)
.map(s -> s.mapToLong(Integer::longValue))
.map(LongStream::summaryStatistics)
.map(i -> i.getMax() - i.getMin() + 1)
.orElse(0L);
}
The use of Collection
is in the same vein as @Simon's answer, to handle more kinds of 'ranges' instead of just ArrayList
. On that note, the method should be taking in a List
so that it can work with other implementations of that interface as well.
Edge case
range(Integer.MIN_VALUE, Integer.MAX_VALUE) == 0; // true
This occurs due to overflowing. To handle this, you will need to update your method return type and temporary variables as long
.
Java 8
It's trivial to get statistics from a LongStream
via LongSummaryStatistics
in order to retrieve the maximum and minimum values.
After adding an Optional
and a healthy amount of using method references, a shorter Java 8 solution can be just one return
statement:
private static long getRange(Collection<Integer> collection) {
return Optional.of(collection)
.filter(c -> !c.isEmpty())
.map(Collection::stream)
.map(s -> s.mapToLong(Integer::longValue))
.map(LongStream::summaryStatistics)
.map(i -> i.getMax() - i.getMin() + 1)
.orElse(0L);
}
The use of Collection
is in the same vein as @Simon's answer, to handle more kinds of 'ranges' instead of just ArrayList
. On that note, the method should be taking in a List
so that it can work with other implementations of that interface as well.