Hamcrest equal collections

Andrey Minogin picture Andrey Minogin · Dec 25, 2009 · Viewed 13k times · Source

Is there a matcher in Hamcrest to compare collections for equality? There is contains and containsInAnyOrder but I need equals not bound to concrete collection type. E.g. I cannot compare Arrays.asList and Map.values with Hamcrest equals.

Thanks in advance!

Answer

skaffman picture skaffman · Dec 25, 2009

I cannot compare Arrays.asList and Map.values with Hamcrest equals.

This is because of hamcrest's over-zealous type signatures. You can do this equality comparison, but you need to cast the List object to Collection before it'll compile.

I often have to do casting with Hamcrest, which feels wrong, but it's the only way to get it to compile sometimes.