有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从字符串中删除HashMap键

我在数据库表列中有移动电话号码,格式为country_code,后跟mobile_number 所以手机号码格式是这样的

+91123456789 // country code of India is +91 followed by mobile number
+97188888888 // Country code of UAE +971

我有一个HashMap包含5个国家的国家代码

map.put("+91","India")
map.put("+94","Sri Lanka")
map.put("+881","Bangladesh")
map.put("+971","UAE")
map.put("+977","Nepal")

我的Bean结构是这样的

class UserDetails {

  // other fields
   String countryCode;
   String mobileNumber;
}

现在,我的任务是从数据库表列中获取移动电话号码,并将其分成两部分,然后设置countryCodemobileNumber,但国家代码长度(在地图的键中)在3和4之间变化。这个检查可以通过使用subString()equals()来完成,但我认为这不是正确的方法,那么解决这个问题的优雅(可能是在map key)方法是什么呢


共 (3) 个答案

  1. # 1 楼答案

    虽然there is a libraryseems to already do the trick好,但我想我会选择一个简单的自编解决方案:

    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,国家呼叫代码是prefix codes,因此保证“系统中没有作为系统中任何其他码字前缀(初始段)的整个码字”

  2. # 2 楼答案

    我觉得一张地图更好。一个例子

    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. # 3 楼答案

    你可以用两张地图来显示不同长度的国家代码,然后先搜索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