[英]Remove HashMap Key from String
我在数据库表列中有手机号码,格式为country_code
然后是mobile_number
所以手机号码格式是这样的,
+91123456789 // country code of India is +91 followed by mobile number
+97188888888 // Country code of UAE +971
我有一个HashMap包含这样的5个国家/地区的CountryCodes,
map.put("+91","India")
map.put("+94","Sri Lanka")
map.put("+881","Bangladesh")
map.put("+971","UAE")
map.put("+977","Nepal")
我的豆结构是这样的
class UserDetails {
// other fields
String countryCode;
String mobileNumber;
}
现在,我的任务是从数据库表列中获取移动电话号码并将其分为两部分,并设置countryCode
和mobileNumber
,但是国家代码长度(在地图键中)在3到4之间变化。可以使用subString()
进行此检查和equals()
但我认为这不是正确的方法,那么解决此问题的优雅方法(可能是在map key
检查)是什么?
尽管有一个 似乎可以解决问题 的库 ,但我认为我会寻求一个简单的自编写解决方案:
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class CountryExtractor {
private static final Map<String, String> COUNTRY_MAPPING = new HashMap<>();
static {
COUNTRY_MAPPING.put("+91", "India");
COUNTRY_MAPPING.put("+94", "Sri Lanka");
COUNTRY_MAPPING.put("+881", "Bangladesh");
COUNTRY_MAPPING.put("+971", "UAE");
COUNTRY_MAPPING.put("+977", "Nepal");
}
public static void main(String[] args) {
String[] inputs = new String[] { "+91123456789", "+97188888888" };
for (String input : inputs) {
System.out.println(Arrays.toString(parseNumber(input)));
}
}
private static String[] parseNumber(String number) {
for (String countryCode : COUNTRY_MAPPING.keySet()) {
if (number.startsWith(countryCode)) {
return new String[] { countryCode, number.replace(countryCode, "") };
}
}
return new String[0];
}
}
输出:
[+91, 123456789]
[+971, 88888888]
请注意,当移动前缀是另一个的子字符串时,这可能无法正常工作,但是根据Wikipedia的国家/地区,呼叫代码是前缀代码 ,因此请确保“系统中没有作为前缀(初始段)的整个代码字。系统中的任何其他代码字”。
恕我直言,一张地图比较好。 一个例子;
public static void main(String args[]) throws Exception {
Map<String, String> map = new HashMap<>();
put(map, "+91", "India");
put(map, "+94", "Sri Lanka");
put(map, "+881", "Bangladesh");
put(map, "+971", "UAE");
put(map, "+977", "Nepal");
map = Collections.unmodifiableMap(map);
String mobileNumber = "+91123456789";
System.out.println(countryCode(map.keySet(), mobileNumber));
}
private static void put(Map<String, String> map, String key, String value) {
if (countryCode(map.keySet(), key) != null) {
throw new IllegalArgumentException("...");
}
map.put(key, value);
}
public static String countryCode(Set<String> countryCodes, String number) {
if (number == null || number.length() < 3) {
throw new IllegalArgumentException("...");
}
String code = number.substring(0, 3);
if (!countryCodes.contains(code)) {
if (number.length() > 3) {
code = number.substring(0, 4);
if (!countryCodes.contains(code)) {
code = null;
}
} else {
code = null;
}
}
return code;
}
您可以将两个地图用于不同长度的国家/地区代码,然后首先搜索3个字母的匹配项,然后搜索4个字母的匹配项。
HashMap<String, String > threeLetterCodes = new HashMap<String, String>();
threeLetterCodes.put("+91","India");
threeLetterCodes.put("+94","Sri Lanka");
HashMap<String, String > fourLetterCodes = new HashMap<String, String>();
fourLetterCodes.put("+881","Bangladesh");
fourLetterCodes.put("+971","UAE");
fourLetterCodes.put("+977","Nepal");
String test = "+97188888888";
String prefix = test.substring(0, 3);
String country = threeLetterCodes.get(prefix);
if (country == null) {
prefix = test.substring(0, 4);
country = fourLetterCodes.get(prefix);
}
System.out.println(country);
输出:
UAE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.