0

I have a hashmap with Key and Value being 'String'. I want to check if a particular key exists by ignoring string after '$' in the Key.

Hashmap contains keys as 'acctId$accountId', 'acctId$desc', 'acctId$crncyCode' etc.

Iterator itx = uiToSrvFldMapList.entrySet().iterator();
if(uiToSrvFldMapList.containsKey(cellId)){
      String sSrvFld = (String) uiToSrvFldMapList.get("acctId");
      System.out.println("sSrvFld :: " +sSrvFld);
user3684156
  • 31
  • 1
  • 2
  • 8
  • Why dont you just use a stacked map? Something like `Map>` would make access easier and faster. You could then check for `myMap.get(firstPart).get(secondPart)`. Of course you have to ceck if the first one is not `null`. This would also be better for your running time. – Alex VII Jun 18 '14 at 12:32
  • Use if(uiToSrvFldMapList.containsKey(cellId.split("$")[0])){ – mizanurahma Jun 18 '14 at 12:32
  • Not easy with this structure, you basically have to do a linear search through the whole table. – Henry Jun 18 '14 at 12:34

4 Answers4

1
public static void main(String[] args) {
    String s = "acctId$accountId";
    s = s.replaceAll("\\$.*", "");// remove everything after $
    System.out.println(s);
    // do hm.get(s) here
}
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

I hope this might help you

    Map map = new HashMap();
    map.put("abc$def","ABC");
    map.put("ab","A");
    map.put("de","b");
    String key = "abc$def";
    String s[] = key.split("$");
    if(map.containsKey(s[0]))
        System.out.println("Value is: "+map.get(key));
    else
        System.out.println("cannot find..");
SparkOn
  • 8,806
  • 4
  • 29
  • 34
0

Supposing that in "acctId$accountId" you will have the same String both as "acctId" and "accountId", you can search for it in the following way:

   `Map<String, String> uiToSrvFldMapList = new HashMap<String, String>();
    uiToSrvFldMapList.put("0000$0000", "test"); // just an example
    uiToSrvFldMapList.put("0000$0001", "description"); // just an example
    uiToSrvFldMapList.put("0001$0000", "2test"); // just an example
    uiToSrvFldMapList.put("0001$0001", "2description"); // just an example

    String acctId = "0000"; // the account id string
    if(uiToSrvFldMapList.containsKey(acctId +"$" + acctId)){
       String sSrvFld = (String) uiToSrvFldMapList.get(acctId + "$" + acctId);                            
       System.out.println("sSrvFld :: " +sSrvFld);
       }`
Lorenzo Addazi
  • 325
  • 3
  • 12
0

This is a test program, which shows a way to achieve this functionality:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Test {

    public static void main(String[] args) {
        Map<String, String> uiToSrvFldMapList = new HashMap<String, String>();
        uiToSrvFldMapList.put("acctId$accountId", "accid");
        uiToSrvFldMapList.put("acctId$desc", "accdesc");
        uiToSrvFldMapList.put("acctId$crncyCode", "currencyCode");
        uiToSrvFldMapList.put("smthElse$smthElse", "smthElse");

        List<String> valuesContainingKey = valuesContainingKeys(
                uiToSrvFldMapList, "acctId");

        // Returns if the key is contained
        if (valuesContainingKey.isEmpty()) {
            System.out.println("The key is not contained in the map");
        } else {
            System.out.println("The part of the key is in the map");
        }

        System.out
                .println("All values, where the corresponding key contains the subkey: ");
        for (String s : valuesContainingKey) {
            System.out.println(s);
        }
    }

    /**
     * 
     * @param map
     *            Map containing the key-value pairs
     * @param searchString
     *            A String used as a subkey, for which is searched if it is
     *            contained as a substring at the beginning of a key in the map
     * @return List of all Values from the map, whose corresponding key contains
     *         searchString
     */
    private static List<String> valuesContainingKeys(Map<String, String> map,
            String searchString) {
        List<String> containingKeys = new ArrayList<String>();
        for (Entry<String, String> e : map.entrySet()) {
            if (e.getKey().startsWith(searchString)) {
                containingKeys.add(e.getValue());
            }
        }
        return containingKeys;
    }
}

Simply write the method valuesContainingKeys (not needed to be static) where you want this functionality. This method will return a list of all values, whose corresponding key contains the string you are looking for. Simply checking valuesContainingKey.isEmpty() will return if there is no value, for which the corresponding key begins with the searched key.

Ivaylo Toskov
  • 3,911
  • 3
  • 32
  • 48