0

I have two linked hashmap (key - String, value = String[]) which got the same size and the same keys in both linked hashmaps, I want to be able to compare values according to the key, verifying values on one linked hashmap are equals to the same values in the second linked hashmap (by key) or at least the other linked hashmap contains the values.

I am populating both of the linked hashmaps with keys and values and set it to different linked hash maps.

Example for hashmap:

Key - alert - Value (array of strings)

0 - Device_UID,Instance_UID,Configuration_Set_ID,Alert_UID
1 - a4daeccb-0115-430c-b516-ab7edf314d35,0a7938aa-9a01-437f-88ac-4b2927ed7665,96,61b68069-9de7-4b85-83cb-8d9f558e8ecb
2 - a4daeccb-0115-430c-b516-ab7edf314d35,0a7938aa-9a01-437f-88ac-4b2927ed7665,12,92757faa-bf6b-4aa3-ba6d-2e57b44f333c
3 - a4daeccb-0115-430c-b516-ab7edf314d35,0a7938aa-9a01-437f-88ac-4b2927ed7665,369,779b3294-2ca3-4613-a413-bf8d4aa05d16

and it should be at least in the second linked hash- map

String rdsColumns="";
for(String key : mapServer.keySet()){
    String[] value = mapServer.get(key);
    String[] item = value[0].split(",");
    rdsColumns="";
    for(String val:item){
        rdsColumns = rdsColumns.concat(val + ",");
    }
    rdsColumns = rdsColumns.concat(" ");
    rdsColumns = rdsColumns.replace(", ", "");
    info(("Query is: "+ returnSuitableQueryString(rdsColumns, key, alertId, deviceId)));
    String query=returnSuitableQueryString(rdsColumns, key, alertId, deviceId);
    mapRDS.put(key, insightSQL.returnResultsAsArray(query ,rdsColumns.split(","),rdsColumns));
}

where rdsColumns are the fields I am querying in RDS data-base.

Expected: iterating over both maps and verifying at that all values according to key in the first map contains or equal in the second map.

Gaurav Mall
  • 2,372
  • 1
  • 17
  • 33
tupac shakur
  • 658
  • 1
  • 12
  • 29
  • "Or at least the other linked hashmap contains the values". Maybe you should try [containsValue](https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html#containsValue-java.lang.Object-)? – Nexevis Aug 12 '19 at 15:37
  • @Gaurav Mall - thank you for your respond, I'm wondering how to use containsValue where taking the key from one map and checking for values on the second one. – tupac shakur Aug 12 '19 at 16:16
  • @Gaurav Mall - it's very simple I want to be able to check whether value from the first map exist in the second map (maps got the same keys with the same order). – tupac shakur Aug 12 '19 at 16:25

1 Answers1

0

This is the code you are looking for:

for (String keys : firstMap.keySet()) {
     String[] val1 = firstMap.get(keys);
     String[] val2 = secondMap.get(keys);

     if (Arrays.equals(val1, val2)) {
         //return true;
     }

     ArrayList<Boolean> contains = new ArrayList<>();

     for (int i = 0; i < val1.length; i++) {
          for (String[] secondMapVal : secondMap.values()) {
               List<String> list = Arrays.asList(secondMapVal);

               if (list.contains(val1[i])) {
                   contains.add(true);
                   break;
               } else contains.add(false);
          }
     }


     if (contains.contains(true)) {
         //return true; Even a single value matches up
     } else {
         //return false; Not even a sinle value matches up
     }
}

Basically what we have here is a HashMap<String, String>. We take the set of keys and iterate through them. Then we take the value with the key from the two sets. After we got the values we compare them and if they are the same I just print that they match. You can change this and implement this with other types of HashMaps, even where you use custom values. If I didn't understand your problem tell me and I will edit the answer.

Gaurav Mall
  • 2,372
  • 1
  • 17
  • 33