This question already has answers here:
HashMap<String, Integer> Search for part of an key? [duplicate]

(9个答案)


6年前关闭。





我有一个键和值是“字符串”的哈希图。我想通过忽略Key中'$'之后的字符串来检查是否存在特定的key。

哈希图包含的键为'acctId $ accountId','acctId $ desc','acctId $ crncyCode'等。

Iterator itx = uiToSrvFldMapList.entrySet().iterator();
if(uiToSrvFldMapList.containsKey(cellId)){
      String sSrvFld = (String) uiToSrvFldMapList.get("acctId");
      System.out.println("sSrvFld :: " +sSrvFld);

最佳答案

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
}

07-24 13:21