下面是一個程序,演示您正在尋找的解決方案。仔細查看getHashmap()
方法,該方法將選擇字符串和選擇參數作爲參數,並返回您正在查找的哈希表。我用你的數據集作爲例子。取決於你在尋找什麼,它應該離你的解決方案更近一步。唯一需要注意的是,如果您要使用不同的邏輯比較以及'=',則需要修改正則表達式。讓我知道,如果它很好地爲您
public static void main(String[] args) {
String sel = "a=? and b=?";
String[] ags = new String[] { "2", "hello" };
HashMap<String, String> result = getHashmap(sel, ags);
Iterator it = result.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry) it.next();
System.out.println(pairs.getKey() + " = " + pairs.getValue());
it.remove();
}
}
public static HashMap<String, String> getHashmap(String selection,
String[] selectionArgs) {
HashMap<String, String> result = new HashMap<String, String>();
Pattern pattern = Pattern.compile("[a-z]*(\\s)*=\\?",
Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(selection);
int pos = 0;
while (matcher.find()) {
String[] selParts = matcher.group(0).split("=");
result.put(selParts[0], selectionArgs[pos]);
pos++;
}
return result;
}
你要面對的問題是,你的地圖是無法representant運營商(或VS和) – njzk2
那麼什麼是你的建議嗎? – Bear
我不知道你在做什麼 – njzk2