is it possible to pass a function with 2 parameters as parameter for another function in java 8/11?
I have this problem.
I am working with some generics and at one point I will need a specific converted depending on each type.
So, so far I have this:
public static <T> List<T> myMethod(List<T> list1, List2<T>, SomeFunction converter) {
//... do suff
return converter.convert(list1, list2);
}
and converter would be like this:
public <T> List<T> converter(List<T> list1, List<T> list2) {
/// cast and do stuff)
return List<T> some stuff;
}
Then I would like to make a call like
myMethod<list1,list2,converter);
I know about the functional internface Function but I need to send two parameters for this, is there any way I could do it in Java8/11?
Ideas?
java generics java-8 functional-interface
add a comment |
I have this problem.
I am working with some generics and at one point I will need a specific converted depending on each type.
So, so far I have this:
public static <T> List<T> myMethod(List<T> list1, List2<T>, SomeFunction converter) {
//... do suff
return converter.convert(list1, list2);
}
and converter would be like this:
public <T> List<T> converter(List<T> list1, List<T> list2) {
/// cast and do stuff)
return List<T> some stuff;
}
Then I would like to make a call like
myMethod<list1,list2,converter);
I know about the functional internface Function but I need to send two parameters for this, is there any way I could do it in Java8/11?
Ideas?
java generics java-8 functional-interface
4
Instead of Function<T,R> you could use BiFunction<T,U,R>
– bhspencer
Dec 31 '18 at 13:54
could you pleae give me an example? sorry about it but Im not good using this interface
– jpganz18
Dec 31 '18 at 13:58
add a comment |
I have this problem.
I am working with some generics and at one point I will need a specific converted depending on each type.
So, so far I have this:
public static <T> List<T> myMethod(List<T> list1, List2<T>, SomeFunction converter) {
//... do suff
return converter.convert(list1, list2);
}
and converter would be like this:
public <T> List<T> converter(List<T> list1, List<T> list2) {
/// cast and do stuff)
return List<T> some stuff;
}
Then I would like to make a call like
myMethod<list1,list2,converter);
I know about the functional internface Function but I need to send two parameters for this, is there any way I could do it in Java8/11?
Ideas?
java generics java-8 functional-interface
I have this problem.
I am working with some generics and at one point I will need a specific converted depending on each type.
So, so far I have this:
public static <T> List<T> myMethod(List<T> list1, List2<T>, SomeFunction converter) {
//... do suff
return converter.convert(list1, list2);
}
and converter would be like this:
public <T> List<T> converter(List<T> list1, List<T> list2) {
/// cast and do stuff)
return List<T> some stuff;
}
Then I would like to make a call like
myMethod<list1,list2,converter);
I know about the functional internface Function but I need to send two parameters for this, is there any way I could do it in Java8/11?
Ideas?
java generics java-8 functional-interface
java generics java-8 functional-interface
edited Dec 31 '18 at 14:48
Aomine
42.1k74172
42.1k74172
asked Dec 31 '18 at 13:52
jpganz18jpganz18
1,708103168
1,708103168
4
Instead of Function<T,R> you could use BiFunction<T,U,R>
– bhspencer
Dec 31 '18 at 13:54
could you pleae give me an example? sorry about it but Im not good using this interface
– jpganz18
Dec 31 '18 at 13:58
add a comment |
4
Instead of Function<T,R> you could use BiFunction<T,U,R>
– bhspencer
Dec 31 '18 at 13:54
could you pleae give me an example? sorry about it but Im not good using this interface
– jpganz18
Dec 31 '18 at 13:58
4
4
Instead of Function<T,R> you could use BiFunction<T,U,R>
– bhspencer
Dec 31 '18 at 13:54
Instead of Function<T,R> you could use BiFunction<T,U,R>
– bhspencer
Dec 31 '18 at 13:54
could you pleae give me an example? sorry about it but Im not good using this interface
– jpganz18
Dec 31 '18 at 13:58
could you pleae give me an example? sorry about it but Im not good using this interface
– jpganz18
Dec 31 '18 at 13:58
add a comment |
2 Answers
2
active
oldest
votes
Look into BinaryOperator which represents an operation upon two operands of the same type, producing a result of the same type as the operands.
public static <T> List<T> someMethodName(List<T> list1, List<T> list2,
BinaryOperator<List<T>> converter) {
return converter.apply(list1, list2);
}
BiFunction is also another option as it represents a function that accepts two arguments and produces a result.
public static <T, R> List<R> someMethodName(List<T> list1, List<T> list2,
BiFunction<List<T>, List<T>, List<R>> converter) {
return converter.apply(list1, list2);
}
To call the function let's assume you have two Integer lists for example sakes:
List<Integer> first = ....
List<Integer> second = ....
and you wanted to concatenate them, you'd pass both lists and a behaviour e.g.
List<Integer> concat = someMethodName(first, second,
(l, r) -> Stream.concat(l.stream(), r.stream())
.collect(Collectors.toList()));
3
Convention would dictate to have the result type be calledR, so<R> List<R> (List<T> list1, List<T>, BiFunction<List<T>,List<T>, List<R>> converter)which would also add flexibility.
– daniu
Dec 31 '18 at 14:05
4
@daniu Very good point (I never bear that in mind) - although type parameters would have to be declared as<T, R>
– ernest_k
Dec 31 '18 at 14:09
@daniu indeed, edited to accommodate. thanks.
– Aomine
Dec 31 '18 at 14:12
could you please give me an example of how the function to override the converter.apply would look like? I am a little bit lost :s
– jpganz18
Dec 31 '18 at 14:16
1
@jpganz18 for this specific question, I believe it solves the problem at hand, but feel free to post a new question if you're struggling with something you're working on. provided you've made an attempt along with a good description I am sure people on the site would be happy to help. good luck.
– Aomine
Dec 31 '18 at 14:36
|
show 6 more comments
You can make your function take a java.util.function.BinaryOperator<T>, which is a function that takes two parameters of the same type and returns a result of that same type:
public static <T> List<T> (List<T> list1, List<T>,
BinaryOperator<List<T>> converter) {
return converter.apply(list1, list2);
}
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%2f53988241%2fis-it-possible-to-pass-a-function-with-2-parameters-as-parameter-for-another-fun%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Look into BinaryOperator which represents an operation upon two operands of the same type, producing a result of the same type as the operands.
public static <T> List<T> someMethodName(List<T> list1, List<T> list2,
BinaryOperator<List<T>> converter) {
return converter.apply(list1, list2);
}
BiFunction is also another option as it represents a function that accepts two arguments and produces a result.
public static <T, R> List<R> someMethodName(List<T> list1, List<T> list2,
BiFunction<List<T>, List<T>, List<R>> converter) {
return converter.apply(list1, list2);
}
To call the function let's assume you have two Integer lists for example sakes:
List<Integer> first = ....
List<Integer> second = ....
and you wanted to concatenate them, you'd pass both lists and a behaviour e.g.
List<Integer> concat = someMethodName(first, second,
(l, r) -> Stream.concat(l.stream(), r.stream())
.collect(Collectors.toList()));
3
Convention would dictate to have the result type be calledR, so<R> List<R> (List<T> list1, List<T>, BiFunction<List<T>,List<T>, List<R>> converter)which would also add flexibility.
– daniu
Dec 31 '18 at 14:05
4
@daniu Very good point (I never bear that in mind) - although type parameters would have to be declared as<T, R>
– ernest_k
Dec 31 '18 at 14:09
@daniu indeed, edited to accommodate. thanks.
– Aomine
Dec 31 '18 at 14:12
could you please give me an example of how the function to override the converter.apply would look like? I am a little bit lost :s
– jpganz18
Dec 31 '18 at 14:16
1
@jpganz18 for this specific question, I believe it solves the problem at hand, but feel free to post a new question if you're struggling with something you're working on. provided you've made an attempt along with a good description I am sure people on the site would be happy to help. good luck.
– Aomine
Dec 31 '18 at 14:36
|
show 6 more comments
Look into BinaryOperator which represents an operation upon two operands of the same type, producing a result of the same type as the operands.
public static <T> List<T> someMethodName(List<T> list1, List<T> list2,
BinaryOperator<List<T>> converter) {
return converter.apply(list1, list2);
}
BiFunction is also another option as it represents a function that accepts two arguments and produces a result.
public static <T, R> List<R> someMethodName(List<T> list1, List<T> list2,
BiFunction<List<T>, List<T>, List<R>> converter) {
return converter.apply(list1, list2);
}
To call the function let's assume you have two Integer lists for example sakes:
List<Integer> first = ....
List<Integer> second = ....
and you wanted to concatenate them, you'd pass both lists and a behaviour e.g.
List<Integer> concat = someMethodName(first, second,
(l, r) -> Stream.concat(l.stream(), r.stream())
.collect(Collectors.toList()));
3
Convention would dictate to have the result type be calledR, so<R> List<R> (List<T> list1, List<T>, BiFunction<List<T>,List<T>, List<R>> converter)which would also add flexibility.
– daniu
Dec 31 '18 at 14:05
4
@daniu Very good point (I never bear that in mind) - although type parameters would have to be declared as<T, R>
– ernest_k
Dec 31 '18 at 14:09
@daniu indeed, edited to accommodate. thanks.
– Aomine
Dec 31 '18 at 14:12
could you please give me an example of how the function to override the converter.apply would look like? I am a little bit lost :s
– jpganz18
Dec 31 '18 at 14:16
1
@jpganz18 for this specific question, I believe it solves the problem at hand, but feel free to post a new question if you're struggling with something you're working on. provided you've made an attempt along with a good description I am sure people on the site would be happy to help. good luck.
– Aomine
Dec 31 '18 at 14:36
|
show 6 more comments
Look into BinaryOperator which represents an operation upon two operands of the same type, producing a result of the same type as the operands.
public static <T> List<T> someMethodName(List<T> list1, List<T> list2,
BinaryOperator<List<T>> converter) {
return converter.apply(list1, list2);
}
BiFunction is also another option as it represents a function that accepts two arguments and produces a result.
public static <T, R> List<R> someMethodName(List<T> list1, List<T> list2,
BiFunction<List<T>, List<T>, List<R>> converter) {
return converter.apply(list1, list2);
}
To call the function let's assume you have two Integer lists for example sakes:
List<Integer> first = ....
List<Integer> second = ....
and you wanted to concatenate them, you'd pass both lists and a behaviour e.g.
List<Integer> concat = someMethodName(first, second,
(l, r) -> Stream.concat(l.stream(), r.stream())
.collect(Collectors.toList()));
Look into BinaryOperator which represents an operation upon two operands of the same type, producing a result of the same type as the operands.
public static <T> List<T> someMethodName(List<T> list1, List<T> list2,
BinaryOperator<List<T>> converter) {
return converter.apply(list1, list2);
}
BiFunction is also another option as it represents a function that accepts two arguments and produces a result.
public static <T, R> List<R> someMethodName(List<T> list1, List<T> list2,
BiFunction<List<T>, List<T>, List<R>> converter) {
return converter.apply(list1, list2);
}
To call the function let's assume you have two Integer lists for example sakes:
List<Integer> first = ....
List<Integer> second = ....
and you wanted to concatenate them, you'd pass both lists and a behaviour e.g.
List<Integer> concat = someMethodName(first, second,
(l, r) -> Stream.concat(l.stream(), r.stream())
.collect(Collectors.toList()));
edited Dec 31 '18 at 14:21
answered Dec 31 '18 at 13:54
AomineAomine
42.1k74172
42.1k74172
3
Convention would dictate to have the result type be calledR, so<R> List<R> (List<T> list1, List<T>, BiFunction<List<T>,List<T>, List<R>> converter)which would also add flexibility.
– daniu
Dec 31 '18 at 14:05
4
@daniu Very good point (I never bear that in mind) - although type parameters would have to be declared as<T, R>
– ernest_k
Dec 31 '18 at 14:09
@daniu indeed, edited to accommodate. thanks.
– Aomine
Dec 31 '18 at 14:12
could you please give me an example of how the function to override the converter.apply would look like? I am a little bit lost :s
– jpganz18
Dec 31 '18 at 14:16
1
@jpganz18 for this specific question, I believe it solves the problem at hand, but feel free to post a new question if you're struggling with something you're working on. provided you've made an attempt along with a good description I am sure people on the site would be happy to help. good luck.
– Aomine
Dec 31 '18 at 14:36
|
show 6 more comments
3
Convention would dictate to have the result type be calledR, so<R> List<R> (List<T> list1, List<T>, BiFunction<List<T>,List<T>, List<R>> converter)which would also add flexibility.
– daniu
Dec 31 '18 at 14:05
4
@daniu Very good point (I never bear that in mind) - although type parameters would have to be declared as<T, R>
– ernest_k
Dec 31 '18 at 14:09
@daniu indeed, edited to accommodate. thanks.
– Aomine
Dec 31 '18 at 14:12
could you please give me an example of how the function to override the converter.apply would look like? I am a little bit lost :s
– jpganz18
Dec 31 '18 at 14:16
1
@jpganz18 for this specific question, I believe it solves the problem at hand, but feel free to post a new question if you're struggling with something you're working on. provided you've made an attempt along with a good description I am sure people on the site would be happy to help. good luck.
– Aomine
Dec 31 '18 at 14:36
3
3
Convention would dictate to have the result type be called
R, so <R> List<R> (List<T> list1, List<T>, BiFunction<List<T>,List<T>, List<R>> converter) which would also add flexibility.– daniu
Dec 31 '18 at 14:05
Convention would dictate to have the result type be called
R, so <R> List<R> (List<T> list1, List<T>, BiFunction<List<T>,List<T>, List<R>> converter) which would also add flexibility.– daniu
Dec 31 '18 at 14:05
4
4
@daniu Very good point (I never bear that in mind) - although type parameters would have to be declared as
<T, R>– ernest_k
Dec 31 '18 at 14:09
@daniu Very good point (I never bear that in mind) - although type parameters would have to be declared as
<T, R>– ernest_k
Dec 31 '18 at 14:09
@daniu indeed, edited to accommodate. thanks.
– Aomine
Dec 31 '18 at 14:12
@daniu indeed, edited to accommodate. thanks.
– Aomine
Dec 31 '18 at 14:12
could you please give me an example of how the function to override the converter.apply would look like? I am a little bit lost :s
– jpganz18
Dec 31 '18 at 14:16
could you please give me an example of how the function to override the converter.apply would look like? I am a little bit lost :s
– jpganz18
Dec 31 '18 at 14:16
1
1
@jpganz18 for this specific question, I believe it solves the problem at hand, but feel free to post a new question if you're struggling with something you're working on. provided you've made an attempt along with a good description I am sure people on the site would be happy to help. good luck.
– Aomine
Dec 31 '18 at 14:36
@jpganz18 for this specific question, I believe it solves the problem at hand, but feel free to post a new question if you're struggling with something you're working on. provided you've made an attempt along with a good description I am sure people on the site would be happy to help. good luck.
– Aomine
Dec 31 '18 at 14:36
|
show 6 more comments
You can make your function take a java.util.function.BinaryOperator<T>, which is a function that takes two parameters of the same type and returns a result of that same type:
public static <T> List<T> (List<T> list1, List<T>,
BinaryOperator<List<T>> converter) {
return converter.apply(list1, list2);
}
add a comment |
You can make your function take a java.util.function.BinaryOperator<T>, which is a function that takes two parameters of the same type and returns a result of that same type:
public static <T> List<T> (List<T> list1, List<T>,
BinaryOperator<List<T>> converter) {
return converter.apply(list1, list2);
}
add a comment |
You can make your function take a java.util.function.BinaryOperator<T>, which is a function that takes two parameters of the same type and returns a result of that same type:
public static <T> List<T> (List<T> list1, List<T>,
BinaryOperator<List<T>> converter) {
return converter.apply(list1, list2);
}
You can make your function take a java.util.function.BinaryOperator<T>, which is a function that takes two parameters of the same type and returns a result of that same type:
public static <T> List<T> (List<T> list1, List<T>,
BinaryOperator<List<T>> converter) {
return converter.apply(list1, list2);
}
answered Dec 31 '18 at 13:55
ernest_kernest_k
22k42446
22k42446
add a comment |
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%2f53988241%2fis-it-possible-to-pass-a-function-with-2-parameters-as-parameter-for-another-fun%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
4
Instead of Function<T,R> you could use BiFunction<T,U,R>
– bhspencer
Dec 31 '18 at 13:54
could you pleae give me an example? sorry about it but Im not good using this interface
– jpganz18
Dec 31 '18 at 13:58