2011-03-22 84 views
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 

回答

4

亞當說了什麼。
或...

allLocales.unique{it.ISO3Language} 

,你忘了比較