Java | 2 liner | Explained
Idea: Nothing clever to do here, just iterate and count\n> T/S: O(n)/O(1), where n = size(items)\n\nprivate static final Map<String, Integer> RULE_TO_INDEX = Ma
**Idea:** Nothing clever to do here, just iterate and count\n> **T/S**: O(n)/O(1), where n = size(items)\n```\nprivate static final Map<String, Integer> RULE_TO_INDEX = Map.of("type", 0,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t "color", 1,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t "name", 2);\n\npublic int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {\n\tvar ruleIndex = RULE_TO_INDEX.get(ruleKey); // get the rule\n\tvar count = 0;\n\t\n\tfor (var item : items) // iterate\n\t\tif (item.get(ruleIndex).equals(ruleValue)) // check\n\t\t\tcount++; // count\n\treturn count;\n}\n```\nVersion 2: 2 liner using streams\n```\npublic int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {\n\treturn (int) items.stream()\n\t\t\t\t\t  .filter(item -> item.get(RULE_TO_INDEX.get(ruleKey))\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t.equals(ruleValue))\n\t\t\t\t\t  .count();\n}\n```\n***Please upvote if this helps***
['Java']