java list里去掉重复值
有一个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>();
map = list.get(i);
System.out.println("area="+map.get("area")+",room="+map.get("room")+",speed="+map.get("speed")+",controlled="+map.get("controlled"));
}
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
for(int i=0;i<3;i++){
Insert("area_"+i,i+"0"+i,"speed_mod_"+i,"controlled_mod_"+i);
}
for (int i = 0; i < list.size(); i++) {
Map<String, String> map = new HashMap<String, String>();
map = list.get(i);
System.out.println("area="+map.get("area")+",room="+map.get("room")+",speed="+map.get("speed")+",controlled="+map.get("controlled"));
}
}
}
package dragon.modules.example.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ExampleAction {
public static List<Map<String, String>> list = new ArrayList<Map<String, String>>();
public static Map<String, String> map = new HashMap<String, String>();
public void insetWordList(String area, String room, String speed, String controlled) {
Map<String, String> wordListMap = new HashMap<String, String>();
String map_area, map_room;
for (int i = 0; i < list.size(); i++) {
wordListMap = list.get(i);
map_area = wordListMap.get("area");
map_room = wordListMap.get("room");
// 如果area重复或者room重复,不做新增操作
if (map_area.equalsIgnoreCase(area)
[解决办法]
map_room.equalsIgnoreCase(room)) {
return;
}
}
wordListMap = insetWordMap(area, room, speed, controlled);
list.add(wordListMap);
}
public Map<String, String> insetWordMap(String area, String room, String speed, String controlled) {
Map<String, String> wordListMap = new HashMap<String, String>();
wordListMap.put("area", area);
wordListMap.put("room", room);
wordListMap.put("speed", speed);
wordListMap.put("controlled", controlled);
return wordListMap;
}
public static void main(String[] args) {
ExampleAction example = new ExampleAction();
// 插入两条不重复的信息
example.insetWordList("1", "1", "1", "1");
example.insetWordList("2", "2", "2", "2");
// 插入area不重复,room重复的信息
example.insetWordList("3", "1", "3", "3");
// 插入area重复,room不重复的信息
example.insetWordList("1", "4", "4", "4");
// 插入area重复,room重复的信息
example.insetWordList("1", "1", "5", "5");
// 打印list
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).toString());
}
}
}