public interface Window<T> extends Collectable<T>
Window functions as exposed in this type are inspired by their counterparts in SQL. They include:
| Function | Description |
|---|---|
rowNumber() |
The distinct row number of the row within the partition, counting from 0. |
rank() |
The rank with gaps of a row within the partition, counting from 0. |
denseRank() |
The rank without gaps of a row within the partition, counting from 0. |
percentRank() |
Relative rank of a row: rank() / Collectable. |
ntile(long) |
Divides the partition in equal buckets and assigns values between 0 and buckets - 1. |
lead() |
Gets the value after the current row. |
lag() |
Gets the value before the current row. |
firstValue() |
Gets the first value in the window. |
lastValue() |
Gets the last value in the window. |
nthValue(long) |
Gets the nth value in the window, counting from 0. |
Note: In Java, indexes are always counted from 0, not from 1 as in SQL. This means that the above ranking functions also rank rows from zero. This is particularly true for:
Seq or from
Agg is also available as an aggregate function on the window. For instance,
Collectable.count() is the same as calling
Seq.count() on
window()
| Modifier and Type | Method and Description |
|---|---|
long |
denseRank()
The dense rank of the current row within the partition.
|
Optional |
firstValue()
The first value in the window.
|
<U> Optional |
firstValue(Function
The first value in the window.
|
Optional |
lag()
The previous value in the window.
|
Optional |
lag(long lag)
The previous value by
lag in the window.
|
Optional |
lastValue()
The last value in the window.
|
<U> Optional |
lastValue(Function
The last value in the window.
|
Optional |
lead()
The next value in the window.
|
Optional |
lead(long lead)
The next value by
lead in the window.
|
Optional |
nthValue(long n)
The nth value in the window.
|
<U> Optional |
nthValue(long n, Function
The nth value in the window.
|
long |
ntile(long buckets)
The bucket number ("ntile") of the current row within the partition.
|
static <T> WindowSpecification |
of()
|
static <T> WindowSpecification |
of(Comparator
|
static <T> WindowSpecification |
of(Comparator
|
static <T |
of(Function
|
static <T |
of(Function
|
static <T |
of(Function
|
static <T |
of(Function
|
static <T> WindowSpecification |
of(long lower, long upper)
|
double |
percentRank()
The precent rank of the current row within the partition.
|
long |
rank()
The rank of the current row within the partition.
|
long |
rowNumber()
The row number of the current row within the partition.
|
T |
value()
The value of the current row in the window.
|
Seq |
window()
Stream all elements in the window.
|
allMatch, anyMatch, avg, avg, avgDouble, avgInt, avgLong, collect, collect, collect, collect, collect, collect, collect, collect, collect, collect, collect, collect, collect, collect, collect, collect, count, count, countDistinct, countDistinct, countDistinctBy, countDistinctBy, max, max, max, max, maxBy, maxBy, median, median, medianBy, medianBy, min, min, min, min, minBy, minBy, mode, noneMatch, percentile, percentile, percentileBy, percentileBy, sum, sum, sumDouble, sumInt, sumLong, toCollection, toList, toList, toMap, toSet, toSet, toString, toStringstatic <T> WindowSpecification<T> of()
static <T> WindowSpecification<T> of(long lower, long upper)
static <T> WindowSpecification<T> of(Comparator <? super T> orderBy)
static <T> WindowSpecification<T> of(Comparator <? super T> orderBy, long lower, long upper)
static <T,U> WindowSpecification <T> of(Function <? super T ,? extends U> partitionBy)
static <T,U> WindowSpecification <T> of(Function <? super T ,? extends U> partitionBy, long lower, long upper)
static <T,U> WindowSpecification <T> of(Function <? super T ,? extends U> partitionBy, Comparator <? super T> orderBy)
static <T,U> WindowSpecification <T> of(Function <? super T ,? extends U> partitionBy, Comparator <? super T> orderBy, long lower, long upper)
T value()
long rowNumber()
// (1, 2, 3, 4, 5)
Seq.of(1, 2, 4, 2, 3).window().map(w -> w.rowNumber());
long rank()
// (1, 2, 2, 4, 5)
Seq.of(1, 2, 2, 3, 4).window(naturalOrder()).map(w -> w.rank());
long denseRank()
// (1, 2, 2, 3, 4)
Seq.of(1, 2, 2, 3, 4).window(naturalOrder()).map(w -> w.denseRank());
double percentRank()
// (0.0, 0.25, 0.25, 0.75, 1.0)
Seq.of(1, 2, 2, 3, 4).window(naturalOrder()).map(w -> w.percentRank());
long ntile(long buckets)
// (0, 0, 1, 1, 2)
Seq.of(1, 2, 2, 3, 4).window(naturalOrder()).map(w -> w.ntile(3));
Optional<T> lead()
This is the same as calling lead(1)
// (2, 2, 3, 4, empty)
Seq.of(1, 2, 2, 3, 4).window().map(w -> w.lead());
Optional<T> lead(long lead)
lead in the window.
// (2, 2, 3, 4, empty)
Seq.of(1, 2, 2, 3, 4).window().map(w -> w.lead());
Optional<T> lag()
This is the same as calling lag(1)
// (empty, 1, 2, 2, 3)
Seq.of(1, 2, 2, 3, 4).window().map(w -> w.lag());
Optional<T> lag(long lag)
lag in the window.
// (empty, 1, 2, 2, 3)
Seq.of(1, 2, 2, 3, 4).window().map(w -> w.lag());
Optional<T> firstValue()
// (1, 1, 1, 1, 1)
Seq.of(1, 2, 4, 2, 3).window().map(w -> w.firstValue());
<U> Optional<U> firstValue(Function <? super T ,? extends U> function)
// (1, 1, 1, 1, 1)
Seq.of(1, 2, 4, 2, 3).window().map(w -> w.firstValue());
Optional<T> lastValue()
// (3, 3, 3, 3, 3)
Seq.of(1, 2, 4, 2, 3).window().map(w -> w.lastValue());
<U> Optional<U> lastValue(Function <? super T ,? extends U> function)
// (3, 3, 3, 3, 3)
Seq.of(1, 2, 4, 2, 3).window().map(w -> w.lastValue());
Optional<T> nthValue(long n)
// (4, 4, 4, 4, 4)
Seq.of(1, 2, 4, 2, 3).window().map(w -> w.nthValue(2));