Dón*_*nal 3 collections groovy
我想获得具有不同语言的所有语言环境的列表,其中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
Run Code Online (Sandbox Code Playgroud)
您使用了错误的语法:您使用的是Collection.unique(Closure closure):
allLocales.unique {new ISO3LangComparator()}
Run Code Online (Sandbox Code Playgroud)
你应该使用Collection.unique(比较器比较器)
allLocales.unique (new ISO3LangComparator())
Run Code Online (Sandbox Code Playgroud)
所以简单地使用()
而不是{}
,你的问题就解决了.
小智 5
亚当说的话.
要么...
allLocales.unique{it.ISO3Language}
Run Code Online (Sandbox Code Playgroud)
而你忘记了比较器
归档时间: |
|
查看次数: |
2673 次 |
最近记录: |