In java 8+ are only single argument method reference permitted in a stream
Is it true to say that in a Stream
in Java 8, you can only use method references that take a single argument (if you disallow wrapping the method reference with a method call)?
I assume so because in a stream at any time you are processing a single item.
Therefore:
Something::new
(must refer to a single arg constructor)
this::doSomething
(must take a single arg)
Something::doSomething
(must take a single arg)
...when used in a Stream
. Is this rule always true?
java lambda java-8 functional-programming java-stream
add a comment |
Is it true to say that in a Stream
in Java 8, you can only use method references that take a single argument (if you disallow wrapping the method reference with a method call)?
I assume so because in a stream at any time you are processing a single item.
Therefore:
Something::new
(must refer to a single arg constructor)
this::doSomething
(must take a single arg)
Something::doSomething
(must take a single arg)
...when used in a Stream
. Is this rule always true?
java lambda java-8 functional-programming java-stream
add a comment |
Is it true to say that in a Stream
in Java 8, you can only use method references that take a single argument (if you disallow wrapping the method reference with a method call)?
I assume so because in a stream at any time you are processing a single item.
Therefore:
Something::new
(must refer to a single arg constructor)
this::doSomething
(must take a single arg)
Something::doSomething
(must take a single arg)
...when used in a Stream
. Is this rule always true?
java lambda java-8 functional-programming java-stream
Is it true to say that in a Stream
in Java 8, you can only use method references that take a single argument (if you disallow wrapping the method reference with a method call)?
I assume so because in a stream at any time you are processing a single item.
Therefore:
Something::new
(must refer to a single arg constructor)
this::doSomething
(must take a single arg)
Something::doSomething
(must take a single arg)
...when used in a Stream
. Is this rule always true?
java lambda java-8 functional-programming java-stream
java lambda java-8 functional-programming java-stream
edited Dec 31 '18 at 9:51
Eran
284k37462550
284k37462550
asked Dec 31 '18 at 9:39
Andy CribbensAndy Cribbens
11310
11310
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
No, it's not. Some of the Stream
methods take functional interfaces having a method with multiple arguments.
For example, Stream
's sorted(Stream<T> Comparator<? super T> comparator)
method, takes a Comparator
, whose method has two arguments.
Here's an example of using a method reference - String::compareTo
- of a method having two arguments:
System.out.println(Stream.of("a","d","c").sorted(String::compareTo).collect(Collectors.toList()));
Stream
's Optional<T> max(Comparator<? super T> comparator)
method is another similar example.
But in this case the method reference is wrapped in a method call (toMap). In my question, I was asking about any cases where it's not wrapped.
– Andy Cribbens
Dec 31 '18 at 9:47
Excellent. I think the second part of your answer has answered my question.
– Andy Cribbens
Dec 31 '18 at 9:50
add a comment |
There are four types of methods references:
A method reference to a static method i.e.
Class::staticMethod
-->(args) -> Class.staticMethod(args)
A method reference to an instance method of an object of a particular type. i.e.
ObjectType::instanceMethod
-->(obj, args) -> obj.instanceMethod(args)
A method reference to an instance method of an existing object i.e.
obj::instanceMethod
-->(args) -> obj.instanceMethod(args)
A method reference to a constructor i.e.
ClassName::new
-->(args) -> new ClassName(args)
As you can see with the second example, a given method can take two arguments and still be translated to a method reference, this is true for the case of calling sorted
, min
, max
etc.. of a stream.
credit to Java 8 Method Reference: How to Use it for the examples above.
add a comment |
Just to add to the answer from Eran, you can possibly find a real life example better to understand.
Let's assume we have a method that adds an Integer i
and an intValue
of a Long l
to return back as a String s
representation. This would look like :
String convertToStringAfterAddingValues(Long l, Integer i) {
return String.valueOf(l.intValue() + i);
}
In the world of FunctionalInterface
, this could be represented as a BiFunction
as an anonymous class:
BiFunction<Long, Integer, String> biFunctionAnonymous = new BiFunction<Long, Integer, String>() {
@Override
public String apply(Long l, Integer i) {
return String.valueOf(l.intValue() + i);
}
};
which in the lambda world could then be represented as:
BiFunction<Long, Integer, String> biFunctLambda = (l, i) -> String.valueOf(l.intValue() + i);
the same can be represented using method reference with the object of the class the method resides in as :
BiFunction<Long, Integer, String> biFunctMethodReference = <YourClassInstance>::convertToStringAfterAddingValues;
1
This is useful but I don't think it addresses the question.
– Andy Cribbens
Jan 2 at 8:37
1
@AndyCribbens Its just an example of how more than one argument can be used within streams using method references. The exact answer to your question was anyway provided by Eran. Just to compare with your questionSomething::doSomething (must take a single arg)
and the last line of code in the above example, you can see how it is not relying on a single argument instead two arguments at the same time.
– nullpointer
Jan 2 at 8:41
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%2f53985887%2fin-java-8-are-only-single-argument-method-reference-permitted-in-a-stream%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
No, it's not. Some of the Stream
methods take functional interfaces having a method with multiple arguments.
For example, Stream
's sorted(Stream<T> Comparator<? super T> comparator)
method, takes a Comparator
, whose method has two arguments.
Here's an example of using a method reference - String::compareTo
- of a method having two arguments:
System.out.println(Stream.of("a","d","c").sorted(String::compareTo).collect(Collectors.toList()));
Stream
's Optional<T> max(Comparator<? super T> comparator)
method is another similar example.
But in this case the method reference is wrapped in a method call (toMap). In my question, I was asking about any cases where it's not wrapped.
– Andy Cribbens
Dec 31 '18 at 9:47
Excellent. I think the second part of your answer has answered my question.
– Andy Cribbens
Dec 31 '18 at 9:50
add a comment |
No, it's not. Some of the Stream
methods take functional interfaces having a method with multiple arguments.
For example, Stream
's sorted(Stream<T> Comparator<? super T> comparator)
method, takes a Comparator
, whose method has two arguments.
Here's an example of using a method reference - String::compareTo
- of a method having two arguments:
System.out.println(Stream.of("a","d","c").sorted(String::compareTo).collect(Collectors.toList()));
Stream
's Optional<T> max(Comparator<? super T> comparator)
method is another similar example.
But in this case the method reference is wrapped in a method call (toMap). In my question, I was asking about any cases where it's not wrapped.
– Andy Cribbens
Dec 31 '18 at 9:47
Excellent. I think the second part of your answer has answered my question.
– Andy Cribbens
Dec 31 '18 at 9:50
add a comment |
No, it's not. Some of the Stream
methods take functional interfaces having a method with multiple arguments.
For example, Stream
's sorted(Stream<T> Comparator<? super T> comparator)
method, takes a Comparator
, whose method has two arguments.
Here's an example of using a method reference - String::compareTo
- of a method having two arguments:
System.out.println(Stream.of("a","d","c").sorted(String::compareTo).collect(Collectors.toList()));
Stream
's Optional<T> max(Comparator<? super T> comparator)
method is another similar example.
No, it's not. Some of the Stream
methods take functional interfaces having a method with multiple arguments.
For example, Stream
's sorted(Stream<T> Comparator<? super T> comparator)
method, takes a Comparator
, whose method has two arguments.
Here's an example of using a method reference - String::compareTo
- of a method having two arguments:
System.out.println(Stream.of("a","d","c").sorted(String::compareTo).collect(Collectors.toList()));
Stream
's Optional<T> max(Comparator<? super T> comparator)
method is another similar example.
edited Dec 31 '18 at 9:50
answered Dec 31 '18 at 9:44
EranEran
284k37462550
284k37462550
But in this case the method reference is wrapped in a method call (toMap). In my question, I was asking about any cases where it's not wrapped.
– Andy Cribbens
Dec 31 '18 at 9:47
Excellent. I think the second part of your answer has answered my question.
– Andy Cribbens
Dec 31 '18 at 9:50
add a comment |
But in this case the method reference is wrapped in a method call (toMap). In my question, I was asking about any cases where it's not wrapped.
– Andy Cribbens
Dec 31 '18 at 9:47
Excellent. I think the second part of your answer has answered my question.
– Andy Cribbens
Dec 31 '18 at 9:50
But in this case the method reference is wrapped in a method call (toMap). In my question, I was asking about any cases where it's not wrapped.
– Andy Cribbens
Dec 31 '18 at 9:47
But in this case the method reference is wrapped in a method call (toMap). In my question, I was asking about any cases where it's not wrapped.
– Andy Cribbens
Dec 31 '18 at 9:47
Excellent. I think the second part of your answer has answered my question.
– Andy Cribbens
Dec 31 '18 at 9:50
Excellent. I think the second part of your answer has answered my question.
– Andy Cribbens
Dec 31 '18 at 9:50
add a comment |
There are four types of methods references:
A method reference to a static method i.e.
Class::staticMethod
-->(args) -> Class.staticMethod(args)
A method reference to an instance method of an object of a particular type. i.e.
ObjectType::instanceMethod
-->(obj, args) -> obj.instanceMethod(args)
A method reference to an instance method of an existing object i.e.
obj::instanceMethod
-->(args) -> obj.instanceMethod(args)
A method reference to a constructor i.e.
ClassName::new
-->(args) -> new ClassName(args)
As you can see with the second example, a given method can take two arguments and still be translated to a method reference, this is true for the case of calling sorted
, min
, max
etc.. of a stream.
credit to Java 8 Method Reference: How to Use it for the examples above.
add a comment |
There are four types of methods references:
A method reference to a static method i.e.
Class::staticMethod
-->(args) -> Class.staticMethod(args)
A method reference to an instance method of an object of a particular type. i.e.
ObjectType::instanceMethod
-->(obj, args) -> obj.instanceMethod(args)
A method reference to an instance method of an existing object i.e.
obj::instanceMethod
-->(args) -> obj.instanceMethod(args)
A method reference to a constructor i.e.
ClassName::new
-->(args) -> new ClassName(args)
As you can see with the second example, a given method can take two arguments and still be translated to a method reference, this is true for the case of calling sorted
, min
, max
etc.. of a stream.
credit to Java 8 Method Reference: How to Use it for the examples above.
add a comment |
There are four types of methods references:
A method reference to a static method i.e.
Class::staticMethod
-->(args) -> Class.staticMethod(args)
A method reference to an instance method of an object of a particular type. i.e.
ObjectType::instanceMethod
-->(obj, args) -> obj.instanceMethod(args)
A method reference to an instance method of an existing object i.e.
obj::instanceMethod
-->(args) -> obj.instanceMethod(args)
A method reference to a constructor i.e.
ClassName::new
-->(args) -> new ClassName(args)
As you can see with the second example, a given method can take two arguments and still be translated to a method reference, this is true for the case of calling sorted
, min
, max
etc.. of a stream.
credit to Java 8 Method Reference: How to Use it for the examples above.
There are four types of methods references:
A method reference to a static method i.e.
Class::staticMethod
-->(args) -> Class.staticMethod(args)
A method reference to an instance method of an object of a particular type. i.e.
ObjectType::instanceMethod
-->(obj, args) -> obj.instanceMethod(args)
A method reference to an instance method of an existing object i.e.
obj::instanceMethod
-->(args) -> obj.instanceMethod(args)
A method reference to a constructor i.e.
ClassName::new
-->(args) -> new ClassName(args)
As you can see with the second example, a given method can take two arguments and still be translated to a method reference, this is true for the case of calling sorted
, min
, max
etc.. of a stream.
credit to Java 8 Method Reference: How to Use it for the examples above.
answered Dec 31 '18 at 9:52
AomineAomine
42.1k74172
42.1k74172
add a comment |
add a comment |
Just to add to the answer from Eran, you can possibly find a real life example better to understand.
Let's assume we have a method that adds an Integer i
and an intValue
of a Long l
to return back as a String s
representation. This would look like :
String convertToStringAfterAddingValues(Long l, Integer i) {
return String.valueOf(l.intValue() + i);
}
In the world of FunctionalInterface
, this could be represented as a BiFunction
as an anonymous class:
BiFunction<Long, Integer, String> biFunctionAnonymous = new BiFunction<Long, Integer, String>() {
@Override
public String apply(Long l, Integer i) {
return String.valueOf(l.intValue() + i);
}
};
which in the lambda world could then be represented as:
BiFunction<Long, Integer, String> biFunctLambda = (l, i) -> String.valueOf(l.intValue() + i);
the same can be represented using method reference with the object of the class the method resides in as :
BiFunction<Long, Integer, String> biFunctMethodReference = <YourClassInstance>::convertToStringAfterAddingValues;
1
This is useful but I don't think it addresses the question.
– Andy Cribbens
Jan 2 at 8:37
1
@AndyCribbens Its just an example of how more than one argument can be used within streams using method references. The exact answer to your question was anyway provided by Eran. Just to compare with your questionSomething::doSomething (must take a single arg)
and the last line of code in the above example, you can see how it is not relying on a single argument instead two arguments at the same time.
– nullpointer
Jan 2 at 8:41
add a comment |
Just to add to the answer from Eran, you can possibly find a real life example better to understand.
Let's assume we have a method that adds an Integer i
and an intValue
of a Long l
to return back as a String s
representation. This would look like :
String convertToStringAfterAddingValues(Long l, Integer i) {
return String.valueOf(l.intValue() + i);
}
In the world of FunctionalInterface
, this could be represented as a BiFunction
as an anonymous class:
BiFunction<Long, Integer, String> biFunctionAnonymous = new BiFunction<Long, Integer, String>() {
@Override
public String apply(Long l, Integer i) {
return String.valueOf(l.intValue() + i);
}
};
which in the lambda world could then be represented as:
BiFunction<Long, Integer, String> biFunctLambda = (l, i) -> String.valueOf(l.intValue() + i);
the same can be represented using method reference with the object of the class the method resides in as :
BiFunction<Long, Integer, String> biFunctMethodReference = <YourClassInstance>::convertToStringAfterAddingValues;
1
This is useful but I don't think it addresses the question.
– Andy Cribbens
Jan 2 at 8:37
1
@AndyCribbens Its just an example of how more than one argument can be used within streams using method references. The exact answer to your question was anyway provided by Eran. Just to compare with your questionSomething::doSomething (must take a single arg)
and the last line of code in the above example, you can see how it is not relying on a single argument instead two arguments at the same time.
– nullpointer
Jan 2 at 8:41
add a comment |
Just to add to the answer from Eran, you can possibly find a real life example better to understand.
Let's assume we have a method that adds an Integer i
and an intValue
of a Long l
to return back as a String s
representation. This would look like :
String convertToStringAfterAddingValues(Long l, Integer i) {
return String.valueOf(l.intValue() + i);
}
In the world of FunctionalInterface
, this could be represented as a BiFunction
as an anonymous class:
BiFunction<Long, Integer, String> biFunctionAnonymous = new BiFunction<Long, Integer, String>() {
@Override
public String apply(Long l, Integer i) {
return String.valueOf(l.intValue() + i);
}
};
which in the lambda world could then be represented as:
BiFunction<Long, Integer, String> biFunctLambda = (l, i) -> String.valueOf(l.intValue() + i);
the same can be represented using method reference with the object of the class the method resides in as :
BiFunction<Long, Integer, String> biFunctMethodReference = <YourClassInstance>::convertToStringAfterAddingValues;
Just to add to the answer from Eran, you can possibly find a real life example better to understand.
Let's assume we have a method that adds an Integer i
and an intValue
of a Long l
to return back as a String s
representation. This would look like :
String convertToStringAfterAddingValues(Long l, Integer i) {
return String.valueOf(l.intValue() + i);
}
In the world of FunctionalInterface
, this could be represented as a BiFunction
as an anonymous class:
BiFunction<Long, Integer, String> biFunctionAnonymous = new BiFunction<Long, Integer, String>() {
@Override
public String apply(Long l, Integer i) {
return String.valueOf(l.intValue() + i);
}
};
which in the lambda world could then be represented as:
BiFunction<Long, Integer, String> biFunctLambda = (l, i) -> String.valueOf(l.intValue() + i);
the same can be represented using method reference with the object of the class the method resides in as :
BiFunction<Long, Integer, String> biFunctMethodReference = <YourClassInstance>::convertToStringAfterAddingValues;
edited Dec 31 '18 at 10:24
answered Dec 31 '18 at 10:09
nullpointernullpointer
47.8k11100194
47.8k11100194
1
This is useful but I don't think it addresses the question.
– Andy Cribbens
Jan 2 at 8:37
1
@AndyCribbens Its just an example of how more than one argument can be used within streams using method references. The exact answer to your question was anyway provided by Eran. Just to compare with your questionSomething::doSomething (must take a single arg)
and the last line of code in the above example, you can see how it is not relying on a single argument instead two arguments at the same time.
– nullpointer
Jan 2 at 8:41
add a comment |
1
This is useful but I don't think it addresses the question.
– Andy Cribbens
Jan 2 at 8:37
1
@AndyCribbens Its just an example of how more than one argument can be used within streams using method references. The exact answer to your question was anyway provided by Eran. Just to compare with your questionSomething::doSomething (must take a single arg)
and the last line of code in the above example, you can see how it is not relying on a single argument instead two arguments at the same time.
– nullpointer
Jan 2 at 8:41
1
1
This is useful but I don't think it addresses the question.
– Andy Cribbens
Jan 2 at 8:37
This is useful but I don't think it addresses the question.
– Andy Cribbens
Jan 2 at 8:37
1
1
@AndyCribbens Its just an example of how more than one argument can be used within streams using method references. The exact answer to your question was anyway provided by Eran. Just to compare with your question
Something::doSomething (must take a single arg)
and the last line of code in the above example, you can see how it is not relying on a single argument instead two arguments at the same time.– nullpointer
Jan 2 at 8:41
@AndyCribbens Its just an example of how more than one argument can be used within streams using method references. The exact answer to your question was anyway provided by Eran. Just to compare with your question
Something::doSomething (must take a single arg)
and the last line of code in the above example, you can see how it is not relying on a single argument instead two arguments at the same time.– nullpointer
Jan 2 at 8:41
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.
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%2f53985887%2fin-java-8-are-only-single-argument-method-reference-permitted-in-a-stream%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