Why does LongSummaryStatistics implements IntConsumer?
Why does LongSummaryStatistics implements IntConsumer when there is IntSummaryStatistics which also implements IntConsumer?
java java-8 java-stream
add a comment |
Why does LongSummaryStatistics implements IntConsumer when there is IntSummaryStatistics which also implements IntConsumer?
java java-8 java-stream
add a comment |
Why does LongSummaryStatistics implements IntConsumer when there is IntSummaryStatistics which also implements IntConsumer?
java java-8 java-stream
Why does LongSummaryStatistics implements IntConsumer when there is IntSummaryStatistics which also implements IntConsumer?
java java-8 java-stream
java java-8 java-stream
edited 1 hour ago
Stefan Zobel
2,44031828
2,44031828
asked 1 hour ago
mkjh
375112
375112
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
LongSummaryStatistics implements IntConsumer in order that it can accept int values as well as long values.
For example, this allows you to pass it to a method requiring an IntConsumer in order to consume some int data abstractly:
LongSummaryStatistics lss = new LongSummaryStatistics();
someMethod(lss);
void someMethod(IntConsumer consumer) { ... }
There's no real reason why a LongSummaryStatistics shouldn't be usable for this purpose: int can always be widened to long without loss. However, the type system wouldn't allow lss to be used as a parameter to someMethod unless LongSummaryStatistics implemented IntConsumer directly.
True, you could do this without implementing the interface, using a lambda:
someMethod(i -> lss.consume(i));
but it's just a bit neater to use the reference directly.
1
A follow-up question that comes to my mind, why not separate them out, why let consumers define separate streams ofintandlongand then mix them?
– nullpointer
1 hour ago
1
@nullpointer try defining a class which doesn't implementIntConsumer, and then pass it to a method which requires anIntConsumerparameter.
– Andy Turner
50 mins ago
1
I don't understand what you mean by "why let consumers define separate streams of int and long and then mix them"? Streams are not the only sources of data for*SummaryStatistics, you can invoke the methods directly.
– Andy Turner
44 mins ago
True, and when invoking the *SummaryStatistics directly too, why should one be collecting stats for varied type(int at once and long at another) of data is what I am thinking about. Maybe thinking too much there, but seemingly an example would cool down my thought process I guess.
– nullpointer
40 mins ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54003796%2fwhy-does-longsummarystatistics-implements-intconsumer%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
LongSummaryStatistics implements IntConsumer in order that it can accept int values as well as long values.
For example, this allows you to pass it to a method requiring an IntConsumer in order to consume some int data abstractly:
LongSummaryStatistics lss = new LongSummaryStatistics();
someMethod(lss);
void someMethod(IntConsumer consumer) { ... }
There's no real reason why a LongSummaryStatistics shouldn't be usable for this purpose: int can always be widened to long without loss. However, the type system wouldn't allow lss to be used as a parameter to someMethod unless LongSummaryStatistics implemented IntConsumer directly.
True, you could do this without implementing the interface, using a lambda:
someMethod(i -> lss.consume(i));
but it's just a bit neater to use the reference directly.
1
A follow-up question that comes to my mind, why not separate them out, why let consumers define separate streams ofintandlongand then mix them?
– nullpointer
1 hour ago
1
@nullpointer try defining a class which doesn't implementIntConsumer, and then pass it to a method which requires anIntConsumerparameter.
– Andy Turner
50 mins ago
1
I don't understand what you mean by "why let consumers define separate streams of int and long and then mix them"? Streams are not the only sources of data for*SummaryStatistics, you can invoke the methods directly.
– Andy Turner
44 mins ago
True, and when invoking the *SummaryStatistics directly too, why should one be collecting stats for varied type(int at once and long at another) of data is what I am thinking about. Maybe thinking too much there, but seemingly an example would cool down my thought process I guess.
– nullpointer
40 mins ago
add a comment |
LongSummaryStatistics implements IntConsumer in order that it can accept int values as well as long values.
For example, this allows you to pass it to a method requiring an IntConsumer in order to consume some int data abstractly:
LongSummaryStatistics lss = new LongSummaryStatistics();
someMethod(lss);
void someMethod(IntConsumer consumer) { ... }
There's no real reason why a LongSummaryStatistics shouldn't be usable for this purpose: int can always be widened to long without loss. However, the type system wouldn't allow lss to be used as a parameter to someMethod unless LongSummaryStatistics implemented IntConsumer directly.
True, you could do this without implementing the interface, using a lambda:
someMethod(i -> lss.consume(i));
but it's just a bit neater to use the reference directly.
1
A follow-up question that comes to my mind, why not separate them out, why let consumers define separate streams ofintandlongand then mix them?
– nullpointer
1 hour ago
1
@nullpointer try defining a class which doesn't implementIntConsumer, and then pass it to a method which requires anIntConsumerparameter.
– Andy Turner
50 mins ago
1
I don't understand what you mean by "why let consumers define separate streams of int and long and then mix them"? Streams are not the only sources of data for*SummaryStatistics, you can invoke the methods directly.
– Andy Turner
44 mins ago
True, and when invoking the *SummaryStatistics directly too, why should one be collecting stats for varied type(int at once and long at another) of data is what I am thinking about. Maybe thinking too much there, but seemingly an example would cool down my thought process I guess.
– nullpointer
40 mins ago
add a comment |
LongSummaryStatistics implements IntConsumer in order that it can accept int values as well as long values.
For example, this allows you to pass it to a method requiring an IntConsumer in order to consume some int data abstractly:
LongSummaryStatistics lss = new LongSummaryStatistics();
someMethod(lss);
void someMethod(IntConsumer consumer) { ... }
There's no real reason why a LongSummaryStatistics shouldn't be usable for this purpose: int can always be widened to long without loss. However, the type system wouldn't allow lss to be used as a parameter to someMethod unless LongSummaryStatistics implemented IntConsumer directly.
True, you could do this without implementing the interface, using a lambda:
someMethod(i -> lss.consume(i));
but it's just a bit neater to use the reference directly.
LongSummaryStatistics implements IntConsumer in order that it can accept int values as well as long values.
For example, this allows you to pass it to a method requiring an IntConsumer in order to consume some int data abstractly:
LongSummaryStatistics lss = new LongSummaryStatistics();
someMethod(lss);
void someMethod(IntConsumer consumer) { ... }
There's no real reason why a LongSummaryStatistics shouldn't be usable for this purpose: int can always be widened to long without loss. However, the type system wouldn't allow lss to be used as a parameter to someMethod unless LongSummaryStatistics implemented IntConsumer directly.
True, you could do this without implementing the interface, using a lambda:
someMethod(i -> lss.consume(i));
but it's just a bit neater to use the reference directly.
edited 48 mins ago
answered 1 hour ago
Andy Turner
80.3k879133
80.3k879133
1
A follow-up question that comes to my mind, why not separate them out, why let consumers define separate streams ofintandlongand then mix them?
– nullpointer
1 hour ago
1
@nullpointer try defining a class which doesn't implementIntConsumer, and then pass it to a method which requires anIntConsumerparameter.
– Andy Turner
50 mins ago
1
I don't understand what you mean by "why let consumers define separate streams of int and long and then mix them"? Streams are not the only sources of data for*SummaryStatistics, you can invoke the methods directly.
– Andy Turner
44 mins ago
True, and when invoking the *SummaryStatistics directly too, why should one be collecting stats for varied type(int at once and long at another) of data is what I am thinking about. Maybe thinking too much there, but seemingly an example would cool down my thought process I guess.
– nullpointer
40 mins ago
add a comment |
1
A follow-up question that comes to my mind, why not separate them out, why let consumers define separate streams ofintandlongand then mix them?
– nullpointer
1 hour ago
1
@nullpointer try defining a class which doesn't implementIntConsumer, and then pass it to a method which requires anIntConsumerparameter.
– Andy Turner
50 mins ago
1
I don't understand what you mean by "why let consumers define separate streams of int and long and then mix them"? Streams are not the only sources of data for*SummaryStatistics, you can invoke the methods directly.
– Andy Turner
44 mins ago
True, and when invoking the *SummaryStatistics directly too, why should one be collecting stats for varied type(int at once and long at another) of data is what I am thinking about. Maybe thinking too much there, but seemingly an example would cool down my thought process I guess.
– nullpointer
40 mins ago
1
1
A follow-up question that comes to my mind, why not separate them out, why let consumers define separate streams of
int and long and then mix them?– nullpointer
1 hour ago
A follow-up question that comes to my mind, why not separate them out, why let consumers define separate streams of
int and long and then mix them?– nullpointer
1 hour ago
1
1
@nullpointer try defining a class which doesn't implement
IntConsumer, and then pass it to a method which requires an IntConsumer parameter.– Andy Turner
50 mins ago
@nullpointer try defining a class which doesn't implement
IntConsumer, and then pass it to a method which requires an IntConsumer parameter.– Andy Turner
50 mins ago
1
1
I don't understand what you mean by "why let consumers define separate streams of int and long and then mix them"? Streams are not the only sources of data for
*SummaryStatistics, you can invoke the methods directly.– Andy Turner
44 mins ago
I don't understand what you mean by "why let consumers define separate streams of int and long and then mix them"? Streams are not the only sources of data for
*SummaryStatistics, you can invoke the methods directly.– Andy Turner
44 mins ago
True, and when invoking the *SummaryStatistics directly too, why should one be collecting stats for varied type(int at once and long at another) of data is what I am thinking about. Maybe thinking too much there, but seemingly an example would cool down my thought process I guess.
– nullpointer
40 mins ago
True, and when invoking the *SummaryStatistics directly too, why should one be collecting stats for varied type(int at once and long at another) of data is what I am thinking about. Maybe thinking too much there, but seemingly an example would cool down my thought process I guess.
– nullpointer
40 mins ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54003796%2fwhy-does-longsummarystatistics-implements-intconsumer%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown