Comparing a string[] to a list of objects with stream?
I have a list of:
String properties = {"prop1","prop2","prop3"};
List<CustomObject> listOfObjects; // contains objects and each object contains a property of `properties` array
class CustomObject{
String prop1;
String prop2;
String prop3;
}
I want to check every properties
value to every listOfObjects
objects properties and check if the value is empty. I just want to check that none of the objects have empty properties.
I know there's stream()
but I'm not sure how to use it correctly.
listOfObjects.stream().anyMatch(x -> x.get(/*What do i put in here?*/) == "");
Update: here's what would basically work without stream()
for(String prop: properties) {
for(CustomObject pdo: listOfObjects) {
if(pdo.get(prop) == "") {
System.out.println("Some of the required fields are empty");
}
}
}
java java-stream
|
show 8 more comments
I have a list of:
String properties = {"prop1","prop2","prop3"};
List<CustomObject> listOfObjects; // contains objects and each object contains a property of `properties` array
class CustomObject{
String prop1;
String prop2;
String prop3;
}
I want to check every properties
value to every listOfObjects
objects properties and check if the value is empty. I just want to check that none of the objects have empty properties.
I know there's stream()
but I'm not sure how to use it correctly.
listOfObjects.stream().anyMatch(x -> x.get(/*What do i put in here?*/) == "");
Update: here's what would basically work without stream()
for(String prop: properties) {
for(CustomObject pdo: listOfObjects) {
if(pdo.get(prop) == "") {
System.out.println("Some of the required fields are empty");
}
}
}
java java-stream
Not really. Just a check whether any of the properties is empty.
– sander
Nov 22 '18 at 13:53
1
1. Why aList<Object>
, use some custom object. 2.get
would depend on the type of object . 3 useequals
to compare strings.
– nullpointer
Nov 22 '18 at 13:55
1
There is no simple builtin facility to treat aString
like an object’s property. This is not Javascript.
– Holger
Nov 22 '18 at 13:59
1
what does .get(String prop) return if the property is not found?
– Brad
Nov 22 '18 at 14:08
1
Please share theCustomObject
class definition. I think that's the most crucial part that you've abstracted from the question && 3. useequals
to compare strings.
– nullpointer
Nov 22 '18 at 14:23
|
show 8 more comments
I have a list of:
String properties = {"prop1","prop2","prop3"};
List<CustomObject> listOfObjects; // contains objects and each object contains a property of `properties` array
class CustomObject{
String prop1;
String prop2;
String prop3;
}
I want to check every properties
value to every listOfObjects
objects properties and check if the value is empty. I just want to check that none of the objects have empty properties.
I know there's stream()
but I'm not sure how to use it correctly.
listOfObjects.stream().anyMatch(x -> x.get(/*What do i put in here?*/) == "");
Update: here's what would basically work without stream()
for(String prop: properties) {
for(CustomObject pdo: listOfObjects) {
if(pdo.get(prop) == "") {
System.out.println("Some of the required fields are empty");
}
}
}
java java-stream
I have a list of:
String properties = {"prop1","prop2","prop3"};
List<CustomObject> listOfObjects; // contains objects and each object contains a property of `properties` array
class CustomObject{
String prop1;
String prop2;
String prop3;
}
I want to check every properties
value to every listOfObjects
objects properties and check if the value is empty. I just want to check that none of the objects have empty properties.
I know there's stream()
but I'm not sure how to use it correctly.
listOfObjects.stream().anyMatch(x -> x.get(/*What do i put in here?*/) == "");
Update: here's what would basically work without stream()
for(String prop: properties) {
for(CustomObject pdo: listOfObjects) {
if(pdo.get(prop) == "") {
System.out.println("Some of the required fields are empty");
}
}
}
java java-stream
java java-stream
edited Nov 22 '18 at 14:21
sander
asked Nov 22 '18 at 13:51


sandersander
351116
351116
Not really. Just a check whether any of the properties is empty.
– sander
Nov 22 '18 at 13:53
1
1. Why aList<Object>
, use some custom object. 2.get
would depend on the type of object . 3 useequals
to compare strings.
– nullpointer
Nov 22 '18 at 13:55
1
There is no simple builtin facility to treat aString
like an object’s property. This is not Javascript.
– Holger
Nov 22 '18 at 13:59
1
what does .get(String prop) return if the property is not found?
– Brad
Nov 22 '18 at 14:08
1
Please share theCustomObject
class definition. I think that's the most crucial part that you've abstracted from the question && 3. useequals
to compare strings.
– nullpointer
Nov 22 '18 at 14:23
|
show 8 more comments
Not really. Just a check whether any of the properties is empty.
– sander
Nov 22 '18 at 13:53
1
1. Why aList<Object>
, use some custom object. 2.get
would depend on the type of object . 3 useequals
to compare strings.
– nullpointer
Nov 22 '18 at 13:55
1
There is no simple builtin facility to treat aString
like an object’s property. This is not Javascript.
– Holger
Nov 22 '18 at 13:59
1
what does .get(String prop) return if the property is not found?
– Brad
Nov 22 '18 at 14:08
1
Please share theCustomObject
class definition. I think that's the most crucial part that you've abstracted from the question && 3. useequals
to compare strings.
– nullpointer
Nov 22 '18 at 14:23
Not really. Just a check whether any of the properties is empty.
– sander
Nov 22 '18 at 13:53
Not really. Just a check whether any of the properties is empty.
– sander
Nov 22 '18 at 13:53
1
1
1. Why a
List<Object>
, use some custom object. 2. get
would depend on the type of object . 3 use equals
to compare strings.– nullpointer
Nov 22 '18 at 13:55
1. Why a
List<Object>
, use some custom object. 2. get
would depend on the type of object . 3 use equals
to compare strings.– nullpointer
Nov 22 '18 at 13:55
1
1
There is no simple builtin facility to treat a
String
like an object’s property. This is not Javascript.– Holger
Nov 22 '18 at 13:59
There is no simple builtin facility to treat a
String
like an object’s property. This is not Javascript.– Holger
Nov 22 '18 at 13:59
1
1
what does .get(String prop) return if the property is not found?
– Brad
Nov 22 '18 at 14:08
what does .get(String prop) return if the property is not found?
– Brad
Nov 22 '18 at 14:08
1
1
Please share the
CustomObject
class definition. I think that's the most crucial part that you've abstracted from the question && 3. use equals
to compare strings.– nullpointer
Nov 22 '18 at 14:23
Please share the
CustomObject
class definition. I think that's the most crucial part that you've abstracted from the question && 3. use equals
to compare strings.– nullpointer
Nov 22 '18 at 14:23
|
show 8 more comments
2 Answers
2
active
oldest
votes
Just for the sake of using Streams and anyMatch you can use this operation to check if any of the properties are blank. This is just to answer your question specifically, my suggestion is to take some time and improve this as there is a lot of room.
listOfObject.stream()
.anyMatch(obj -> Arrays.stream(obj)
.anyMatch(p -> !(obj.get(p)!= null && !obj.get(p).isEmpty())));
1
I think this is the answer I was looking for, I will test it later and accept if it does work.
– sander
Nov 22 '18 at 14:52
Instead of.equals("")
, you can use.isEmpty()
.
– Holger
Nov 22 '18 at 15:37
Good point, made me think about the case where the value retrieved by the get method is null. Currently with.equals()
it would return false and would invalidate the outcome, and with.isEmpty
it would raise aNullPointerException
. I will update my answer, considering this case.
– Brad
Nov 22 '18 at 16:13
@sander, out of curiosity, did it solve your issue?
– Brad
Jan 13 at 10:05
add a comment |
You can just use a list of Predicate
s. Each of the following predicates tests one of the properties for equality against ""
.
List<Predicate<CustomObject>> propertiesCheckers =
Arrays.asList(pdo -> "".equals(pdo.get("prop1")),
pdo -> "".equals(pdo.get("prop2")),
pdo -> "".equals(pdo.get("prop3")));
And this stream checks each element against the predicate, returning true
when the first empty value is found:
boolean anyEmpty = listOfObjects.stream()
.flatMap(ob -> propertiesCheckers.stream().map(pred -> pred.test(ob)))
.anyMatch(b -> b); //returns true if any "".equals returned true
if(anyEmpty) System.out.println("Some of the required fields are empty");
1. Though honestly, I am not very sure if I understand the question right. But, does this not miss the relation between theString properties
in the question to theList<CustomObejct> listOfObjects
somewhere? 2. There is also a typo in your checkers left withemp
.
– nullpointer
Nov 22 '18 at 14:21
1
@nullpointer Thanks, fixed typo. Edited to use the added example in the question. So I'm proposing the OP changeString properties
toList<Predicate<CustomObject>>
based on those property names.
– ernest_k
Nov 22 '18 at 14:37
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%2f53432477%2fcomparing-a-string-to-a-list-of-objects-with-stream%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
Just for the sake of using Streams and anyMatch you can use this operation to check if any of the properties are blank. This is just to answer your question specifically, my suggestion is to take some time and improve this as there is a lot of room.
listOfObject.stream()
.anyMatch(obj -> Arrays.stream(obj)
.anyMatch(p -> !(obj.get(p)!= null && !obj.get(p).isEmpty())));
1
I think this is the answer I was looking for, I will test it later and accept if it does work.
– sander
Nov 22 '18 at 14:52
Instead of.equals("")
, you can use.isEmpty()
.
– Holger
Nov 22 '18 at 15:37
Good point, made me think about the case where the value retrieved by the get method is null. Currently with.equals()
it would return false and would invalidate the outcome, and with.isEmpty
it would raise aNullPointerException
. I will update my answer, considering this case.
– Brad
Nov 22 '18 at 16:13
@sander, out of curiosity, did it solve your issue?
– Brad
Jan 13 at 10:05
add a comment |
Just for the sake of using Streams and anyMatch you can use this operation to check if any of the properties are blank. This is just to answer your question specifically, my suggestion is to take some time and improve this as there is a lot of room.
listOfObject.stream()
.anyMatch(obj -> Arrays.stream(obj)
.anyMatch(p -> !(obj.get(p)!= null && !obj.get(p).isEmpty())));
1
I think this is the answer I was looking for, I will test it later and accept if it does work.
– sander
Nov 22 '18 at 14:52
Instead of.equals("")
, you can use.isEmpty()
.
– Holger
Nov 22 '18 at 15:37
Good point, made me think about the case where the value retrieved by the get method is null. Currently with.equals()
it would return false and would invalidate the outcome, and with.isEmpty
it would raise aNullPointerException
. I will update my answer, considering this case.
– Brad
Nov 22 '18 at 16:13
@sander, out of curiosity, did it solve your issue?
– Brad
Jan 13 at 10:05
add a comment |
Just for the sake of using Streams and anyMatch you can use this operation to check if any of the properties are blank. This is just to answer your question specifically, my suggestion is to take some time and improve this as there is a lot of room.
listOfObject.stream()
.anyMatch(obj -> Arrays.stream(obj)
.anyMatch(p -> !(obj.get(p)!= null && !obj.get(p).isEmpty())));
Just for the sake of using Streams and anyMatch you can use this operation to check if any of the properties are blank. This is just to answer your question specifically, my suggestion is to take some time and improve this as there is a lot of room.
listOfObject.stream()
.anyMatch(obj -> Arrays.stream(obj)
.anyMatch(p -> !(obj.get(p)!= null && !obj.get(p).isEmpty())));
edited Nov 22 '18 at 16:18
answered Nov 22 '18 at 14:44


BradBrad
1749
1749
1
I think this is the answer I was looking for, I will test it later and accept if it does work.
– sander
Nov 22 '18 at 14:52
Instead of.equals("")
, you can use.isEmpty()
.
– Holger
Nov 22 '18 at 15:37
Good point, made me think about the case where the value retrieved by the get method is null. Currently with.equals()
it would return false and would invalidate the outcome, and with.isEmpty
it would raise aNullPointerException
. I will update my answer, considering this case.
– Brad
Nov 22 '18 at 16:13
@sander, out of curiosity, did it solve your issue?
– Brad
Jan 13 at 10:05
add a comment |
1
I think this is the answer I was looking for, I will test it later and accept if it does work.
– sander
Nov 22 '18 at 14:52
Instead of.equals("")
, you can use.isEmpty()
.
– Holger
Nov 22 '18 at 15:37
Good point, made me think about the case where the value retrieved by the get method is null. Currently with.equals()
it would return false and would invalidate the outcome, and with.isEmpty
it would raise aNullPointerException
. I will update my answer, considering this case.
– Brad
Nov 22 '18 at 16:13
@sander, out of curiosity, did it solve your issue?
– Brad
Jan 13 at 10:05
1
1
I think this is the answer I was looking for, I will test it later and accept if it does work.
– sander
Nov 22 '18 at 14:52
I think this is the answer I was looking for, I will test it later and accept if it does work.
– sander
Nov 22 '18 at 14:52
Instead of
.equals("")
, you can use .isEmpty()
.– Holger
Nov 22 '18 at 15:37
Instead of
.equals("")
, you can use .isEmpty()
.– Holger
Nov 22 '18 at 15:37
Good point, made me think about the case where the value retrieved by the get method is null. Currently with
.equals()
it would return false and would invalidate the outcome, and with .isEmpty
it would raise a NullPointerException
. I will update my answer, considering this case.– Brad
Nov 22 '18 at 16:13
Good point, made me think about the case where the value retrieved by the get method is null. Currently with
.equals()
it would return false and would invalidate the outcome, and with .isEmpty
it would raise a NullPointerException
. I will update my answer, considering this case.– Brad
Nov 22 '18 at 16:13
@sander, out of curiosity, did it solve your issue?
– Brad
Jan 13 at 10:05
@sander, out of curiosity, did it solve your issue?
– Brad
Jan 13 at 10:05
add a comment |
You can just use a list of Predicate
s. Each of the following predicates tests one of the properties for equality against ""
.
List<Predicate<CustomObject>> propertiesCheckers =
Arrays.asList(pdo -> "".equals(pdo.get("prop1")),
pdo -> "".equals(pdo.get("prop2")),
pdo -> "".equals(pdo.get("prop3")));
And this stream checks each element against the predicate, returning true
when the first empty value is found:
boolean anyEmpty = listOfObjects.stream()
.flatMap(ob -> propertiesCheckers.stream().map(pred -> pred.test(ob)))
.anyMatch(b -> b); //returns true if any "".equals returned true
if(anyEmpty) System.out.println("Some of the required fields are empty");
1. Though honestly, I am not very sure if I understand the question right. But, does this not miss the relation between theString properties
in the question to theList<CustomObejct> listOfObjects
somewhere? 2. There is also a typo in your checkers left withemp
.
– nullpointer
Nov 22 '18 at 14:21
1
@nullpointer Thanks, fixed typo. Edited to use the added example in the question. So I'm proposing the OP changeString properties
toList<Predicate<CustomObject>>
based on those property names.
– ernest_k
Nov 22 '18 at 14:37
add a comment |
You can just use a list of Predicate
s. Each of the following predicates tests one of the properties for equality against ""
.
List<Predicate<CustomObject>> propertiesCheckers =
Arrays.asList(pdo -> "".equals(pdo.get("prop1")),
pdo -> "".equals(pdo.get("prop2")),
pdo -> "".equals(pdo.get("prop3")));
And this stream checks each element against the predicate, returning true
when the first empty value is found:
boolean anyEmpty = listOfObjects.stream()
.flatMap(ob -> propertiesCheckers.stream().map(pred -> pred.test(ob)))
.anyMatch(b -> b); //returns true if any "".equals returned true
if(anyEmpty) System.out.println("Some of the required fields are empty");
1. Though honestly, I am not very sure if I understand the question right. But, does this not miss the relation between theString properties
in the question to theList<CustomObejct> listOfObjects
somewhere? 2. There is also a typo in your checkers left withemp
.
– nullpointer
Nov 22 '18 at 14:21
1
@nullpointer Thanks, fixed typo. Edited to use the added example in the question. So I'm proposing the OP changeString properties
toList<Predicate<CustomObject>>
based on those property names.
– ernest_k
Nov 22 '18 at 14:37
add a comment |
You can just use a list of Predicate
s. Each of the following predicates tests one of the properties for equality against ""
.
List<Predicate<CustomObject>> propertiesCheckers =
Arrays.asList(pdo -> "".equals(pdo.get("prop1")),
pdo -> "".equals(pdo.get("prop2")),
pdo -> "".equals(pdo.get("prop3")));
And this stream checks each element against the predicate, returning true
when the first empty value is found:
boolean anyEmpty = listOfObjects.stream()
.flatMap(ob -> propertiesCheckers.stream().map(pred -> pred.test(ob)))
.anyMatch(b -> b); //returns true if any "".equals returned true
if(anyEmpty) System.out.println("Some of the required fields are empty");
You can just use a list of Predicate
s. Each of the following predicates tests one of the properties for equality against ""
.
List<Predicate<CustomObject>> propertiesCheckers =
Arrays.asList(pdo -> "".equals(pdo.get("prop1")),
pdo -> "".equals(pdo.get("prop2")),
pdo -> "".equals(pdo.get("prop3")));
And this stream checks each element against the predicate, returning true
when the first empty value is found:
boolean anyEmpty = listOfObjects.stream()
.flatMap(ob -> propertiesCheckers.stream().map(pred -> pred.test(ob)))
.anyMatch(b -> b); //returns true if any "".equals returned true
if(anyEmpty) System.out.println("Some of the required fields are empty");
edited Nov 22 '18 at 14:34
answered Nov 22 '18 at 14:03
ernest_kernest_k
23.4k42649
23.4k42649
1. Though honestly, I am not very sure if I understand the question right. But, does this not miss the relation between theString properties
in the question to theList<CustomObejct> listOfObjects
somewhere? 2. There is also a typo in your checkers left withemp
.
– nullpointer
Nov 22 '18 at 14:21
1
@nullpointer Thanks, fixed typo. Edited to use the added example in the question. So I'm proposing the OP changeString properties
toList<Predicate<CustomObject>>
based on those property names.
– ernest_k
Nov 22 '18 at 14:37
add a comment |
1. Though honestly, I am not very sure if I understand the question right. But, does this not miss the relation between theString properties
in the question to theList<CustomObejct> listOfObjects
somewhere? 2. There is also a typo in your checkers left withemp
.
– nullpointer
Nov 22 '18 at 14:21
1
@nullpointer Thanks, fixed typo. Edited to use the added example in the question. So I'm proposing the OP changeString properties
toList<Predicate<CustomObject>>
based on those property names.
– ernest_k
Nov 22 '18 at 14:37
1. Though honestly, I am not very sure if I understand the question right. But, does this not miss the relation between the
String properties
in the question to the List<CustomObejct> listOfObjects
somewhere? 2. There is also a typo in your checkers left with emp
.– nullpointer
Nov 22 '18 at 14:21
1. Though honestly, I am not very sure if I understand the question right. But, does this not miss the relation between the
String properties
in the question to the List<CustomObejct> listOfObjects
somewhere? 2. There is also a typo in your checkers left with emp
.– nullpointer
Nov 22 '18 at 14:21
1
1
@nullpointer Thanks, fixed typo. Edited to use the added example in the question. So I'm proposing the OP change
String properties
to List<Predicate<CustomObject>>
based on those property names.– ernest_k
Nov 22 '18 at 14:37
@nullpointer Thanks, fixed typo. Edited to use the added example in the question. So I'm proposing the OP change
String properties
to List<Predicate<CustomObject>>
based on those property names.– ernest_k
Nov 22 '18 at 14:37
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%2f53432477%2fcomparing-a-string-to-a-list-of-objects-with-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
Not really. Just a check whether any of the properties is empty.
– sander
Nov 22 '18 at 13:53
1
1. Why a
List<Object>
, use some custom object. 2.get
would depend on the type of object . 3 useequals
to compare strings.– nullpointer
Nov 22 '18 at 13:55
1
There is no simple builtin facility to treat a
String
like an object’s property. This is not Javascript.– Holger
Nov 22 '18 at 13:59
1
what does .get(String prop) return if the property is not found?
– Brad
Nov 22 '18 at 14:08
1
Please share the
CustomObject
class definition. I think that's the most crucial part that you've abstracted from the question && 3. useequals
to compare strings.– nullpointer
Nov 22 '18 at 14:23