'How to iterate over a list of Map of Strings and add to another list if map contains matching elements on a key value?

I have a List of Map<String, String> that I want to iterate over and find the common elements inside the map of string and add to another map.

I am confused what should go inside the if loop to get my expected output. I am looking for comparator type call but I couldn't find that anywhere.

      for (int i = 0; i < list.size() - 1; i++) {
       if (list.get(i).get("Journal ID").equals(list.get(i+1).get("Journal ID")))
            // ???
         }
      }

I was using this method to sort list of Maps. I am expecting some thing like this

public Comparator<Map<String, String>> mapComparator = new Comparator<>() {
public int compare(Map<String, String> m1, Map<String, String> m2) {
    return m1.get("Journal ID").compareTo(m2.get("Journal ID"));
    }
       }

     Collections.sort(list, mapComparator);

// input and the expected output my List = [{Journal ID=123, featureID=312},{Journal ID=123, featureID=313},{Journal ID=134, featureID=314},{Journal ID=123, featureID=1255}] expected output is one that matching the "Journal ID" [{Journal ID=123, featureID=312}, { Journal ID=123, featureID=313},{Journal ID=123, featureID=1255}].



Solution 1:[1]

One approach is to construct a second map which will aggregate all maps. It will reflect all keys form all maps. As value will be a list of each key value with counters. Implementation can be enhanced also, but the main aspect is how to proceed. Having the aggregate map, then is straight forward to transform in what ever structure needed.

public class TestEqMap {

    public static void main(String[] args) 
    {
        Map<String, String> m1 = Map.of("a","a1","b","b1");
        Map<String, String> m2 = Map.of("a","a1","b","b2");
        Map<String, String> m3 = Map.of("a","a2","b","b2");
        Map<String, String> m4 = Map.of("a","a1","b","b2");
        Map<String, String> m5 = Map.of("a","a3","b","b2");
        AggMap amap = new AggMap();
        amap.addMap(m1);
        amap.addMap(m2);
        amap.addMap(m3);
        amap.addMap(m4);
        amap.addMap(m5);
    
        amap.map.forEach((k,v)->System.out.println("key="+k+"\n"+v));
    }
    static class AggMap
    {
        public Map<String, ListItem> map = new HashMap<String,ListItem>();
        public void addMap(Map<String,String> m)
        {
            for(String key: m.keySet())
            {
                if(this.map.containsKey(key))
                {
                    this.map.get(key).addItem(m.get(key));
                }
                else
                {
                    ListItem li = new ListItem();
                    li.addItem(m.get(key));
                    this.map.put(key, li);
                }
            }
        }
        
    }
    static class ListItem
    {
        public List<Item> li = new ArrayList<Item>();
        public ListItem() {};
        public void addItem(String str)
        {
            for(Item i: this.li)
            {
                if(i.val.equals(str))
                {
                    i.count++;
                    return;
                }   
            }
            this.li.add(new Item(str));
            
        }
        public String toString()
        {
            StringBuffer sb= new StringBuffer();
            this.li.forEach(i->sb.append(i+"\n"));
            return sb.toString();
        }
    }
    static class Item
    {
        public String val;
        public int count=1;
        
        public Item(String val)
        {
            this.val = val;
        }
        
        public String toString()
        {
            return "val="+this.val+" count="+this.count;
        }
    }
}

Output:

key=a
val=a1 count=3
val=a2 count=1
val=a3 count=1

key=b
val=b1 count=1
val=b2 count=4

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Traian GEICU