Search HashMap in ArrayList from HashMap

I have an ArrayList from HashMap. I want to search for a HashMap in it, but I cannot find a way to achieve this. Please suggest me how can this be done? Thanks.

+6
source share
6 answers

Answer your question as I understand it!

for (HashMap<String, String> hashMap : yourArrayList) { // For each hashmap, iterate over it for (Map.Entry<String, String> entry : hashMap.entrySet()) { // Do something with your entrySet, for example get the key. String sListName = entry.getKey(); } } 

Your Hashmap may use other types, this uses strings.

+5
source

See if this helps:

 @Test public void searchMap() { 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); String keyToSearch = "key2"; for (Map<String, String> map : listOfMaps) { for (String key : map.keySet()) { if (keyToSearch.equals(key)) { System.out.println("Found : " + key + " / value : " + map.get(key)); } } } } 

Hurrah!

+3
source
 Object myObj; Object myKey; //Traverse the list for(HashMap curMap : listOfMaps){ //If this map has the object, that is the key doesn't return a null object if( (myObj = curMap.get(myKey)) != null) { //Stop traversing because we are done break; } } //Act on the object if(myObj != null) { //TODO: Do your logic here } 

If you want to get a link to a map instead of an object (for any reason), the same process applies, except that you just save the link to the map:

 Map myMap; Object myKey; //Traverse the list for(HashMap curMap : listOfMaps){ //If this map has the object, that is the key doesn't return a null object if(curMap.get(myKey) != null) { //Store instance to the map myMap = curMap; //Stop traversing because we are done break; } } //Act on the map if(myMap != null) { //TODO: Do your logic here } 
+2
source

The efficient way that I used to search for a hash map in an arraylist without using loops. Because a loop increases execution time

 try{ int index = list.indexOf(map); // map is your map to find in ArrayList if(index>=0){ HashMap<String, String> map = array_list.get(index); // Here you can get your values } } catch(Exception e){ e.printStackTrace(); Log.i("HashMap","Not Found"); } 
+1
source

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; } 
+1
source

if you have an ArrayList like this: ArrayList <HashMap <String, String β†’ and you want to compare one of the values ​​inside the HashMap, try this code.

I use it to compare alarm notification settings.

 for (HashMap<String, String> map : AlarmList) { for (String key : map.keySet()) { if (key.equals("SendungsID")) { if(map.get(key).equals(alarmMap.get("AlarmID"))) { //found this value in ArrayList } } } } 
0
source

Source: https://habr.com/ru/post/920159/


All Articles