What's the elegant way to replace following code in java 8?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have the following code in my java program (paraphrased from the actual program):
import java.util.Objects;
import java.util.function.BiPredicate;
class Class1
String propertyA;
boolean propertyANot;
String propertyB; //Not all members have corresponding negate indicator
String propertyC;
String propertyD;
boolean propertyDNot;
....
class Class2
String propertyX;
String propertyY;
String propertyZ;
String propertyW;
....
public class Main
//Fail early without inspecting all properties
private BiPredicate<Class1,Class2> matchObjects = (obj1,obj2) ->
if(obj1.propertyA != null && obj2.propertyX != null)
//property1Not is a boolean indicates negation
if(obj1.propertyANot && !Objects.equals(obj1.propertyA,obj2.propertyX))
return false;
else if(!obj1.propertyANot && Objects.equals(obj1.propertyA,obj2.propertyX))
return false;
if(obj1.propertyB != null && obj2.propertyY != null)
if(!Objects.equals(obj1.propertyB,obj2.propertyY))
return false;
....
return true;
;
As indicated in the comments, I want the matchObjects method to fail as soon as one of the matching conditions fail. Also the input objects don't have one-to-one correspondence to generalize the code in a loop.
I have following question:
What's the best way to rewrite this code using functional programming
concepts of java8?
Having too many if conditions are making me think there is a scope for improvement here.
I also wrote a custom predicate that takes multiple & Optional
parameters to generalize the testing of property equivalence in the if
conditions.
However this is not helping me to get rid of multiple if
conditions.
Please help & thanks in advance.
lambda java-8
add a comment |
I have the following code in my java program (paraphrased from the actual program):
import java.util.Objects;
import java.util.function.BiPredicate;
class Class1
String propertyA;
boolean propertyANot;
String propertyB; //Not all members have corresponding negate indicator
String propertyC;
String propertyD;
boolean propertyDNot;
....
class Class2
String propertyX;
String propertyY;
String propertyZ;
String propertyW;
....
public class Main
//Fail early without inspecting all properties
private BiPredicate<Class1,Class2> matchObjects = (obj1,obj2) ->
if(obj1.propertyA != null && obj2.propertyX != null)
//property1Not is a boolean indicates negation
if(obj1.propertyANot && !Objects.equals(obj1.propertyA,obj2.propertyX))
return false;
else if(!obj1.propertyANot && Objects.equals(obj1.propertyA,obj2.propertyX))
return false;
if(obj1.propertyB != null && obj2.propertyY != null)
if(!Objects.equals(obj1.propertyB,obj2.propertyY))
return false;
....
return true;
;
As indicated in the comments, I want the matchObjects method to fail as soon as one of the matching conditions fail. Also the input objects don't have one-to-one correspondence to generalize the code in a loop.
I have following question:
What's the best way to rewrite this code using functional programming
concepts of java8?
Having too many if conditions are making me think there is a scope for improvement here.
I also wrote a custom predicate that takes multiple & Optional
parameters to generalize the testing of property equivalence in the if
conditions.
However this is not helping me to get rid of multiple if
conditions.
Please help & thanks in advance.
lambda java-8
add a comment |
I have the following code in my java program (paraphrased from the actual program):
import java.util.Objects;
import java.util.function.BiPredicate;
class Class1
String propertyA;
boolean propertyANot;
String propertyB; //Not all members have corresponding negate indicator
String propertyC;
String propertyD;
boolean propertyDNot;
....
class Class2
String propertyX;
String propertyY;
String propertyZ;
String propertyW;
....
public class Main
//Fail early without inspecting all properties
private BiPredicate<Class1,Class2> matchObjects = (obj1,obj2) ->
if(obj1.propertyA != null && obj2.propertyX != null)
//property1Not is a boolean indicates negation
if(obj1.propertyANot && !Objects.equals(obj1.propertyA,obj2.propertyX))
return false;
else if(!obj1.propertyANot && Objects.equals(obj1.propertyA,obj2.propertyX))
return false;
if(obj1.propertyB != null && obj2.propertyY != null)
if(!Objects.equals(obj1.propertyB,obj2.propertyY))
return false;
....
return true;
;
As indicated in the comments, I want the matchObjects method to fail as soon as one of the matching conditions fail. Also the input objects don't have one-to-one correspondence to generalize the code in a loop.
I have following question:
What's the best way to rewrite this code using functional programming
concepts of java8?
Having too many if conditions are making me think there is a scope for improvement here.
I also wrote a custom predicate that takes multiple & Optional
parameters to generalize the testing of property equivalence in the if
conditions.
However this is not helping me to get rid of multiple if
conditions.
Please help & thanks in advance.
lambda java-8
I have the following code in my java program (paraphrased from the actual program):
import java.util.Objects;
import java.util.function.BiPredicate;
class Class1
String propertyA;
boolean propertyANot;
String propertyB; //Not all members have corresponding negate indicator
String propertyC;
String propertyD;
boolean propertyDNot;
....
class Class2
String propertyX;
String propertyY;
String propertyZ;
String propertyW;
....
public class Main
//Fail early without inspecting all properties
private BiPredicate<Class1,Class2> matchObjects = (obj1,obj2) ->
if(obj1.propertyA != null && obj2.propertyX != null)
//property1Not is a boolean indicates negation
if(obj1.propertyANot && !Objects.equals(obj1.propertyA,obj2.propertyX))
return false;
else if(!obj1.propertyANot && Objects.equals(obj1.propertyA,obj2.propertyX))
return false;
if(obj1.propertyB != null && obj2.propertyY != null)
if(!Objects.equals(obj1.propertyB,obj2.propertyY))
return false;
....
return true;
;
As indicated in the comments, I want the matchObjects method to fail as soon as one of the matching conditions fail. Also the input objects don't have one-to-one correspondence to generalize the code in a loop.
I have following question:
What's the best way to rewrite this code using functional programming
concepts of java8?
Having too many if conditions are making me think there is a scope for improvement here.
I also wrote a custom predicate that takes multiple & Optional
parameters to generalize the testing of property equivalence in the if
conditions.
However this is not helping me to get rid of multiple if
conditions.
Please help & thanks in advance.
lambda java-8
lambda java-8
asked Nov 15 '18 at 17:20
Bhanuprakash DBhanuprakash D
595514
595514
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can try something like this:
@FunctionalInterface interface TriPredicate<A, B, C>
boolean test (A a, B b, C c);
private BiPredicate<Class1, Class2> matchObjects = (obj1, obj2) -> ;
1
+1. The issue is that I have 2 digit properties in both my classes in which case, the return statement becomes too large. I am on the lookout for chaining lambda functions of sorts.
– Bhanuprakash D
Nov 15 '18 at 18:47
What means too large? Doesn't it compile?
– Donat
Nov 15 '18 at 18:54
@Federico Peralta Schaffner : yes, I think this may be possible. Good point.
– Donat
Nov 15 '18 at 20:16
1
@Federico Peralta Schaffner : Yes, I have checked. It is possible.
– Donat
Nov 15 '18 at 21:02
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%2f53324805%2fwhats-the-elegant-way-to-replace-following-code-in-java-8%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
You can try something like this:
@FunctionalInterface interface TriPredicate<A, B, C>
boolean test (A a, B b, C c);
private BiPredicate<Class1, Class2> matchObjects = (obj1, obj2) -> ;
1
+1. The issue is that I have 2 digit properties in both my classes in which case, the return statement becomes too large. I am on the lookout for chaining lambda functions of sorts.
– Bhanuprakash D
Nov 15 '18 at 18:47
What means too large? Doesn't it compile?
– Donat
Nov 15 '18 at 18:54
@Federico Peralta Schaffner : yes, I think this may be possible. Good point.
– Donat
Nov 15 '18 at 20:16
1
@Federico Peralta Schaffner : Yes, I have checked. It is possible.
– Donat
Nov 15 '18 at 21:02
add a comment |
You can try something like this:
@FunctionalInterface interface TriPredicate<A, B, C>
boolean test (A a, B b, C c);
private BiPredicate<Class1, Class2> matchObjects = (obj1, obj2) -> ;
1
+1. The issue is that I have 2 digit properties in both my classes in which case, the return statement becomes too large. I am on the lookout for chaining lambda functions of sorts.
– Bhanuprakash D
Nov 15 '18 at 18:47
What means too large? Doesn't it compile?
– Donat
Nov 15 '18 at 18:54
@Federico Peralta Schaffner : yes, I think this may be possible. Good point.
– Donat
Nov 15 '18 at 20:16
1
@Federico Peralta Schaffner : Yes, I have checked. It is possible.
– Donat
Nov 15 '18 at 21:02
add a comment |
You can try something like this:
@FunctionalInterface interface TriPredicate<A, B, C>
boolean test (A a, B b, C c);
private BiPredicate<Class1, Class2> matchObjects = (obj1, obj2) -> ;
You can try something like this:
@FunctionalInterface interface TriPredicate<A, B, C>
boolean test (A a, B b, C c);
private BiPredicate<Class1, Class2> matchObjects = (obj1, obj2) -> ;
edited Nov 15 '18 at 21:01
answered Nov 15 '18 at 18:25
DonatDonat
848128
848128
1
+1. The issue is that I have 2 digit properties in both my classes in which case, the return statement becomes too large. I am on the lookout for chaining lambda functions of sorts.
– Bhanuprakash D
Nov 15 '18 at 18:47
What means too large? Doesn't it compile?
– Donat
Nov 15 '18 at 18:54
@Federico Peralta Schaffner : yes, I think this may be possible. Good point.
– Donat
Nov 15 '18 at 20:16
1
@Federico Peralta Schaffner : Yes, I have checked. It is possible.
– Donat
Nov 15 '18 at 21:02
add a comment |
1
+1. The issue is that I have 2 digit properties in both my classes in which case, the return statement becomes too large. I am on the lookout for chaining lambda functions of sorts.
– Bhanuprakash D
Nov 15 '18 at 18:47
What means too large? Doesn't it compile?
– Donat
Nov 15 '18 at 18:54
@Federico Peralta Schaffner : yes, I think this may be possible. Good point.
– Donat
Nov 15 '18 at 20:16
1
@Federico Peralta Schaffner : Yes, I have checked. It is possible.
– Donat
Nov 15 '18 at 21:02
1
1
+1. The issue is that I have 2 digit properties in both my classes in which case, the return statement becomes too large. I am on the lookout for chaining lambda functions of sorts.
– Bhanuprakash D
Nov 15 '18 at 18:47
+1. The issue is that I have 2 digit properties in both my classes in which case, the return statement becomes too large. I am on the lookout for chaining lambda functions of sorts.
– Bhanuprakash D
Nov 15 '18 at 18:47
What means too large? Doesn't it compile?
– Donat
Nov 15 '18 at 18:54
What means too large? Doesn't it compile?
– Donat
Nov 15 '18 at 18:54
@Federico Peralta Schaffner : yes, I think this may be possible. Good point.
– Donat
Nov 15 '18 at 20:16
@Federico Peralta Schaffner : yes, I think this may be possible. Good point.
– Donat
Nov 15 '18 at 20:16
1
1
@Federico Peralta Schaffner : Yes, I have checked. It is possible.
– Donat
Nov 15 '18 at 21:02
@Federico Peralta Schaffner : Yes, I have checked. It is possible.
– Donat
Nov 15 '18 at 21:02
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%2f53324805%2fwhats-the-elegant-way-to-replace-following-code-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