-2

I have a String array like this:

String[] a={"Hi","Hello","Dear","Jungle"};

And I have a HashMap like this:

Map<String, String> map=new HashMap<>();
map.put("Hi", "1");
map.put("Hello", "2");
map.put("Dear", "3");
map.put("Jungle", "4");
map.put("Sai", "5");
map.put("Surya", "6");

The HashMap is my basis to compare. If we see, the String array misses out two Strings SAI and SURYA.

I'm iterating over the hashmap and checking with String array like this:

int c=0;
for(String key: map.keySet()){
    c=0;
    for (int i = 0; i < a.length; i++) {

        if(key.equals(a[i])){
            c++;
            System.out.println("App :"+ key+"present in DB , SO it is passed.");
        }               
    }
    if(c==0){
        System.out.println("App not present in DB, SO it might have failed");
    }
}

This is my complete program:

import java.util.HashMap;
import java.util.Map;


public class TestingApplication {

    public static void main(String[] args) {

        String[] a={"Hi","Hello","Dear","Jungle"};
        String[] status={"Passed","Passed","Passed","Passed"};

        Map<String, String> map=new HashMap<>();
        map.put("Hi", "1");
        map.put("Hello", "2");
        map.put("Dear", "3");
        map.put("Jungle", "4");
        map.put("Sai", "5");
        map.put("Surya", "6");
        int c=0;
        for(String key: map.keySet()){
            c=0;
            for (int i = 0; i < a.length; i++) {

                if(key.equals(a[i])){
                    c++;
                    System.out.println("App:"+ a[i]+" present in DB , SO it is passed.");
                }
            }
            if(c==0){
                System.out.println("App not present in DB, SO it might have failed");
            }
        }
    }
}

And my OUTPUT is:

App not present in DB, SO it might have failed
App:Hi present in DB , SO it is passed.
App:Dear present in DB , SO it is passed.
App:Hello present in DB , SO it is passed.
App:Jungle present in DB , SO it is passed.
App not present in DB, SO it might have failed

1.) How do we get to know, which KEY is missing in the array? 2.) How do we add the missing String to the array?

EDIT: My question isn't about iterating over a HashMap. My question is about how to find out the differences and storing them back into a data sturcture. Please read my question and then mark it duplicate, if you feel so.

Sai Surya
  • 43
  • 1
  • 11
  • Iterate on the hashmap: http://stackoverflow.com/questions/46898/how-to-efficiently-iterate-over-each-entry-in-a-map – denvercoder9 Dec 08 '16 at 15:19
  • @RafiduzzamanSonnet Iterating through HashMaps is not my question. I wanted to know how to find the difference in data. – Sai Surya Dec 08 '16 at 15:32
  • `if (c==0) {...}` It means that the `key` does not exist in the array, right? So, you know which KEY is missing in the array. There goes your first answer. And for adding the missing KEY to the array, you can make `String [] a` an arraylist instead of an array. So if you make `a` an arraylist from the start, then you can just do this in your `if` condition: `a.add(key)` – denvercoder9 Dec 08 '16 at 15:38

3 Answers3

2

You need to change only few things in your code, try below code

 public class TestingApplication 
    {
        public static void main(String[] args) 
        {
            String[] a = { "Hi", "Hello", "Dear", "Jungle" };
            Map<String, String> map = new HashMap<>();
            map.put("Hi", "1");
            map.put("Hello", "2");
            map.put("Dear", "3");
            map.put("Jungle", "4");
            map.put("Sai", "5");
            map.put("Surya", "6");
            List<String> missedKeyList = new ArrayList<>();
            for (String key : map.keySet()) 
            {
                int c = 0;
                for (int i = 0; i < a.length; i++) 
                {
                    if (key.equals(a[i])) 
                    {
                        c++;
                        System.out.println("App: " + a[i] + " present in DB , So it is passed.");
                        break;
                    }
                }

                if (c == 0) 
                {
                    System.out.println("App: " + key + " not present in DB, So it might have failed");
                    missedKeyList.add(key);
                }
            }

            String[] allKey = Arrays.copyOfRange(a, 0, a.length + missedKeyList.size());
            int counter = a.length;
            for(String missedKey : missedKeyList)
            {
                allKey[counter] = missedKey;
                counter++;
            }
        }
    }
Ravi
  • 673
  • 1
  • 5
  • 13
1

Using Guava library it is very simple to achieve this one, refer documentation for more info: https://github.com/google/guava/wiki/CollectionUtilitiesExplained

Using Java-8 feature, Im printing it but you can take iterate on itemsNotFoundInArray and add to array with missing items.

final Set<String> itemsFromArray = Sets.newHashSet(a);
final Set<String> itemsFromMap= map.keySet();
Sets.SetView<String> itemsNotInArray = Sets.difference(itemsFromMap, itemsFromArray);
itemsNotInArray.forEach(System.out::println);
Nagaraddi
  • 269
  • 2
  • 7
1

First of all if you want to add something to the array then you would need dynamic array for it unless you create an array of large size beforehand. ArrayList would be a good candidate for that. And then once you make an ArrayList then achieving your task would 2 or 3 liner code.

    String[] staticArray = new String[] { "Hi","Hello","Dear","Jungle"};
    ArrayList myList = Arrays.asList(staticArray);
    for(String key: map.keySet()){
    if(!myList.contains(key){
         myList.add(key);    
      }
    }
Arjun Lajpal
  • 208
  • 1
  • 11