1

I was going through How to remove a key from HashMap while iterating over it?, but my requirement is bit different.

class Main {
    public static void main(String[] args) {
        Map<String, String> hashMap = new HashMap<>();

        hashMap.put("RED", "#FF0000");
        hashMap.put("BLACK", null);
        hashMap.put("BLUE", "#0000FF");
        hashMap.put("GREEN", "#008000");
        hashMap.put("WHITE", null);

        // I wan't result like below - get All keys whose value is null
        List<String> collect = hashMap.values()
                .stream()
                .filter(e -> e == null)
                .collect(Collectors.toList());
        System.out.println(collect);
        
        // Result - BLACK, WHITE in list
    }
}
PAA
  • 1
  • 46
  • 174
  • 282

1 Answers1

0

Try this:

import java.util.*;
import java.util.stream.*;
class Main {
    public static void main(String[] args) {
        Map<String, String> hashMap = new HashMap<>();

        hashMap.put("RED", "#FF0000");
        hashMap.put("BLACK", null);
        hashMap.put("BLUE", "#0000FF");
        hashMap.put("GREEN", "#008000");
        hashMap.put("WHITE", null);

        // I wan't result like below - get All keys whose value is null
        List<String> collect = hashMap.keySet()
                .stream()
                .filter(e -> Objects.isNull(hashMap.get(e)))
                .collect(Collectors.toList());
        System.out.println(collect);
        
        // Result - BLACK, WHITE in list
    }
}

As pointed out in the comments, you can try this as well:

import java.util.*;
import java.util.stream.*;
class Main {
    public static void main(String[] args) {
        Map<String, String> hashMap = new HashMap<>();

        hashMap.put("RED", "#FF0000");
        hashMap.put("BLACK", null);
        hashMap.put("BLUE", "#0000FF");
        hashMap.put("GREEN", "#008000");
        hashMap.put("WHITE", null);

        // I wan't result like below - get All keys whose value is null
        List<String> collect = hashMap.entrySet()
                .stream()
                .filter(e -> Objects.isNull(e.getValue()))
                .map(e -> e.getKey())
                .collect(Collectors.toList());
        System.out.println(collect);
        
        // Result - BLACK, WHITE in list
    }
}

This is more optimized, as compared to the first solution.

Charchit Kapoor
  • 8,934
  • 2
  • 8
  • 24
  • 2
    You loop over all keys and still call `get` for all of them. In that case it would probably be better to just iterate over [the `entrySet`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Map.html#entrySet()). – Joachim Sauer Sep 12 '22 at 09:39
  • Yeah, I just did it this way, cause otherwise I had to add additional `.map` to just obtain the keys. – Charchit Kapoor Sep 12 '22 at 09:45
  • And this way you need an additional `get` which has to go through the whole "hashing-and-lookup"-process for each case, as opposed to a map t hat executes a simple `getKey` call. – Joachim Sauer Sep 12 '22 at 09:47
  • Yeah, I will update the answer to contain that as well, but wouldn't the `entrySet`, would also go through the `hashing and lookup` process, to generate it's output? I am sorry if it's a basic question, but it does have some learning for me. – Charchit Kapoor Sep 12 '22 at 09:50
  • 1
    No, it wouldn't. (Look at the OpenJDK source code ....) – Stephen C Sep 12 '22 at 09:50
  • Cool, I have updated the answer and will take a look at the source code as well. – Charchit Kapoor Sep 12 '22 at 09:53