开发者

remove duplicates from collection

I want to get a list of all Locales that have a different language, where the ISO3 code is used to identify the language of a Locale. I thought the following should work

class ISO3LangComparator implements Comparator<Locale> {

    int compare(Locale locale1, Locale locale2) {
        locale1.ISO3Language <=> locale2.ISO3Language
    }
}

def allLocales = Locale.getAvailableLocales().toList()
def uniqueLocales = allLocales.unique {new ISO3LangComparator()}

// Test how many locales there are with iso3 code 'ara'
def arabicLocaleCount = uniqueLocales.findAll {it.ISO3Languag开发者_如何学运维e == 'ara'}.size()

// This assertion fails
assert arabicLocaleCount <= 1


You are using the wrong syntax: you are using Collection.unique(Closure closure):

allLocales.unique {new ISO3LangComparator()}

You should use Collection.unique(Comparator comparator)

allLocales.unique (new ISO3LangComparator())

So simply use () instead of {}, and your problem is solved.


what Adam said.
or...

allLocales.unique{it.ISO3Language}

and you forget about the comparator

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜