List<map<string,string>>类型的集合,分离出重复的map值和不重复的? 100
2019-11-22
思路很简单:利用set集合不重复特性,找出重复的value,然后做个标记,进行分类即可!
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Day22A {
final static List<Map<String, String>> list = new ArrayList<Map<String, String>>() {
private static final long serialVersionUID = 32L;{
add(new HashMap<String, String>() {
private static final long serialVersionUID = 1L;{
put("A=2", "ID_CARD=q");
}
});
add(new HashMap<String, String>() {
private static final long serialVersionUID = 1L;{
put("B=2", "衡液ID_CARD=q");
}
});
add(new HashMap<String, String>() {
private static final long serialVersionUID = 1L;{
put("B=2", "ID_CARD=s");
}
});
add(new HashMap<String, String>() {
private static final long serialVersionUID = 1L;{
put("C=2", "ID_CARD=3");
}
});
add(new HashMap<String, String>() {
private static final long serialVersionUID = 1L;{
put("C=1", "ID_CARD=q");
}
});
}
};
static List<Map<String, String>> listA = new ArrayList<>();
static List<Map<String, String>>咐信物坦段 listB = new ArrayList<>();
static Set<String> set = new HashSet<>();
static String str;
static boolean flag=true;
public static void main(String[] args) {
System.out.println("-------原始集合--------");
lookUp(list);
selectFun(true);
System.out.println("\r\n--------[分离A]--------");
lookUp(listA);
System.out.println("\r\n--------[分离B]--------");
lookUp(listB);
}
public static void selectFun(boolean b) {
for (Map<String, String> map : list) {
for(Map.Entry<String, String> me:map.entrySet()){
String key=me.getKey();
String value=me.getValue();
if(b) {
if(!(flag=set.add(value))){
str=value;
selectFun(false);
return;
}
}else{
if(value.equals(str)) {
listA.add(new HashMap<String,String>(){
private static final long serialVersionUID = 1L;{
put(key,value);}
});
}else {
listB.add(new HashMap<String,String>(){
private static final long serialVersionUID = 1L;{
put(key,value);}
});
}
}
}
}
}
public static void lookUp(List<Map<String,String>> list) {
for (Map<String,String> map : list) {
for(Map.Entry<String, String> me:map.entrySet()){
String key=me.getKey();
String value=me.getValue();
System.out.println(key+","+value);
}
}
}
}