需求:现有一个字符串"a,b"然后还有个HashMap如[{id=1, name=a}, {id=2, name=b}, {id=3, name=c}],现在需要判断字符串是否和HashMap中的name相同,如果相同则放入新表作为参数传递。
具体实现如下:
public static void main(String[] args) {
String val = "a,b";
String arr[] = val.split(",");
HashMap<String,String> row1 = new HashMap<String,String>();
row1.put("id", "1");
row1.put("name","a");
HashMap<String,String> row2 = new HashMap<String,String>();
row2.put("id", "2");
row2.put("name","b");
HashMap<String,String> row3 = new HashMap<String,String>();
row3.put("id", "3");
row3.put("name","c");
List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
list.add(row1);
list.add(row2);
list.add(row3);
List<String> listReal = new ArrayList<String>();
for(int j=0,len=arr.length;j<len;j++){
String strTemp = arr[j];
for(int h=0,len2=list.size();h<len2;h++){
HashMap<String, String> strTemp2 = list.get(h);
if(strTemp.equals(strTemp2.get("name"))){
listReal.add(strTemp);
break;
}
}
}
for(String s:listReal){
System.out.println(s);
}
}