explain
Official documentation:
The difference between min and minBy is that min returns the minimum value, whereas minBy returns the element that has the minimum value in this field (same for max and maxBy).
Translation:
The difference between min and minBy is that min returns the minimum value, while minBy returns the element that has the minimum value in this field (the same as Max and maxBy).
But in fact,min and Max also return the entire element.
The difference is that min will take the minimum value according to the specified field and save the value in the corresponding position. For other fields, it takes the first value, which cannot guarantee the correct value of each element. The same is true for Max.
MinBy returns the element with the minimum value of the specified field and overwrites the element with the minimum value of the specified field that is currently found. MaxBy similarly.
The sample demonstrates
Take min() and minBy() for example:
Take the minimum of the third element
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
// Get the data source
List data = new ArrayList<Tuple3<Integer,Integer,Integer>>();
data.add(new Tuple3<>(0.2.2));
data.add(new Tuple3<>(0.1.1));
data.add(new Tuple3<>(0.5.6));
data.add(new Tuple3<>(0.3.5));
data.add(new Tuple3<>(1.1.9));
data.add(new Tuple3<>(1.2.8));
data.add(new Tuple3<>(1.3.10));
data.add(new Tuple3<>(1.2.9));
DataStreamSource<Tuple3<Integer,Integer,Integer>> items = env.fromCollection(data);
items.keyBy(0).min(2).print();
env.execute("defined streaming source");
}
Copy the code
Output result:
(0.2.2)
(0.2.1)
(0.2.1)
(0.2.1)
(1.1.9)
(1.1.8)
(1.1.8)
(1.1.8)
Copy the code
You can see that the second field of the returned element takes the field value of the first element; If the specified value of the second element is the smallest, store the value in the corresponding location.
Let’s look at the result of minBy() :
(0.2.2)
(0.1.1)
(0.1.1)
(0.1.1)
(1.1.9)
(1.2.8)
(1.2.8)
(1.2.8)
Copy the code
Returns the element that specifies the minimum value for the field. You can see that the element values are correct.
Of course Max (),maxBy.
More articles :www.ipooli.com