Java 检查';键';存在于哈希映射中
我有一个hashmap,其键和值为“String”。我想通过忽略键中“$”之后的字符串来检查特定键是否存在 Hashmap包含“acctId$accountId”、“acctId$desc”、“acctId$crncyCode”等键Java 检查';键';存在于哈希映射中,java,Java,我有一个hashmap,其键和值为“String”。我想通过忽略键中“$”之后的字符串来检查特定键是否存在 Hashmap包含“acctId$accountId”、“acctId$desc”、“acctId$crncyCode”等键 Iterator itx = uiToSrvFldMapList.entrySet().iterator(); if(uiToSrvFldMapList.containsKey(cellId)){ String sSrvFld = (String) ui
Iterator itx = uiToSrvFldMapList.entrySet().iterator();
if(uiToSrvFldMapList.containsKey(cellId)){
String sSrvFld = (String) uiToSrvFldMapList.get("acctId");
System.out.println("sSrvFld :: " +sSrvFld);
我希望这对你有帮助
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..");
假设在“acctId$accountId”中,您将拥有与“acctId”和“accountId”相同的字符串,您可以通过以下方式搜索该字符串:
`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);
}`
`Map uitosrvldmaplist=newhashmap();
uiToSrvFldMapList.put(“0000$0000”,“测试”);//只是一个例子
uiToSrvFldMapList.put(“0000$0001”,“说明”);//只是一个例子
uiToSrvFldMapList.put(“0001$0000”,“2测试”);//只是一个例子
uiToSrvFldMapList.put(“0001$0001”,“2说明”);//只是一个例子
字符串acctId=“0000”;//帐户id字符串
if(uiToSrvFldMapList.containsKey(acctId+“$”+acctId)){
String sSrvFld=(String)uitosrvldmaplist.get(acctId+“$”+acctId);
System.out.println(“sSrvFld::”+sSrvFld);
}`
这是一个测试程序,显示了实现此功能的方法:
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;
}
}
import java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入java.util.Map.Entry;
公开课考试{
公共静态void main(字符串[]args){
Map uitosrvldmaplist=新HashMap();
uiToSrvFldMapList.put(“账户$accountId”,“账户ID”);
uiToSrvFldMapList.put(“acctId$desc”、“accdesc”);
uiToSrvFldMapList.put(“账户$crncyCode”,“货币代码”);
uiToSrvFldMapList.put(“smthElse$smthElse”,“smthElse”);
列表值ContainingKey=值ContainingKey(
uiToSrvFldMapList,“acctId”);
//如果包含密钥,则返回
if(valuesContainingKey.isEmpty()){
System.out.println(“地图中不包含密钥”);
}否则{
System.out.println(“键的部分在地图中”);
}
系统输出
.println(“所有值,其中对应的键包含子键:”);
for(字符串s:valuesContainingKey){
系统输出打印项次;
}
}
/**
*
*@param-map
*包含键值对的映射
*@param searchString
*用作子键的字符串,如果子键是
*作为子字符串包含在映射中键的开头
*@返回映射中所有值的列表,其对应键包含
*搜索字符串
*/
私有静态列表值包含密钥(映射,
字符串搜索(字符串){
List containingKeys=new ArrayList();
对于(条目e:map.entrySet()){
if(例如getKey().startsWith(searchString)){
包含keys.add(例如getValue());
}
}
返回包含键;
}
}
只需将方法
值containingkeys
(不需要是静态的)写入需要此功能的位置。此方法将返回所有值的列表,其对应的键包含要查找的字符串。只要检查值containingkey.isEmpty()
就会返回没有值的值,对应的键以搜索的键开始。为什么不使用堆叠贴图呢?像Map
这样的东西会使访问更容易、更快。然后,您可以检查myMap.get(第一部分)。get(第二部分)。当然,如果第一个不是null
,您必须切克。这对您的运行时间也有好处。使用if(uitosrvldmaplist.containsKey(cellId.split(“$”[0]){使用这种结构不容易,您基本上必须对整个表进行线性搜索。
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;
}
}