我创建了一个返回包含 URI 参数的 Map 的方法。除了 map.containsKey(value) 之外,还有其他方法可以检查键是否已经在 Map 中???
public static Map <String,Object> UriMap(String uri){
Map <String,Object> map = new HashMap<String,Object>();
String [] pathAndQuery = uri.split("\\?",2);
if(pathAndQuery.length == 2){//processing starts only if the size of pathAndQuery is 2
String [] query = pathAndQuery[1].split("\\&");//split the string to array of Strings
for(String str: query){
if(str.contains("=")){
String [] keysAndValues = str.split("\\=",2);
if(map.containsKey(keysAndValues[0])){
String oldValue = map.get(keysAndValues[0]).toString();
map.remove(keysAndValues[0]);
map.put(keysAndValues[0],Arrays.asList(oldValue, keysAndValues[1]));//for parameters listed twice put a list to value
}else{
map.put(keysAndValues[0],keysAndValues[1]);//put a String to a map
}
}else{
String newParameter = str;
map.put(newParameter,"");//if "=" symbol is missing I put empty string for value
}
}
}
return map;
}