package com.akfucc.zhidao;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
/**
* @description:
http://zhidao.baidu.com/question/124856697.html 查找出相同两次以上的值 如:
* 3和4
* @author akfucc
* @date Nov 9, 2009
*/
public class P124856697 {
/**
* 查找出相同两次以上的值 如: 3和4
*
* 这里只是简单地将两字符串数组存放在一个HashMap里
*
* @param args
*/
public static void main(String[] args) {
String str_1[] = { "1", "4", "3", "3", "4", "4" };
String str_2[] = { "1", "4", "3", "3", "4", "4" };
method1(str_1, str_2);
}
public static void method1(String[] str_1, String[] str_2) {
// 不讲究效率, 只求结果
Map<String, Integer> map1 = new HashMap<String, Integer>();
Map<String, Integer> map2 = new HashMap<String, Integer>();
// 放入两个MAP
for (String str : str_1) {
Integer temp = map1.get(str);
if (temp == null) {
map1.put(str, 1);
} else {
map1.put(str, temp + 1);
}
}
for (String str : str_2) {
Integer temp = map2.get(str);
if (temp == null) {
map2.put(str, 1);
} else {
map2.put(str, temp + 1);
}
}
// 两个MAP 比较
Set<Entry<String, Integer>> set1 = map1.entrySet();
for (Entry<String, Integer> entry : set1) {
int num1 = entry.getValue().intValue();
if (num1 > 1) {
int num2 = map2.get(entry.getKey()).intValue();
if (num2 > 1) {
// 简单地测试, 仅仅打印出来, 如果你需要的话, 可以用List存放起来
System.out.println(entry.getKey());
}
}
}
}
}