java-10 中的 XXXSummaryStatistics 新构造函数
XXXSummaryStatistics new constructor in java-10
我看到 java-10
为 IntSummaryStatistics
(LongSummaryStatistics
和 DoubleSummaryStatistics
)添加了一个构造函数,它带有 4 个参数,分别是 count
、min
、max
和 sum
.
我理解为什么存在无参数构造函数,以便将其用于缩减,例如:
..stream().collect(Collectors.summarizingInt(Class::someFunction))
有道理,但是为什么要加4个参数的构造函数呢? (我在自己的回答中做了一个假设,但如果不是这样,我很乐意收回它。)
如果在某处使用了这些构造函数,我曾尝试搜索源代码,但我做不到。
所以我唯一的想法是它用于手动构造这样的对象。假设我们有一个计算所有这些平均值、最小值、最大值、计数的方法,而不是 return 一个包含 4 个参数的 array/List
,你可以 return 一个 IntSummaryStatistics
,以前这是不可能的。所以,我想,这只是扩展了 API 而没有(还?)任何内部用法。
来自 relative CSR 恰好 :-
问题:
不可能从它的记录值中重建 *SummaryStatistics
。 例如"cloned"或以串行形式传输并重组。
解决方法:
将构造函数添加到 *SummaryStatistics
接受 pre-recorded 状态。
除了对这种构造函数的使用(需要)的回答之外,要关心 API 实现的正确性,应该真正考虑大声调用 API 注意 这样 such instances 的创建是在某些深思熟虑的考虑下作为(强调我的)-
If the count
is zero then the remaining arguments are ignored and an empty instance is constructed.
示例 -
var intsummstats = new IntSummaryStatistics();
// creates the following stat
=> IntSummaryStatistics{count=0, sum=0, min=2147483647, average=0.000000, max=-2147483648}
// and the following results into a similar stat as well
var anotherintsummstats = new IntSummaryStatistics(0, 12, 100, 1000);
=> IntSummaryStatistics{count=0, sum=0, min=2147483647, average=0.000000, max=-2147483648}
If the arguments are inconsistent then an IllegalArgumentException
is thrown. The necessary consistent argument conditions are:
- count >= 0
- min <= max
但是由于这并未涵盖对用户可以输入的值的 count
、sum
、max
和 min
组合的所有类型的检查在里面,有这个语句(我在玩构造函数时发现的)
The enforcement of argument correctness means that the retrieved set
of recorded values obtained from an *SummaryStatistics
source
instance may not be a legal set of arguments for this constructor due
to arithmetic overflow of the source's recorded count of values. The
consistent argument conditions are not sufficient to prevent the
creation of an internally inconsistent instance. An example of such a
state would be an (IntSummaryStatistics) instance with: count = 2, min = 1, max = 2, and sum
= 0.
此外,当 combine
d 与其他 *SummaryStatistic
一起创建时,此类错误创建的实例可能会进一步导致一组非法参数。
请注意,虽然今天的参考实现中没有进行此类优化,但像 IntStream.rangeClosed(from, to).summaryStatistics()
这样的操作不需要实际迭代所有值。
它可以简单地 return
new IntSummaryStatistics((long)to-from+1, from, to, ((long)from+to)*((long)to-from+1)/2)
.
如前所述,这种优化今天并没有发生,但它是一个例子,有时,有一些方法可以计算这样的统计数据而不必迭代每个值,所以这是一个重要的限制,唯一的方法是填充xxxSummaryStatistics accept
ing 单个值(和 combine
,但这需要一个已经存在的统计实例,必须以某种方式填充)。
我看到 java-10
为 IntSummaryStatistics
(LongSummaryStatistics
和 DoubleSummaryStatistics
)添加了一个构造函数,它带有 4 个参数,分别是 count
、min
、max
和 sum
.
我理解为什么存在无参数构造函数,以便将其用于缩减,例如:
..stream().collect(Collectors.summarizingInt(Class::someFunction))
有道理,但是为什么要加4个参数的构造函数呢? (我在自己的回答中做了一个假设,但如果不是这样,我很乐意收回它。)
如果在某处使用了这些构造函数,我曾尝试搜索源代码,但我做不到。
所以我唯一的想法是它用于手动构造这样的对象。假设我们有一个计算所有这些平均值、最小值、最大值、计数的方法,而不是 return 一个包含 4 个参数的 array/List
,你可以 return 一个 IntSummaryStatistics
,以前这是不可能的。所以,我想,这只是扩展了 API 而没有(还?)任何内部用法。
来自 relative CSR 恰好 :-
问题:
不可能从它的记录值中重建 *SummaryStatistics
。 例如"cloned"或以串行形式传输并重组。
解决方法:
将构造函数添加到 *SummaryStatistics
接受 pre-recorded 状态。
除了对这种构造函数的使用(需要)的回答之外,要关心 API 实现的正确性,应该真正考虑大声调用 API 注意 这样 such instances 的创建是在某些深思熟虑的考虑下作为(强调我的)-
If the
count
is zero then the remaining arguments are ignored and an empty instance is constructed.
示例 -
var intsummstats = new IntSummaryStatistics();
// creates the following stat
=> IntSummaryStatistics{count=0, sum=0, min=2147483647, average=0.000000, max=-2147483648}
// and the following results into a similar stat as well
var anotherintsummstats = new IntSummaryStatistics(0, 12, 100, 1000);
=> IntSummaryStatistics{count=0, sum=0, min=2147483647, average=0.000000, max=-2147483648}
If the arguments are inconsistent then an
IllegalArgumentException
is thrown. The necessary consistent argument conditions are:
- count >= 0
- min <= max
但是由于这并未涵盖对用户可以输入的值的 count
、sum
、max
和 min
组合的所有类型的检查在里面,有这个语句(我在玩构造函数时发现的)
The enforcement of argument correctness means that the retrieved set of recorded values obtained from an
*SummaryStatistics
source instance may not be a legal set of arguments for this constructor due to arithmetic overflow of the source's recorded count of values. The consistent argument conditions are not sufficient to prevent the creation of an internally inconsistent instance. An example of such a state would be an (IntSummaryStatistics) instance with: count = 2, min = 1, max = 2, and sum = 0.
此外,当 combine
d 与其他 *SummaryStatistic
一起创建时,此类错误创建的实例可能会进一步导致一组非法参数。
请注意,虽然今天的参考实现中没有进行此类优化,但像 IntStream.rangeClosed(from, to).summaryStatistics()
这样的操作不需要实际迭代所有值。
它可以简单地 return
new IntSummaryStatistics((long)to-from+1, from, to, ((long)from+to)*((long)to-from+1)/2)
.
如前所述,这种优化今天并没有发生,但它是一个例子,有时,有一些方法可以计算这样的统计数据而不必迭代每个值,所以这是一个重要的限制,唯一的方法是填充xxxSummaryStatistics accept
ing 单个值(和 combine
,但这需要一个已经存在的统计实例,必须以某种方式填充)。