Try using the improved code to find the key in the HashMap list.
public static boolean searchInMap(String keyToSearch) { boolean returnVal = false; List<Map<String, String>> listOfMaps = new ArrayList<Map<String, String>>(); Map<String, String> map1 = new HashMap<String, String>(); map1.put("key1", "value1"); Map<String, String> map2 = new HashMap<String, String>(); map1.put("key2", "value2"); Map<String, String> map3 = new HashMap<String, String>(); map1.put("key3", "value3"); listOfMaps.add(map1); listOfMaps.add(map2); listOfMaps.add(map3); for (Map<String, String> map : listOfMaps) { if(map.containsKey(keyToSearch)) { returnVal =true; break; } } return returnVal; }
source share