简体   繁体   中英

How to only return true when all conditions in for loop are true?

  • I have a keyStore, that can consist of one or many key value pairs
  • I have a nodeMap that can consist of one or many key value pairs
  • All keys in keyStore must exist in nodeMap and all key value pairs from keyStore must match in nodeMap to return true

For example:

keyStore {key1=>value1}
nodeMap  {key1=>value1, key2=>value2}
return true

----

keyStore {key1=>value1, key2=>value2}
nodeMap  {key1=>value1, key2=>value2}
return true

----

keyStore {key1=>value1, key3=>value3}
nodeMap  {key1=>value1, key2=>value2}
return false;

----

keyStore {key1=>value1, key3=>value3}
nodeMap  {key1=>value1, key2=>value2, key3=>value3}
return true;

Code:

private boolean checkForMatch(NamedNodeMap nodeMap, Map<String, String> keyStore)
   {
      boolean foundMatch = false;
      for (int i = 0; i < nodeMap.getLength(); i++)
      {
         Node node = nodeMap.item(i);
         String nodeName = node.getNodeName();
         if (keyStore.containsKey(nodeName))
         {
            if (keyStore.get(nodeName).contains(node.getNodeValue()))
            {
               foundMatch = true;
            }
            else
            {
               foundMatch = false;
               break;
            }
         }
      }
      return foundMatch;
   }

This will not work for several reasons eg, when nodeMap consists of many key value pairs but keyStore consists of only one key value pair, while first conditions fails then loop breaks, instead of continuing.

The most simple and readable way I can think of consists of converting your NamedNodeMap instance to an actual Map<String, String> and then use standard Set.containsAll method to perform the check you want:

private boolean checkForMatch(NamedNodeMap nodeMap, Map<String, String> keyStore) {

    // First create map from nodeMap
    Map<String, String> nodes = new HashMap<>();
    for (int i = 0; i < nodeMap.getLength(); i++) {
        Node node = nodeMap.item(i);
        nodes.put(node.getNodeName(), node.getNodeValue());
    }

    // Then check the condition
    return nodes.entrySet().containsAll(keyStore.entrySet());
}

You can use streams to do it in a one-liner:

private boolean checkForMatch(NamedNodeMap nodeMap, Map<String, String> keyStore) {
    return IntStream.range(0, nodeMap.getLength())
        .mapToObj(nodeMap::item)
        .collect(Collectors.toMap(Node::getNodeName, Node::getNodeValue))
        .entrySet().containsAll(keyStore.entrySet());
}

Both snippets are functionally equivalent.

Let's try not to over complicate things, you simply need to check if all elements in keyStore exists in nodeMap .

Steps:

  • check if keyStore size is bigger than nodeMap size and return false if so as nodeMap can't hold all keyStore's elements
  • check if any element in keyStore doesn't match nodeMap return false
  • other than that return true

Here is an easy way to do so:

if(keyStore.size() > nodeMap.getLength())
              return false;
          int count = 0;
          for (String key: keyStore.keySet())
          {
             if(keyStore.get(key) != nodeMap.item(count).getNodeValue())
                 return false;
          }
          return true; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM