据我所知,在任何库中都没有这样的方法,但至少你可以自己声明它,就像这样:
import static java.util.Arrays.binarySearch;
import java.util.Locale;
/**
* Validator of country code.
* Uses binary search over array of sorted country codes.
* Country code has two ASCII letters so we need at least two bytes to represent the code.
* Two bytes are represented in Java by short type. This is useful for us because we can use Arrays.binarySearch(short[] a, short needle)
* Each country code is converted to short via countryCodeNeedle() function.
*
* Average speed of the method is 246.058 ops/ms which is twice slower than lookup over HashSet (523.678 ops/ms).
* Complexity is O(log(N)) instead of O(1) for HashSet.
* But it consumes only 520 bytes of RAM to keep the list of country codes instead of 22064 (> 21 Kb) to hold HashSet of country codes.
*/
public class CountryValidator {
/** Sorted array of country codes converted to short */
private static final short[] COUNTRIES_SHORT = initShortArray(Locale.getISOCountries());
public static boolean isValidCountryCode(String countryCode) {
if (countryCode == null || countryCode.length() != 2 || countryCodeIsNotAlphaUppercase(countryCode)) {
return false;
}
short needle = countryCodeNeedle(countryCode);
return binarySearch(COUNTRIES_SHORT, needle) >= 0;
}
private static boolean countryCodeIsNotAlphaUppercase(String countryCode) {
char c1 = countryCode.charAt(0);
if (c1 < 'A' || c1 > 'Z') {
return true;
}
char c2 = countryCode.charAt(1);
return c2 < 'A' || c2 > 'Z';
}
/**
* Country code has two ASCII letters so we need at least two bytes to represent the code.
* Two bytes are represented in Java by short type. So we should convert two bytes of country code to short.
* We can use something like:
* short val = (short)((hi << 8) | lo);
* But in fact very similar logic is done inside of String.hashCode() function.
* And what is even more important is that each string object already has cached hash code.
* So for us the conversion of two letter country code to short can be immediately.
* We can relay on String's hash code because it's specified in JLS
**/
private static short countryCodeNeedle(String countryCode) {
return (short) countryCode.hashCode();
}
private static short[] initShortArray(String[] isoCountries) {
short[] countriesShortArray = new short[isoCountries.length];
for (int i = 0; i < isoCountries.length; i++) {
String isoCountry = isoCountries[i];
countriesShortArray[i] = countryCodeNeedle(isoCountry);
}
return countriesShortArray;
}
}
将始终创建一个新数组,因此我们应该将其存储到静态字段中以避免不必要的分配。同时或消耗大量内存,因此此验证程序将在数组上使用二进制搜索。这是速度和内存之间的权衡。Locale.getISOCountries()
HashSet
TreeSet