// 求两个集合中,相同的键的值,值中的元素不能重复:
// 例如:
// key1:"abc" value1:"123"
// key2:"abc" value2:"234"
// 输出:1234
// 思路,找出两个集合中相同的键,将对应的值拼接、去重
public static void test(String[] args) {
Map<String, String> map1 = new HashMap<String, String>();
Map<String, String> map2 = new HashMap<String, String>();
Set<String> set = new HashSet<String>();
StringBuilder sb = new StringBuilder();
List list = new ArrayList();
map1.put("ASDF", "9527");
map1.put("sdfd", "123");
map1.put("xxxx", "456");
map1.put("ASccDF", "789");
map2.put("ASDF", "00");
map2.put("sdfd", "456");
map2.put("asdv", "789");
Set<String> set1 = map1.keySet();
Iterator<String> iterator = set1.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
if (map2.containsKey(key)) { // 找出两个集合中相同的键
char[] ch = map1.get(key).concat(map2.get(key)).toCharArray(); // 将结果拼接
for (int i = 0; i < ch.length; i++) { // 去重
if (!list.contains(ch[i])) {
list.add(ch[i]);
sb.append(ch[i]);
}
}
System.out.println("相同的键为:" + key + "去重后的结果为" + sb.toString());
sb.setLength(0); // 每次得到一个不重复的值后将stringBuilder和list中的数据清空
list.clear();
}
}
}