I'd use linked hash map as a container and java.util.Locale to get information about country:
private HashMap<String,Country> defaultCountries() {
return countriesWithFlags().collect(
LinkedHashMap::new,
(map,c) -> {try {
map.put(c[0], new Country(c[0], c[1], Files.readAllBytes(Paths.get(c[2]))));
} catch (IOException ignore) {}},
LinkedHashMap::putAll
);
}
private Stream<String[]> countriesWithFlags() {
return Arrays.stream(Locale.getISOCountries()).map(iso -> new Locale("", iso))
.map(l -> new String[]{
l.getCountry(),
l.getDisplayCountry(),
String.format("/images/flags/%s.png", l.getDisplayCountry().toLowerCase())
});
}
Unit test for countriesWithFlags:
package example;
import org.junit.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
public class ContryCodesTest {
final static Logger LOGGER = Logger.getLogger(ContryCodesTest.class.getName());
public static class Country{
public Country(String iso, String name, byte[] image) {
// FIXME
}
}
private HashMap<String,Country> defaultCountries() {
return countriesWithFlags().collect(
LinkedHashMap::new,
(map,c) -> {try {
map.put(c[0], new Country(c[0], c[1], Files.readAllBytes(Paths.get(c[2]))));
} catch (IOException ignore) {}},
LinkedHashMap::putAll
);
}
private Stream<String[]> countriesWithFlags() {
return Arrays.stream(Locale.getISOCountries()).map(iso -> new Locale("", iso))
.map(l -> new String[]{
l.getCountry(),
l.getDisplayCountry(),
String.format("/images/flags/%s.png", l.getDisplayCountry().toLowerCase())
});
}
@Test public void test() {
List<String[]> cwfs = countriesWithFlags().collect(Collectors.toList());
assertThat(cwfs, hasSize(250));
assertThat(cwfs.get(cwfs.size()-1)[2], equalTo("/images/flags/zimbabwe.png"));
}
}