从集合中删除重复项

3

我想获取所有使用不同语言的语言环境列表,其中ISO3代码用于标识语言环境的语言。我认为以下代码应该有效。

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.ISO3Language == 'ara'}.size()

// This assertion fails
assert arabicLocaleCount <= 1
2个回答

5

5

亚当说的话。
或者...

allLocales.unique{it.ISO3Language}

而你忘记了比较器


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接