[英]You can extend the IntSummaryStatistics class?
I'm using the Java stream statistics, using the speedment, this way: 我正在使用Java流统计信息,通过这种方式来提高速度:
IntSummaryStatistics intSummary = join.stream(). MapToInt (t> t.get0(). GetCNationkey ()). SummaryStatistics();
Long sumResult = intSummary.getSum ();
I wanted a new class to construct a new getSum()
method. 我想要一个新的类来构造一个新的getSum()
方法。 Something like: 就像是:
IntSummaryStatisticsTest intSummarytest = join.stream (). MapToInt (t> t.get0 (). GetCNationkey ()). SummaryStatistics ();
Long sumResult = intSummarytest.getSumTest();
I tried to create a new class: 我试图创建一个新类:
public class IntSummaryStatisticsTest extends IntSummaryStatistics {}
IntSummaryStatisticsTest summa = join.stream().mapToInt(t->t.get0().getCNationkey()).summaryStatistics();
but I got this error: incompatible types required java. Required: IntSummaryStatisticsTest Found: java.util.IntSummaryStatistics
但我收到此错误: incompatible types required java. Required: IntSummaryStatisticsTest Found: java.util.IntSummaryStatistics
incompatible types required java. Required: IntSummaryStatisticsTest Found: java.util.IntSummaryStatistics
. incompatible types required java. Required: IntSummaryStatisticsTest Found: java.util.IntSummaryStatistics
。
Is it possible to implement this new getSumTest()
method? 是否可以实现此新的getSumTest()
方法?
Like I stated in the comments, I'd opt for composition over inheritance. 就像我在评论中指出的那样,我会选择继承而不是继承。 This means that you can create your class, IntSummaryStatisticsTest
, and accept an IntSummaryStatistics
object as a parameter in your constructor. 这意味着您可以创建类IntSummaryStatisticsTest
,并在构造函数中接受IntSummaryStatistics
对象作为参数。 Your class would look something like the following: 您的课程如下所示:
class IntSummaryStatisticsTest {
private final IntSummaryStatistics statistics;
public IntSummaryStatisticsTest(IntSummaryStatistics statistics) {
this.statistics = statistics;
}
public long getSumTest() {
// return your value
}
}
The usage of the class would look like: 该类的用法如下所示:
var summary = new IntSummaryStatisticsTest(join.stream()
.mapToInt(t -> t.get0().getCNationkey())
.summaryStatistics());
System.out.println(summary.getSumTest());
The documentation of IntStream
gives a hint: IntStream
的文档提供了一个提示:
...For example, you can compute summary statistics on a stream of ints with: ...例如,您可以使用以下方法在整数流上计算摘要统计信息:
IntSummaryStatistics stats = intStream.collect( IntSummaryStatistics::new, IntSummaryStatistics::accept, IntSummaryStatistics::combine);
you should be able to do the same with IntSummaryStatisticsTest
, that is: 您应该可以使用IntSummaryStatisticsTest
进行相同的IntSummaryStatisticsTest
,即:
IntSummaryStatisticsTest stats = intStream.collect(
IntSummaryStatisticsTest::new,
IntSummaryStatisticsTest::accept,
IntSummaryStatisticsTest::combine);
But also consider Jacob's comment/solution (using composition would probably be even better) 但还要考虑Jacob的评论/解决方案(使用组合可能会更好)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.