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.