0

So I have two String arrays:

String[] a = {"Tony", "33", "male", "New York"};
String[] b = {"John", "33", "male", "Chicago"};

Then I have an ArrayList of HashMaps:

ArrayList<HashMap<String, String>> list =
[{"name": "John", "age": "33", "gender": "male", "city": "Chicago"}],
[{"name": "Tony", "Age": "33", "gender": "male", "city": "New York"}];

So I get array a or b first from another method. I want to then compare that the 'name' or the value at index 0 in the string a or b is the same as 'name' value in the ArrayList, String a or b value at index 1 which is '33' is the same as 'age' value in the ArrayList and so forth. I have tried the following:

for(int i = 0; i <= list.size(); i++) {
    assertEquals(b[0], list.get(i).get("name"));
    assertEquals(b[1], list.get(i).get("age"));
    assertEquals(b[3], list.get(i).get("city"));
}

But the problem is, it works for only one of the string lists, i.e. it would only work if I get string b first because at i = 0, the ArrayList has same values as that of string b.

1 Answer 1

1
String[] a = {"Tony", "33", "male", "New York"};

List<Map<String, String>> listOfMaps = ...;

boolean found = false;
for(final Map<String, String> map : listOfmaps) {
    if(a[0].equals(map.get("name"))
            && a[1].equals(map.get("age"))
            && a[2].equals(map.get("gender"))
            && a[3].equals(map.get("city"))) {
        found = true;
        break;
    }
}

if(!found) {
    // raise the alarm - nothing matched
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.