有一个list,里面添加的是map,要保证list里每个map的区域和房间号不重复,我这么之后没有实现这个功能,想请教下各位!
public static List<Map<String, String>> list = new ArrayList<Map<String, String>>();
public static Map<String, String> map = new HashMap<String, String>();
public void Insert(String area,String room,String speed,String controlled){
Map<String, String> wordListMap =new HashMap<String, String>();
String map_area,map_room;
if(list.size() >0){
for(int i=0;i<list.size();i++){
wordListMap = list.get(i);
map_area = wordListMap.get("area");
map_room = wordListMap.get("room");
if(map_area.equalsIgnoreCase(area) && map_room.equalsIgnoreCase(room)){
list.remove(i);
i--;
}
}
wordListMap.put("area",area);
wordListMap.put("room",room);
wordListMap.put("speed",speed);
wordListMap.put("controlled",controlled);
}else{
wordListMap.put("area",area);
wordListMap.put("room",room);
wordListMap.put("speed",speed);
wordListMap.put("controlled",controlled);
}
}直接上代码
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ListTest {
public static List<Map<String, String>> list = new ArrayList<Map<String, String>>();
public static void Insert(String area, String room, String speed, String controlled) {
String map_area, map_room;
if (list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
Map<String, String> wordListMap = list.get(i);
map_area = wordListMap.get("area");
map_room = wordListMap.get("room");
if (map_area.equalsIgnoreCase(area) && map_room.equalsIgnoreCase(room)) {
list.remove(i);
System.out.println("--重复移除--");
i--;
}
}
Map<String, String> map = new HashMap<String, String>();
map.put("area", area);
map.put("room", room);
map.put("speed", speed);
map.put("controlled", controlled);
list.add(map);
} else {
Map<String, String> map = new HashMap<String, String>();
map.put("area", area);
map.put("room", room);
map.put("speed", speed);
map.put("controlled", controlled);
list.add(map);
}
}
/**
* @param args
*/
public static void main(String[] args) {
for(int i=0;i<5;i++){
Insert("area_"+i,i+"0"+i,"speed_"+i,"controlled_"+i);
}
for (int i = 0; i < list.size(); i++) {
Map<String, String> map = new HashMap<String, String>();