Govinda Sakhare
Govinda Sakhare

Reputation: 5739

Remove HashMap Key from String

I have mobile numbers in database table column, in a format of country_code followed by mobile_number So Mobile Number format is like this,

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

I have one HashMap containing CountryCodes of 5 countries like this,

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

My Bean Structure is something like this

class UserDetails {

  // other fields
   String countryCode;
   String mobileNumber;
}

Now my task is to take the mobile number from Database table column and split it in two parts and set countryCode and mobileNumber, but country code length(in map's key) varies between 3 and 4. This checking can be done by using subString() and equals() but I don't think it's correct way, So what would be the elegant(may be checking in map key) way to solve this issue?

Upvotes: 0

Views: 912

Answers (3)

David Pérez Cabrera
David Pérez Cabrera

Reputation: 5068

IMHO a single map is better. An example;

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

Upvotes: 1

Marvin
Marvin

Reputation: 14415

Although there is a library which seems to already do the trick, I think I'd go for an easy self-written solution:

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

Output:

[+91, 123456789]
[+971, 88888888]

Note that this may not work correctly when a mobile prefix is a substring of another, but according to Wikipedia country calling codes are prefix codes and therefore guarantee that "there is no whole code word in the system that is a prefix (initial segment) of any other code word in the system".

Upvotes: 2

Ashwinee K Jha
Ashwinee K Jha

Reputation: 9317

You could use two maps for country code of different lengths and then search first for a match with 3 letters, and then with 4 letters.

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

Output:

UAE

Upvotes: 0

Related Questions