0

I have something like

Collection<LinkedHashMap<String,Object>> responseObjects = (Collection<LinkedHashMap<String, Object>>) response.getResponseObjects();

I am writing some unit tests where I am getting 2 elements in this collection and want to put assertions on each element Collection having a map of <String,Object>. Both elements have different sets of values in LinkedHashMap.

I tried using For-Each

for(Map<String,Object> response : responseObjects) {
     assertEquals("Test", responseObj.get("stringField"));
}

But how do I control the index from this, since I need to put assertion on different data coming in.

Laurel
  • 5,965
  • 14
  • 31
  • 57
  • Does this answer your question? [How to iterate through LinkedHashMap with lists as values](https://stackoverflow.com/questions/12310914/how-to-iterate-through-linkedhashmap-with-lists-as-values) – abdo Salm Sep 09 '22 at 09:35
  • 1
    Actually, there is no notion of index in java.util.Collection but you can simply increment a local variable to get the job done, it just looks weird and not the right way to go in my humble opinion. You should use a List instead of a Collection if indexes matter. – gouessej Sep 09 '22 at 09:48
  • 1
    The notion of `index` is applicable **only** if you're dialing with a `List` interface. With `Collection` it doesn't make sense. – Alexander Ivanchenko Sep 09 '22 at 11:07
  • 1
    A Collection does not guarantee to traverse in a predictable order. – Piotr Gwiazda Sep 09 '22 at 19:13
  • @gouessej I ended up using a local variable to achieve the test result. But yes I agree with other suggestions to go with List interface to deal with index. Thanks! – Kanchan Soni Nov 08 '22 at 06:43

0 Answers0