How can I check if there's element in my arraylist that is not in the hashmap?












7















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?










share|improve this question

























  • please provide HashMap object type<?>

    – Ashok Kumar N
    Feb 20 at 13:11
















7















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?










share|improve this question

























  • please provide HashMap object type<?>

    – Ashok Kumar N
    Feb 20 at 13:11














7












7








7


1






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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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












2 Answers
2






active

oldest

votes


















12














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);





share|improve this answer


























  • 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






  • 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






  • 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








  • 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





















7














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






share|improve this answer

























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    12














    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);





    share|improve this answer


























    • 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






    • 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






    • 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








    • 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


















    12














    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);





    share|improve this answer


























    • 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






    • 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






    • 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








    • 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
















    12












    12








    12







    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);





    share|improve this answer















    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);






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Feb 20 at 18:28

























    answered Feb 20 at 13:12









    nullpointernullpointer

    43.2k10101200




    43.2k10101200













    • 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






    • 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






    • 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








    • 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





















    • 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






    • 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






    • 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








    • 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



















    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















    7














    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






    share|improve this answer






























      7














      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






      share|improve this answer




























        7












        7








        7







        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






        share|improve this answer















        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







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 20 at 13:30

























        answered Feb 20 at 13:12









        LinoLino

        9,65722041




        9,65722041






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents