How can I check if there's element in my arraylist that is not in the hashmap?
I have a List<String>
in java
and it contains some Strings.
I also have a hashmap
with String
values and I would like to check if there's any element in my List that is not in the Hashmap. This is the code I wrote:
List<String> someStrings = fetchData();
if (someStrings.stream().noneMatch(s -> myHashMap.containsValue(s))) {
return false;
}
return true;
But it does not work properly. Can you help me with that?
java collections java-8 java-stream
add a comment |
I have a List<String>
in java
and it contains some Strings.
I also have a hashmap
with String
values and I would like to check if there's any element in my List that is not in the Hashmap. This is the code I wrote:
List<String> someStrings = fetchData();
if (someStrings.stream().noneMatch(s -> myHashMap.containsValue(s))) {
return false;
}
return true;
But it does not work properly. Can you help me with that?
java collections java-8 java-stream
please provide HashMap object type<?>
– Ashok Kumar N
Feb 20 at 13:11
add a comment |
I have a List<String>
in java
and it contains some Strings.
I also have a hashmap
with String
values and I would like to check if there's any element in my List that is not in the Hashmap. This is the code I wrote:
List<String> someStrings = fetchData();
if (someStrings.stream().noneMatch(s -> myHashMap.containsValue(s))) {
return false;
}
return true;
But it does not work properly. Can you help me with that?
java collections java-8 java-stream
I have a List<String>
in java
and it contains some Strings.
I also have a hashmap
with String
values and I would like to check if there's any element in my List that is not in the Hashmap. This is the code I wrote:
List<String> someStrings = fetchData();
if (someStrings.stream().noneMatch(s -> myHashMap.containsValue(s))) {
return false;
}
return true;
But it does not work properly. Can you help me with that?
java collections java-8 java-stream
java collections java-8 java-stream
edited Feb 21 at 5:49
Stuart Marks
82.4k26137208
82.4k26137208
asked Feb 20 at 13:09
randomuser1randomuser1
1,06231539
1,06231539
please provide HashMap object type<?>
– Ashok Kumar N
Feb 20 at 13:11
add a comment |
please provide HashMap object type<?>
– Ashok Kumar N
Feb 20 at 13:11
please provide HashMap object type<?>
– Ashok Kumar N
Feb 20 at 13:11
please provide HashMap object type<?>
– Ashok Kumar N
Feb 20 at 13:11
add a comment |
2 Answers
2
active
oldest
votes
Given that your condition is
if there's any element in my List that is not in the Hashmap
you can use anyMatch
while iterating over the list of elements to check if any of them is not present in the hashmap values.
return someStrings.stream().anyMatch(val -> !myHashMap.containsValue(val))
Or to look at it as if all elements of someStrings
are present in hashmap values
return someStrings.stream().allMatch(myHashMap::containsValue);
A similar check though could also be using containsAll
over the Collection
of values :
return myHashMap.values().containsAll(someStrings);
thank you, I like thecontainsAll
approach, I just noticed that it doesn't work when there's one empty string added tosomeStrings
(containsAll
seems to return true in that case, even though I don't have an empty value in my hashmap). Do you know how to prevent it?
– randomuser1
Feb 20 at 13:29
@randomuser1 Well if you meanvar someStrings = List.of("", "alpha","beta")
andvar mapValues = List.of("alpha","beta")
, then it would certainly returnfalse
. I can confirm testing it.
– nullpointer
Feb 20 at 13:33
1
Keep in mind thatsomeStrings.stream().anyMatch(val -> !myHashMap.containsValue(val))
is the opposite ofmyHashMap.values().containsAll(someStrings)
…
– Holger
Feb 20 at 13:50
1
You can read your code literally,anyMatch(val -> !myHashMap.containsValue(val))
checks if any element is not contained in the values. Which is the opposite of whether all are contained. Which is, of course, a viable test using the process of elimination. It’s just delivering the opposite value, so a!
before the entire expression would produce the same value. Or just usingallMatch(val -> myHashMap.containsValue(val))
, which now, without the negation, can be written asallMatch(myHashMap::containsValue)
orallMatch(myHashMap.values()::contains)
…
– Holger
Feb 20 at 17:53
2
Well yes, these are two sides of the same test and you could use either result accordingly, but since the OP seemed to be confused about the result regardingList.of("")
, I just wanted to leave a reminder about the opposite values, to ensure that the OP did not overlook that small detail when testing.
– Holger
Feb 20 at 18:41
|
show 6 more comments
No need for streams, you can just use the good old Collection#removeAll()
:
Set<String> copy = new HashSet<>(someStrings);
copy.removeAll(myHashMap.values());
Now copy
will contain all the values not contained in myHashMap
. You can then do something with them (like iterating) or just call Collection#isEmpty()
to check if all are contained in the map
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%2f54787135%2fhow-can-i-check-if-theres-element-in-my-arraylist-that-is-not-in-the-hashmap%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
Given that your condition is
if there's any element in my List that is not in the Hashmap
you can use anyMatch
while iterating over the list of elements to check if any of them is not present in the hashmap values.
return someStrings.stream().anyMatch(val -> !myHashMap.containsValue(val))
Or to look at it as if all elements of someStrings
are present in hashmap values
return someStrings.stream().allMatch(myHashMap::containsValue);
A similar check though could also be using containsAll
over the Collection
of values :
return myHashMap.values().containsAll(someStrings);
thank you, I like thecontainsAll
approach, I just noticed that it doesn't work when there's one empty string added tosomeStrings
(containsAll
seems to return true in that case, even though I don't have an empty value in my hashmap). Do you know how to prevent it?
– randomuser1
Feb 20 at 13:29
@randomuser1 Well if you meanvar someStrings = List.of("", "alpha","beta")
andvar mapValues = List.of("alpha","beta")
, then it would certainly returnfalse
. I can confirm testing it.
– nullpointer
Feb 20 at 13:33
1
Keep in mind thatsomeStrings.stream().anyMatch(val -> !myHashMap.containsValue(val))
is the opposite ofmyHashMap.values().containsAll(someStrings)
…
– Holger
Feb 20 at 13:50
1
You can read your code literally,anyMatch(val -> !myHashMap.containsValue(val))
checks if any element is not contained in the values. Which is the opposite of whether all are contained. Which is, of course, a viable test using the process of elimination. It’s just delivering the opposite value, so a!
before the entire expression would produce the same value. Or just usingallMatch(val -> myHashMap.containsValue(val))
, which now, without the negation, can be written asallMatch(myHashMap::containsValue)
orallMatch(myHashMap.values()::contains)
…
– Holger
Feb 20 at 17:53
2
Well yes, these are two sides of the same test and you could use either result accordingly, but since the OP seemed to be confused about the result regardingList.of("")
, I just wanted to leave a reminder about the opposite values, to ensure that the OP did not overlook that small detail when testing.
– Holger
Feb 20 at 18:41
|
show 6 more comments
Given that your condition is
if there's any element in my List that is not in the Hashmap
you can use anyMatch
while iterating over the list of elements to check if any of them is not present in the hashmap values.
return someStrings.stream().anyMatch(val -> !myHashMap.containsValue(val))
Or to look at it as if all elements of someStrings
are present in hashmap values
return someStrings.stream().allMatch(myHashMap::containsValue);
A similar check though could also be using containsAll
over the Collection
of values :
return myHashMap.values().containsAll(someStrings);
thank you, I like thecontainsAll
approach, I just noticed that it doesn't work when there's one empty string added tosomeStrings
(containsAll
seems to return true in that case, even though I don't have an empty value in my hashmap). Do you know how to prevent it?
– randomuser1
Feb 20 at 13:29
@randomuser1 Well if you meanvar someStrings = List.of("", "alpha","beta")
andvar mapValues = List.of("alpha","beta")
, then it would certainly returnfalse
. I can confirm testing it.
– nullpointer
Feb 20 at 13:33
1
Keep in mind thatsomeStrings.stream().anyMatch(val -> !myHashMap.containsValue(val))
is the opposite ofmyHashMap.values().containsAll(someStrings)
…
– Holger
Feb 20 at 13:50
1
You can read your code literally,anyMatch(val -> !myHashMap.containsValue(val))
checks if any element is not contained in the values. Which is the opposite of whether all are contained. Which is, of course, a viable test using the process of elimination. It’s just delivering the opposite value, so a!
before the entire expression would produce the same value. Or just usingallMatch(val -> myHashMap.containsValue(val))
, which now, without the negation, can be written asallMatch(myHashMap::containsValue)
orallMatch(myHashMap.values()::contains)
…
– Holger
Feb 20 at 17:53
2
Well yes, these are two sides of the same test and you could use either result accordingly, but since the OP seemed to be confused about the result regardingList.of("")
, I just wanted to leave a reminder about the opposite values, to ensure that the OP did not overlook that small detail when testing.
– Holger
Feb 20 at 18:41
|
show 6 more comments
Given that your condition is
if there's any element in my List that is not in the Hashmap
you can use anyMatch
while iterating over the list of elements to check if any of them is not present in the hashmap values.
return someStrings.stream().anyMatch(val -> !myHashMap.containsValue(val))
Or to look at it as if all elements of someStrings
are present in hashmap values
return someStrings.stream().allMatch(myHashMap::containsValue);
A similar check though could also be using containsAll
over the Collection
of values :
return myHashMap.values().containsAll(someStrings);
Given that your condition is
if there's any element in my List that is not in the Hashmap
you can use anyMatch
while iterating over the list of elements to check if any of them is not present in the hashmap values.
return someStrings.stream().anyMatch(val -> !myHashMap.containsValue(val))
Or to look at it as if all elements of someStrings
are present in hashmap values
return someStrings.stream().allMatch(myHashMap::containsValue);
A similar check though could also be using containsAll
over the Collection
of values :
return myHashMap.values().containsAll(someStrings);
edited Feb 20 at 18:28
answered Feb 20 at 13:12
nullpointernullpointer
43.2k10101200
43.2k10101200
thank you, I like thecontainsAll
approach, I just noticed that it doesn't work when there's one empty string added tosomeStrings
(containsAll
seems to return true in that case, even though I don't have an empty value in my hashmap). Do you know how to prevent it?
– randomuser1
Feb 20 at 13:29
@randomuser1 Well if you meanvar someStrings = List.of("", "alpha","beta")
andvar mapValues = List.of("alpha","beta")
, then it would certainly returnfalse
. I can confirm testing it.
– nullpointer
Feb 20 at 13:33
1
Keep in mind thatsomeStrings.stream().anyMatch(val -> !myHashMap.containsValue(val))
is the opposite ofmyHashMap.values().containsAll(someStrings)
…
– Holger
Feb 20 at 13:50
1
You can read your code literally,anyMatch(val -> !myHashMap.containsValue(val))
checks if any element is not contained in the values. Which is the opposite of whether all are contained. Which is, of course, a viable test using the process of elimination. It’s just delivering the opposite value, so a!
before the entire expression would produce the same value. Or just usingallMatch(val -> myHashMap.containsValue(val))
, which now, without the negation, can be written asallMatch(myHashMap::containsValue)
orallMatch(myHashMap.values()::contains)
…
– Holger
Feb 20 at 17:53
2
Well yes, these are two sides of the same test and you could use either result accordingly, but since the OP seemed to be confused about the result regardingList.of("")
, I just wanted to leave a reminder about the opposite values, to ensure that the OP did not overlook that small detail when testing.
– Holger
Feb 20 at 18:41
|
show 6 more comments
thank you, I like thecontainsAll
approach, I just noticed that it doesn't work when there's one empty string added tosomeStrings
(containsAll
seems to return true in that case, even though I don't have an empty value in my hashmap). Do you know how to prevent it?
– randomuser1
Feb 20 at 13:29
@randomuser1 Well if you meanvar someStrings = List.of("", "alpha","beta")
andvar mapValues = List.of("alpha","beta")
, then it would certainly returnfalse
. I can confirm testing it.
– nullpointer
Feb 20 at 13:33
1
Keep in mind thatsomeStrings.stream().anyMatch(val -> !myHashMap.containsValue(val))
is the opposite ofmyHashMap.values().containsAll(someStrings)
…
– Holger
Feb 20 at 13:50
1
You can read your code literally,anyMatch(val -> !myHashMap.containsValue(val))
checks if any element is not contained in the values. Which is the opposite of whether all are contained. Which is, of course, a viable test using the process of elimination. It’s just delivering the opposite value, so a!
before the entire expression would produce the same value. Or just usingallMatch(val -> myHashMap.containsValue(val))
, which now, without the negation, can be written asallMatch(myHashMap::containsValue)
orallMatch(myHashMap.values()::contains)
…
– Holger
Feb 20 at 17:53
2
Well yes, these are two sides of the same test and you could use either result accordingly, but since the OP seemed to be confused about the result regardingList.of("")
, I just wanted to leave a reminder about the opposite values, to ensure that the OP did not overlook that small detail when testing.
– Holger
Feb 20 at 18:41
thank you, I like the
containsAll
approach, I just noticed that it doesn't work when there's one empty string added to someStrings
(containsAll
seems to return true in that case, even though I don't have an empty value in my hashmap). Do you know how to prevent it?– randomuser1
Feb 20 at 13:29
thank you, I like the
containsAll
approach, I just noticed that it doesn't work when there's one empty string added to someStrings
(containsAll
seems to return true in that case, even though I don't have an empty value in my hashmap). Do you know how to prevent it?– randomuser1
Feb 20 at 13:29
@randomuser1 Well if you mean
var someStrings = List.of("", "alpha","beta")
and var mapValues = List.of("alpha","beta")
, then it would certainly return false
. I can confirm testing it.– nullpointer
Feb 20 at 13:33
@randomuser1 Well if you mean
var someStrings = List.of("", "alpha","beta")
and var mapValues = List.of("alpha","beta")
, then it would certainly return false
. I can confirm testing it.– nullpointer
Feb 20 at 13:33
1
1
Keep in mind that
someStrings.stream().anyMatch(val -> !myHashMap.containsValue(val))
is the opposite of myHashMap.values().containsAll(someStrings)
…– Holger
Feb 20 at 13:50
Keep in mind that
someStrings.stream().anyMatch(val -> !myHashMap.containsValue(val))
is the opposite of myHashMap.values().containsAll(someStrings)
…– Holger
Feb 20 at 13:50
1
1
You can read your code literally,
anyMatch(val -> !myHashMap.containsValue(val))
checks if any element is not contained in the values. Which is the opposite of whether all are contained. Which is, of course, a viable test using the process of elimination. It’s just delivering the opposite value, so a !
before the entire expression would produce the same value. Or just using allMatch(val -> myHashMap.containsValue(val))
, which now, without the negation, can be written as allMatch(myHashMap::containsValue)
or allMatch(myHashMap.values()::contains)
…– Holger
Feb 20 at 17:53
You can read your code literally,
anyMatch(val -> !myHashMap.containsValue(val))
checks if any element is not contained in the values. Which is the opposite of whether all are contained. Which is, of course, a viable test using the process of elimination. It’s just delivering the opposite value, so a !
before the entire expression would produce the same value. Or just using allMatch(val -> myHashMap.containsValue(val))
, which now, without the negation, can be written as allMatch(myHashMap::containsValue)
or allMatch(myHashMap.values()::contains)
…– Holger
Feb 20 at 17:53
2
2
Well yes, these are two sides of the same test and you could use either result accordingly, but since the OP seemed to be confused about the result regarding
List.of("")
, I just wanted to leave a reminder about the opposite values, to ensure that the OP did not overlook that small detail when testing.– Holger
Feb 20 at 18:41
Well yes, these are two sides of the same test and you could use either result accordingly, but since the OP seemed to be confused about the result regarding
List.of("")
, I just wanted to leave a reminder about the opposite values, to ensure that the OP did not overlook that small detail when testing.– Holger
Feb 20 at 18:41
|
show 6 more comments
No need for streams, you can just use the good old Collection#removeAll()
:
Set<String> copy = new HashSet<>(someStrings);
copy.removeAll(myHashMap.values());
Now copy
will contain all the values not contained in myHashMap
. You can then do something with them (like iterating) or just call Collection#isEmpty()
to check if all are contained in the map
add a comment |
No need for streams, you can just use the good old Collection#removeAll()
:
Set<String> copy = new HashSet<>(someStrings);
copy.removeAll(myHashMap.values());
Now copy
will contain all the values not contained in myHashMap
. You can then do something with them (like iterating) or just call Collection#isEmpty()
to check if all are contained in the map
add a comment |
No need for streams, you can just use the good old Collection#removeAll()
:
Set<String> copy = new HashSet<>(someStrings);
copy.removeAll(myHashMap.values());
Now copy
will contain all the values not contained in myHashMap
. You can then do something with them (like iterating) or just call Collection#isEmpty()
to check if all are contained in the map
No need for streams, you can just use the good old Collection#removeAll()
:
Set<String> copy = new HashSet<>(someStrings);
copy.removeAll(myHashMap.values());
Now copy
will contain all the values not contained in myHashMap
. You can then do something with them (like iterating) or just call Collection#isEmpty()
to check if all are contained in the map
edited Feb 20 at 13:30
answered Feb 20 at 13:12
LinoLino
9,65722041
9,65722041
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%2f54787135%2fhow-can-i-check-if-theres-element-in-my-arraylist-that-is-not-in-the-hashmap%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
please provide HashMap object type<?>
– Ashok Kumar N
Feb 20 at 13:11