从String中删除HashMap Key

时间:2016-08-21 13:02:50

标签: java

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

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

我有一个包含5个国家/地区的CountryCodes的HashMap,

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 个答案:

答案 0 :(得分:2)

虽然there is a library seems to already do the trick Wikipedia,但我认为我会选择一个简单的自编解决方案:

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]

请注意,当移动前缀是另一个的子字符串时,这可能无法正常工作,但根据prefix codes国家/地区的调用代码为enter image description here,因此保证&#34;没有完整的代码系统中的单词,是系统中任何其他代码字的前缀(初始段)&#34;。

答案 1 :(得分:1)

恕我直言,单个地图更好。一个例子;

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;
}

答案 2 :(得分:0)

您可以为不同长度的国家/地区代码使用两张地图,然后首先搜索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