Home > OS >  Checking Map for two condtions using filter
Checking Map for two condtions using filter

Time:09-15

I have a map with collegeID and Student Id

I am checking if the Map has both the keys and values associated with them

This is always giving false

This is my code

Map<String, String> allDataMap = new HashMap<>();
allDataMap.put("college_id", "1095");
allDataMap.put("student_id", "108");

boolean present = allDataMap.entrySet().stream()
    .filter(map -> map.getKey().equals("college_id") && map.getValue().equals("1095"))
    .filter(map -> map.getKey().equals("student_id") && map.getValue().equals("108"))
    .findAny().isPresent() ? true : false;

System.out.println(present);

CodePudding user response:

The two filters are mutual exclusive, so no element of your Stream can pass both of them. Therefore you get false.

You can use a single filter:

boolean present =  allDataMap.entrySet().stream()
    .filter(map-> (map.getKey().equals("college_id") && map.getValue().equals("1095")) || 
                  (map.getKey().equals("student_id") && map.getValue().equals("108")))
    .count() == 2;

Note that this relies on the source of the Stream being a Map, which means no key can appear twice, so if the Stream has exactly two elements after applying the filter, this means both key-value pairs appear in the source Map.

In general Streams this won't necessarily return the correct result, since the same key-value pair might appear twice.

CodePudding user response:

stream.filter returns a stream with that only includes the items that evaluate to true in the condition. This means when you're filtering after a filter you are essentially saying stream.filter(filterCondition1 && filterCondition2). In your case the condition is always false which is why the filter does not select anything:

allDataMap.entrySet().stream()
        .filter(map -> map.getKey().equals("college_id") && 
                map.getValue().equals("1095") && 
                map.getKey().equals("student_id") && 
                map.getValue().equals("108"))

In your example the first filter map -> map.getKey().equals("college_id") && map.getValue().equals("1095") returns a stream that contains [college_id: 1095]. The second filter will find 0 matches where the key is "student_id" and the value is "108" and will return an empty stream. This is why when you call stream.findAny nothing is found.

CodePudding user response:

You don't need a stream that at all, as @Thomas has pointed out in the comment.

Checking two values in a HashMap is a constant time O(1) action, but iterating over map entries takes O(n). And the latter option requires having the same conditional logic inside the stream. Definitely, employing a stream would buy you nothing in this case.

The following condition would be concise and straightforwards:

boolean present = "1095".equals(allDataMap.get("college_id"))
                && "108".equals(allDataMap.get("student_id"));

Note: if argument passed to the String.equals() is null, method call would return false.

  •  Tags:  
  • java
  • Related