Functional class level arguments in Java 8

Multi tool use
up vote
2
down vote
favorite
I would like to make more of an effort to use Java 8's functional features and shift my thinking towards that paradigm; however, I'm not quite sure how to reconcile this scenario.
For example I recently wrote something similar to this:
public class ExampleClass
private ExampleData exampleData;
private Function<ExampleData, Boolean> workFunction;
public ExampleData getExampeData()
return exampleData;
public void setExampleData(ExampleData exampleData)
this.exampleData = exampleData;
public Function<ExampleData, Boolean> getWorkFunction()
return workFunction;
public void setWorkFunction(Function<ExampleData, Boolean> workFunction)
this.workFunction = workFunction;
then I proceed to use it like so...
public class Worker implements Callable {
private final ExampleClass exampleClass;
// ExampleClass serves as a container for the function and it's
// input data in order to minimize the number of arguments
// passed to this constructor. Otherwise would be
// public Worker(ExampleData exampleData, Function<T, R> workFunction, ...)
// Note: using "..." here to represent more args.
public Worker(ExampleClass exampleClass, ...)
this.exampleClass = exampleClass;
...
@Override
public void call()
final Boolean isComplete = exampleClass.getWorkFunction().apply(exampleClass.getExampleData());
if (isComplete)
...
...
Would something like the above be preferred over what I would consider the more traditional approach?
public class ExampleClass
private ExampleData exampleData;
public ExampleClass(ExampleData exampleData)
this.exampleData = exampleData;
public Boolean work()
return isComplete(exampleData);
and the implementing class...
public class Worker implements Callable
private ExampleClass exampleClass;
public Worker(ExampleClass exampleClass, ...)
this.exampleClass = exampleClass;
...
@Override
public void call()
final Boolean isComplete = exampleClass.work();
if (isComplete)
...
...
If I'm completely off base, what would be a proper functional solution using Java 8?
Update: Lets say my example doesn't return a Boolean. In the context of functional programming with Java 8, is it better to explicitly pass a function as a class level argument or is it better to just use the traditional object oriented way of just passing another class that performs that function?
java design-patterns java-8 functional-programming
|
show 2 more comments
up vote
2
down vote
favorite
I would like to make more of an effort to use Java 8's functional features and shift my thinking towards that paradigm; however, I'm not quite sure how to reconcile this scenario.
For example I recently wrote something similar to this:
public class ExampleClass
private ExampleData exampleData;
private Function<ExampleData, Boolean> workFunction;
public ExampleData getExampeData()
return exampleData;
public void setExampleData(ExampleData exampleData)
this.exampleData = exampleData;
public Function<ExampleData, Boolean> getWorkFunction()
return workFunction;
public void setWorkFunction(Function<ExampleData, Boolean> workFunction)
this.workFunction = workFunction;
then I proceed to use it like so...
public class Worker implements Callable {
private final ExampleClass exampleClass;
// ExampleClass serves as a container for the function and it's
// input data in order to minimize the number of arguments
// passed to this constructor. Otherwise would be
// public Worker(ExampleData exampleData, Function<T, R> workFunction, ...)
// Note: using "..." here to represent more args.
public Worker(ExampleClass exampleClass, ...)
this.exampleClass = exampleClass;
...
@Override
public void call()
final Boolean isComplete = exampleClass.getWorkFunction().apply(exampleClass.getExampleData());
if (isComplete)
...
...
Would something like the above be preferred over what I would consider the more traditional approach?
public class ExampleClass
private ExampleData exampleData;
public ExampleClass(ExampleData exampleData)
this.exampleData = exampleData;
public Boolean work()
return isComplete(exampleData);
and the implementing class...
public class Worker implements Callable
private ExampleClass exampleClass;
public Worker(ExampleClass exampleClass, ...)
this.exampleClass = exampleClass;
...
@Override
public void call()
final Boolean isComplete = exampleClass.work();
if (isComplete)
...
...
If I'm completely off base, what would be a proper functional solution using Java 8?
Update: Lets say my example doesn't return a Boolean. In the context of functional programming with Java 8, is it better to explicitly pass a function as a class level argument or is it better to just use the traditional object oriented way of just passing another class that performs that function?
java design-patterns java-8 functional-programming
Welcome to Stack Overflow! Please take the tour and visit our help center to learn what kinds of questions are appropriate for this site. Opinion-based questions such as this are off-topic, as they tend to result in angry debates here. If you can edit your question to fit the requirements of this site, please do so.
– Joe C
Nov 10 at 23:05
It’s hardly an opinion based question. By that definition all design pattern questions would be a matter of opinion.
– 3xecve
Nov 10 at 23:08
1
There are different opinions about what is "opinion based" ;-) The main issue with your code is IMHO that it is too vague and sketchy. AnExampleClass
may be helpful to get an idea across, but here, this is not the case: It is not clear what this class is supposed to be (i.e. how it could map to a "real world class"), and what theFunction
part is supposed to accomplish. The way it is, it looks highly dubious (as if you had thrown in theFunction
randomly, because ... functional!!!111), but maybe there's a deeper meaning/intention/goal behind this...?!
– Marco13
Nov 11 at 0:08
1
More specifically: TheExampleClass
is basically a POJO, consisting of two elements: The data and the function. And the only way how this class is used is by obtaining the data and the function, and doing something with them. The class itself does not seem to serve any purpose, beyond being a "container". Maybe a more realistic example could make the question clearer.
– Marco13
Nov 11 at 0:10
The ExampleClass is a POJO. Its only purpose is to minimize the number of arguments passed to the Worker class. I guess my question doesn't have a lot to do with the ExampleClass, its more about explicitly passing functions as class level arguments vs passing classes that perform functions. I've updated my question to hopefully clarify that.
– 3xecve
Nov 11 at 2:20
|
show 2 more comments
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I would like to make more of an effort to use Java 8's functional features and shift my thinking towards that paradigm; however, I'm not quite sure how to reconcile this scenario.
For example I recently wrote something similar to this:
public class ExampleClass
private ExampleData exampleData;
private Function<ExampleData, Boolean> workFunction;
public ExampleData getExampeData()
return exampleData;
public void setExampleData(ExampleData exampleData)
this.exampleData = exampleData;
public Function<ExampleData, Boolean> getWorkFunction()
return workFunction;
public void setWorkFunction(Function<ExampleData, Boolean> workFunction)
this.workFunction = workFunction;
then I proceed to use it like so...
public class Worker implements Callable {
private final ExampleClass exampleClass;
// ExampleClass serves as a container for the function and it's
// input data in order to minimize the number of arguments
// passed to this constructor. Otherwise would be
// public Worker(ExampleData exampleData, Function<T, R> workFunction, ...)
// Note: using "..." here to represent more args.
public Worker(ExampleClass exampleClass, ...)
this.exampleClass = exampleClass;
...
@Override
public void call()
final Boolean isComplete = exampleClass.getWorkFunction().apply(exampleClass.getExampleData());
if (isComplete)
...
...
Would something like the above be preferred over what I would consider the more traditional approach?
public class ExampleClass
private ExampleData exampleData;
public ExampleClass(ExampleData exampleData)
this.exampleData = exampleData;
public Boolean work()
return isComplete(exampleData);
and the implementing class...
public class Worker implements Callable
private ExampleClass exampleClass;
public Worker(ExampleClass exampleClass, ...)
this.exampleClass = exampleClass;
...
@Override
public void call()
final Boolean isComplete = exampleClass.work();
if (isComplete)
...
...
If I'm completely off base, what would be a proper functional solution using Java 8?
Update: Lets say my example doesn't return a Boolean. In the context of functional programming with Java 8, is it better to explicitly pass a function as a class level argument or is it better to just use the traditional object oriented way of just passing another class that performs that function?
java design-patterns java-8 functional-programming
I would like to make more of an effort to use Java 8's functional features and shift my thinking towards that paradigm; however, I'm not quite sure how to reconcile this scenario.
For example I recently wrote something similar to this:
public class ExampleClass
private ExampleData exampleData;
private Function<ExampleData, Boolean> workFunction;
public ExampleData getExampeData()
return exampleData;
public void setExampleData(ExampleData exampleData)
this.exampleData = exampleData;
public Function<ExampleData, Boolean> getWorkFunction()
return workFunction;
public void setWorkFunction(Function<ExampleData, Boolean> workFunction)
this.workFunction = workFunction;
then I proceed to use it like so...
public class Worker implements Callable {
private final ExampleClass exampleClass;
// ExampleClass serves as a container for the function and it's
// input data in order to minimize the number of arguments
// passed to this constructor. Otherwise would be
// public Worker(ExampleData exampleData, Function<T, R> workFunction, ...)
// Note: using "..." here to represent more args.
public Worker(ExampleClass exampleClass, ...)
this.exampleClass = exampleClass;
...
@Override
public void call()
final Boolean isComplete = exampleClass.getWorkFunction().apply(exampleClass.getExampleData());
if (isComplete)
...
...
Would something like the above be preferred over what I would consider the more traditional approach?
public class ExampleClass
private ExampleData exampleData;
public ExampleClass(ExampleData exampleData)
this.exampleData = exampleData;
public Boolean work()
return isComplete(exampleData);
and the implementing class...
public class Worker implements Callable
private ExampleClass exampleClass;
public Worker(ExampleClass exampleClass, ...)
this.exampleClass = exampleClass;
...
@Override
public void call()
final Boolean isComplete = exampleClass.work();
if (isComplete)
...
...
If I'm completely off base, what would be a proper functional solution using Java 8?
Update: Lets say my example doesn't return a Boolean. In the context of functional programming with Java 8, is it better to explicitly pass a function as a class level argument or is it better to just use the traditional object oriented way of just passing another class that performs that function?
java design-patterns java-8 functional-programming
java design-patterns java-8 functional-programming
edited Nov 11 at 2:45
asked Nov 10 at 23:00
3xecve
133
133
Welcome to Stack Overflow! Please take the tour and visit our help center to learn what kinds of questions are appropriate for this site. Opinion-based questions such as this are off-topic, as they tend to result in angry debates here. If you can edit your question to fit the requirements of this site, please do so.
– Joe C
Nov 10 at 23:05
It’s hardly an opinion based question. By that definition all design pattern questions would be a matter of opinion.
– 3xecve
Nov 10 at 23:08
1
There are different opinions about what is "opinion based" ;-) The main issue with your code is IMHO that it is too vague and sketchy. AnExampleClass
may be helpful to get an idea across, but here, this is not the case: It is not clear what this class is supposed to be (i.e. how it could map to a "real world class"), and what theFunction
part is supposed to accomplish. The way it is, it looks highly dubious (as if you had thrown in theFunction
randomly, because ... functional!!!111), but maybe there's a deeper meaning/intention/goal behind this...?!
– Marco13
Nov 11 at 0:08
1
More specifically: TheExampleClass
is basically a POJO, consisting of two elements: The data and the function. And the only way how this class is used is by obtaining the data and the function, and doing something with them. The class itself does not seem to serve any purpose, beyond being a "container". Maybe a more realistic example could make the question clearer.
– Marco13
Nov 11 at 0:10
The ExampleClass is a POJO. Its only purpose is to minimize the number of arguments passed to the Worker class. I guess my question doesn't have a lot to do with the ExampleClass, its more about explicitly passing functions as class level arguments vs passing classes that perform functions. I've updated my question to hopefully clarify that.
– 3xecve
Nov 11 at 2:20
|
show 2 more comments
Welcome to Stack Overflow! Please take the tour and visit our help center to learn what kinds of questions are appropriate for this site. Opinion-based questions such as this are off-topic, as they tend to result in angry debates here. If you can edit your question to fit the requirements of this site, please do so.
– Joe C
Nov 10 at 23:05
It’s hardly an opinion based question. By that definition all design pattern questions would be a matter of opinion.
– 3xecve
Nov 10 at 23:08
1
There are different opinions about what is "opinion based" ;-) The main issue with your code is IMHO that it is too vague and sketchy. AnExampleClass
may be helpful to get an idea across, but here, this is not the case: It is not clear what this class is supposed to be (i.e. how it could map to a "real world class"), and what theFunction
part is supposed to accomplish. The way it is, it looks highly dubious (as if you had thrown in theFunction
randomly, because ... functional!!!111), but maybe there's a deeper meaning/intention/goal behind this...?!
– Marco13
Nov 11 at 0:08
1
More specifically: TheExampleClass
is basically a POJO, consisting of two elements: The data and the function. And the only way how this class is used is by obtaining the data and the function, and doing something with them. The class itself does not seem to serve any purpose, beyond being a "container". Maybe a more realistic example could make the question clearer.
– Marco13
Nov 11 at 0:10
The ExampleClass is a POJO. Its only purpose is to minimize the number of arguments passed to the Worker class. I guess my question doesn't have a lot to do with the ExampleClass, its more about explicitly passing functions as class level arguments vs passing classes that perform functions. I've updated my question to hopefully clarify that.
– 3xecve
Nov 11 at 2:20
Welcome to Stack Overflow! Please take the tour and visit our help center to learn what kinds of questions are appropriate for this site. Opinion-based questions such as this are off-topic, as they tend to result in angry debates here. If you can edit your question to fit the requirements of this site, please do so.
– Joe C
Nov 10 at 23:05
Welcome to Stack Overflow! Please take the tour and visit our help center to learn what kinds of questions are appropriate for this site. Opinion-based questions such as this are off-topic, as they tend to result in angry debates here. If you can edit your question to fit the requirements of this site, please do so.
– Joe C
Nov 10 at 23:05
It’s hardly an opinion based question. By that definition all design pattern questions would be a matter of opinion.
– 3xecve
Nov 10 at 23:08
It’s hardly an opinion based question. By that definition all design pattern questions would be a matter of opinion.
– 3xecve
Nov 10 at 23:08
1
1
There are different opinions about what is "opinion based" ;-) The main issue with your code is IMHO that it is too vague and sketchy. An
ExampleClass
may be helpful to get an idea across, but here, this is not the case: It is not clear what this class is supposed to be (i.e. how it could map to a "real world class"), and what the Function
part is supposed to accomplish. The way it is, it looks highly dubious (as if you had thrown in the Function
randomly, because ... functional!!!111), but maybe there's a deeper meaning/intention/goal behind this...?!– Marco13
Nov 11 at 0:08
There are different opinions about what is "opinion based" ;-) The main issue with your code is IMHO that it is too vague and sketchy. An
ExampleClass
may be helpful to get an idea across, but here, this is not the case: It is not clear what this class is supposed to be (i.e. how it could map to a "real world class"), and what the Function
part is supposed to accomplish. The way it is, it looks highly dubious (as if you had thrown in the Function
randomly, because ... functional!!!111), but maybe there's a deeper meaning/intention/goal behind this...?!– Marco13
Nov 11 at 0:08
1
1
More specifically: The
ExampleClass
is basically a POJO, consisting of two elements: The data and the function. And the only way how this class is used is by obtaining the data and the function, and doing something with them. The class itself does not seem to serve any purpose, beyond being a "container". Maybe a more realistic example could make the question clearer.– Marco13
Nov 11 at 0:10
More specifically: The
ExampleClass
is basically a POJO, consisting of two elements: The data and the function. And the only way how this class is used is by obtaining the data and the function, and doing something with them. The class itself does not seem to serve any purpose, beyond being a "container". Maybe a more realistic example could make the question clearer.– Marco13
Nov 11 at 0:10
The ExampleClass is a POJO. Its only purpose is to minimize the number of arguments passed to the Worker class. I guess my question doesn't have a lot to do with the ExampleClass, its more about explicitly passing functions as class level arguments vs passing classes that perform functions. I've updated my question to hopefully clarify that.
– 3xecve
Nov 11 at 2:20
The ExampleClass is a POJO. Its only purpose is to minimize the number of arguments passed to the Worker class. I guess my question doesn't have a lot to do with the ExampleClass, its more about explicitly passing functions as class level arguments vs passing classes that perform functions. I've updated my question to hopefully clarify that.
– 3xecve
Nov 11 at 2:20
|
show 2 more comments
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
The point of Functional programming is that The function must be treated as an object.
but you know, function(method) is not object in java. it is differ to javascript.
so, we should use interface called Predicate.
List<Apple> apples = new ArrayList<>();
you want to get weight apples. originally,
List<Apple> weightApples = new ArrayList<>();
for(int i = 0; 9< apples.size(); i++)
if(apples.getWeight() > 10)
weightApples.add(apples.get(i));
this code.
in functional programming, function is be 'pure function'.
in pure function, You should avoid using external variables in functions.
and, we dont care function's logic in 'the line using function'
first, we need predicdate interface.
interface Predicate<T>
public boolean pass(T t);
by generic, we can extend object type.
we can make filter.
public static <T> List<T> filter(List<T> list, Predicate<T> predicate)
List<T> result = new ArrayList<>();
for(T el : list)
if(predicate.pass(el))
result.add(el);
return result;
the pass() is not implemented not yet.
it can implemented by annonymous class.
finally, method is treated like object by use lamda!
filter(apples, (Apple apple) -> apple.getWeight()>10);
i'm not people in english, so it is poor english.
but i hope it is helpful for you! thanks.
This does help, but let’s say I modified my example so that the function I was looking to pass did not return a Boolean thus would not work as a predicate and that I was trying to pass a function to a Worker thread that extends from Callable or Runnable meaning the function has to be passed through the constructor to be used in its overridable run method. Is it okay to have a container class that holds a function?
– 3xecve
Nov 11 at 1:05
add a comment |
up vote
0
down vote
First of all, a function that accepts one argument and returns a boolean should implement Java 8 Predicate
.
However, in my opinion, predicates were not meant to replace any and all if
statements. They are meant to be used as filters in Java 8's collection streams. The example you provided does not seem to fit this situation.
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',
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%2f53244259%2ffunctional-class-level-arguments-in-java-8%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
up vote
1
down vote
accepted
The point of Functional programming is that The function must be treated as an object.
but you know, function(method) is not object in java. it is differ to javascript.
so, we should use interface called Predicate.
List<Apple> apples = new ArrayList<>();
you want to get weight apples. originally,
List<Apple> weightApples = new ArrayList<>();
for(int i = 0; 9< apples.size(); i++)
if(apples.getWeight() > 10)
weightApples.add(apples.get(i));
this code.
in functional programming, function is be 'pure function'.
in pure function, You should avoid using external variables in functions.
and, we dont care function's logic in 'the line using function'
first, we need predicdate interface.
interface Predicate<T>
public boolean pass(T t);
by generic, we can extend object type.
we can make filter.
public static <T> List<T> filter(List<T> list, Predicate<T> predicate)
List<T> result = new ArrayList<>();
for(T el : list)
if(predicate.pass(el))
result.add(el);
return result;
the pass() is not implemented not yet.
it can implemented by annonymous class.
finally, method is treated like object by use lamda!
filter(apples, (Apple apple) -> apple.getWeight()>10);
i'm not people in english, so it is poor english.
but i hope it is helpful for you! thanks.
This does help, but let’s say I modified my example so that the function I was looking to pass did not return a Boolean thus would not work as a predicate and that I was trying to pass a function to a Worker thread that extends from Callable or Runnable meaning the function has to be passed through the constructor to be used in its overridable run method. Is it okay to have a container class that holds a function?
– 3xecve
Nov 11 at 1:05
add a comment |
up vote
1
down vote
accepted
The point of Functional programming is that The function must be treated as an object.
but you know, function(method) is not object in java. it is differ to javascript.
so, we should use interface called Predicate.
List<Apple> apples = new ArrayList<>();
you want to get weight apples. originally,
List<Apple> weightApples = new ArrayList<>();
for(int i = 0; 9< apples.size(); i++)
if(apples.getWeight() > 10)
weightApples.add(apples.get(i));
this code.
in functional programming, function is be 'pure function'.
in pure function, You should avoid using external variables in functions.
and, we dont care function's logic in 'the line using function'
first, we need predicdate interface.
interface Predicate<T>
public boolean pass(T t);
by generic, we can extend object type.
we can make filter.
public static <T> List<T> filter(List<T> list, Predicate<T> predicate)
List<T> result = new ArrayList<>();
for(T el : list)
if(predicate.pass(el))
result.add(el);
return result;
the pass() is not implemented not yet.
it can implemented by annonymous class.
finally, method is treated like object by use lamda!
filter(apples, (Apple apple) -> apple.getWeight()>10);
i'm not people in english, so it is poor english.
but i hope it is helpful for you! thanks.
This does help, but let’s say I modified my example so that the function I was looking to pass did not return a Boolean thus would not work as a predicate and that I was trying to pass a function to a Worker thread that extends from Callable or Runnable meaning the function has to be passed through the constructor to be used in its overridable run method. Is it okay to have a container class that holds a function?
– 3xecve
Nov 11 at 1:05
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
The point of Functional programming is that The function must be treated as an object.
but you know, function(method) is not object in java. it is differ to javascript.
so, we should use interface called Predicate.
List<Apple> apples = new ArrayList<>();
you want to get weight apples. originally,
List<Apple> weightApples = new ArrayList<>();
for(int i = 0; 9< apples.size(); i++)
if(apples.getWeight() > 10)
weightApples.add(apples.get(i));
this code.
in functional programming, function is be 'pure function'.
in pure function, You should avoid using external variables in functions.
and, we dont care function's logic in 'the line using function'
first, we need predicdate interface.
interface Predicate<T>
public boolean pass(T t);
by generic, we can extend object type.
we can make filter.
public static <T> List<T> filter(List<T> list, Predicate<T> predicate)
List<T> result = new ArrayList<>();
for(T el : list)
if(predicate.pass(el))
result.add(el);
return result;
the pass() is not implemented not yet.
it can implemented by annonymous class.
finally, method is treated like object by use lamda!
filter(apples, (Apple apple) -> apple.getWeight()>10);
i'm not people in english, so it is poor english.
but i hope it is helpful for you! thanks.
The point of Functional programming is that The function must be treated as an object.
but you know, function(method) is not object in java. it is differ to javascript.
so, we should use interface called Predicate.
List<Apple> apples = new ArrayList<>();
you want to get weight apples. originally,
List<Apple> weightApples = new ArrayList<>();
for(int i = 0; 9< apples.size(); i++)
if(apples.getWeight() > 10)
weightApples.add(apples.get(i));
this code.
in functional programming, function is be 'pure function'.
in pure function, You should avoid using external variables in functions.
and, we dont care function's logic in 'the line using function'
first, we need predicdate interface.
interface Predicate<T>
public boolean pass(T t);
by generic, we can extend object type.
we can make filter.
public static <T> List<T> filter(List<T> list, Predicate<T> predicate)
List<T> result = new ArrayList<>();
for(T el : list)
if(predicate.pass(el))
result.add(el);
return result;
the pass() is not implemented not yet.
it can implemented by annonymous class.
finally, method is treated like object by use lamda!
filter(apples, (Apple apple) -> apple.getWeight()>10);
i'm not people in english, so it is poor english.
but i hope it is helpful for you! thanks.
answered Nov 11 at 0:38
곽대용
455
455
This does help, but let’s say I modified my example so that the function I was looking to pass did not return a Boolean thus would not work as a predicate and that I was trying to pass a function to a Worker thread that extends from Callable or Runnable meaning the function has to be passed through the constructor to be used in its overridable run method. Is it okay to have a container class that holds a function?
– 3xecve
Nov 11 at 1:05
add a comment |
This does help, but let’s say I modified my example so that the function I was looking to pass did not return a Boolean thus would not work as a predicate and that I was trying to pass a function to a Worker thread that extends from Callable or Runnable meaning the function has to be passed through the constructor to be used in its overridable run method. Is it okay to have a container class that holds a function?
– 3xecve
Nov 11 at 1:05
This does help, but let’s say I modified my example so that the function I was looking to pass did not return a Boolean thus would not work as a predicate and that I was trying to pass a function to a Worker thread that extends from Callable or Runnable meaning the function has to be passed through the constructor to be used in its overridable run method. Is it okay to have a container class that holds a function?
– 3xecve
Nov 11 at 1:05
This does help, but let’s say I modified my example so that the function I was looking to pass did not return a Boolean thus would not work as a predicate and that I was trying to pass a function to a Worker thread that extends from Callable or Runnable meaning the function has to be passed through the constructor to be used in its overridable run method. Is it okay to have a container class that holds a function?
– 3xecve
Nov 11 at 1:05
add a comment |
up vote
0
down vote
First of all, a function that accepts one argument and returns a boolean should implement Java 8 Predicate
.
However, in my opinion, predicates were not meant to replace any and all if
statements. They are meant to be used as filters in Java 8's collection streams. The example you provided does not seem to fit this situation.
add a comment |
up vote
0
down vote
First of all, a function that accepts one argument and returns a boolean should implement Java 8 Predicate
.
However, in my opinion, predicates were not meant to replace any and all if
statements. They are meant to be used as filters in Java 8's collection streams. The example you provided does not seem to fit this situation.
add a comment |
up vote
0
down vote
up vote
0
down vote
First of all, a function that accepts one argument and returns a boolean should implement Java 8 Predicate
.
However, in my opinion, predicates were not meant to replace any and all if
statements. They are meant to be used as filters in Java 8's collection streams. The example you provided does not seem to fit this situation.
First of all, a function that accepts one argument and returns a boolean should implement Java 8 Predicate
.
However, in my opinion, predicates were not meant to replace any and all if
statements. They are meant to be used as filters in Java 8's collection streams. The example you provided does not seem to fit this situation.
answered Nov 10 at 23:12
Sharon Ben Asher
8,66731936
8,66731936
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.
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%2f53244259%2ffunctional-class-level-arguments-in-java-8%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
IURG0nz4RGIuZKL eqI1CpRj frl,qn XE4K95ncUh7
Welcome to Stack Overflow! Please take the tour and visit our help center to learn what kinds of questions are appropriate for this site. Opinion-based questions such as this are off-topic, as they tend to result in angry debates here. If you can edit your question to fit the requirements of this site, please do so.
– Joe C
Nov 10 at 23:05
It’s hardly an opinion based question. By that definition all design pattern questions would be a matter of opinion.
– 3xecve
Nov 10 at 23:08
1
There are different opinions about what is "opinion based" ;-) The main issue with your code is IMHO that it is too vague and sketchy. An
ExampleClass
may be helpful to get an idea across, but here, this is not the case: It is not clear what this class is supposed to be (i.e. how it could map to a "real world class"), and what theFunction
part is supposed to accomplish. The way it is, it looks highly dubious (as if you had thrown in theFunction
randomly, because ... functional!!!111), but maybe there's a deeper meaning/intention/goal behind this...?!– Marco13
Nov 11 at 0:08
1
More specifically: The
ExampleClass
is basically a POJO, consisting of two elements: The data and the function. And the only way how this class is used is by obtaining the data and the function, and doing something with them. The class itself does not seem to serve any purpose, beyond being a "container". Maybe a more realistic example could make the question clearer.– Marco13
Nov 11 at 0:10
The ExampleClass is a POJO. Its only purpose is to minimize the number of arguments passed to the Worker class. I guess my question doesn't have a lot to do with the ExampleClass, its more about explicitly passing functions as class level arguments vs passing classes that perform functions. I've updated my question to hopefully clarify that.
– 3xecve
Nov 11 at 2:20