programmer
programmer

Reputation: 65

You can extend the IntSummaryStatistics class?

I'm using the Java stream statistics, using the speedment, this way:

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. 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.

Is it possible to implement this new getSumTest() method?

Upvotes: 0

Views: 189

Answers (2)

Jacob G.
Jacob G.

Reputation: 29680

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. 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());

Upvotes: 2

user85421
user85421

Reputation: 29680

The documentation of IntStream gives a hint:

...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 stats = intStream.collect(
    IntSummaryStatisticsTest::new,
    IntSummaryStatisticsTest::accept,
    IntSummaryStatisticsTest::combine);

But also consider Jacob's comment/solution (using composition would probably be even better)

Upvotes: 1

Related Questions