这是一个程序,它演示了您正在寻找的解决方案。仔细查看该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;
}