mmar
mmar

Reputation: 2020

How can we compare two hashmaps with case insensitive on both keys and values

I have two maps with both same values, but differing in case, either in key or in value. While asserting, I need to make it as pass. I know the Treemap with case insensitive option can be used, but it validates the keys alone and not the values. Is there a way that we can compare two maps irrespective of case in both key and value pairs?

map1 = { "Object" : "Java", "Scripting" : "Javascript" } // value Javascript with 's'
map1 = { "object" : "Java", "Scripting" : "JavaScript" } // key Object with 'o'

I use assertj-core and while asserting these two maps I get error because of case difference.

Upvotes: 2

Views: 1129

Answers (1)

k314159
k314159

Reputation: 11100

You can use AssertJ's method usingElementComparator:

    assertThat(actualMap.entrySet())
        .usingElementComparator(
            Map.Entry.<String, String>comparingByKey(String.CASE_INSENSITIVE_ORDER)
                .thenComparing(Map.Entry::getValue, String.CASE_INSENSITIVE_ORDER)
        )
        .containsExactlyInAnyOrderElementsOf(expectedMap.entrySet());

Upvotes: 4

Related Questions