解释
官方文档中:
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).
翻译:
min和minBy之间的区别是min返回最小值,而minBy返回在此字段中具有最小值的元素(与max和maxBy相同)。
但是事实上,min与max 也会返回整个元素。
不同的是min会根据指定的字段取最小值,并且把这个值保存在对应的位置上,对于其他的字段取了最先获取的值,不能保证每个元素的数值正确,max同理。
而minBy会返回指定字段取最小值的元素,并且会覆盖指定字段小于当前已找到的最小值元素。maxBy同理。
示例论证
先拿min()与minBy()举例:
取第三个元素的最小值
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//获取数据源
List data = new ArrayList<Tuple3<Integer,Integer,Integer>>();
data.add(new Tuple3<>(0,2,2)

本文详细介绍了Flink DataStream API中min()与minBy()、max()与maxBy()的区别。min返回最小值,但可能不保留其他字段的正确值,而minBy返回具有最小指定字段值的完整元素。max和maxBy同理。通过示例论证,展示了minBy能确保元素数值的正确性。
最低0.47元/天 解锁文章
158

被折叠的 条评论
为什么被折叠?



