Java Reference
In-Depth Information
Make use of the new locale matching and filtering methods that have been introduced
in the java.util.Locale class in Java 8. If you're given a comma-separated list
of locales in string format, you can apply a filter or “priority list” to that string to return
only those locales within the string that meet the filter. In the following example, a list
of language tags is filtered using the java.util.Locale filterTag method,
returning the matching tags in String format:
List<Locale.LanguageRange> list1
= Locale.LanguageRange.parse("ja-JP, en-US");
list1.stream().forEach((range) -> {
System.out.println("Range:" + range.getRange());
});
ArrayList localeList = new ArrayList();
localeList.add("en-US");
localeList.add("en-JP");
List<String> tags1 = Locale.filterTags(list1, localeList);
System.out.println("The following is the filtered list of
locales:");
tags1.stream().forEach((tag) -> {
System.out.println(tag);
});
Results:
Range:ja-jp
Range:en-us
The following is the filtered list of Locales:
en-us
The filter() method of the Locale classes allows you to return a list of
matching Locale instances. In the following example, a list of locale language tags is
used to filter Locale classes out of a list of locales.
String localeTags = Locale.ENGLISH.toLanguageTag() + "," +
Locale.CANADA.toLanguageTag();
List<Locale.LanguageRange> list1
= Locale.LanguageRange.parse(localeTags);
Search WWH ::




Custom Search