优质解答
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SomeProgram {
\x09public static void main(String[] args) {
\x09\x09Map map = new TreeMap();
\x09\x09map.put("a1","qwe");
\x09\x09map.put("a2","rty");
\x09\x09map.put("a3","qwe");
\x09\x09map.put("a4","uio");
\x09\x09map.put("a5","pas");
\x09\x09map.put("a6","pas");
\x09\x09ByteArrayOutputStream baos = new ByteArrayOutputStream();
\x09\x09PrintStream ps = new PrintStream(baos);
\x09\x09PrintStream old = System.out;
\x09\x09System.setOut(ps);
\x09\x09Set set1 = new HashSet();
\x09\x09Set set2 = new HashSet();
\x09\x09for (Map.Entry entry :map.entrySet()) {
\x09\x09\x09if (!set1.add(entry.getValue())) {
\x09\x09\x09\x09set2.add(entry.getValue());
\x09\x09\x09}
\x09\x09\x09System.out.print("key=" + entry.getKey() + ",value="
\x09\x09\x09\x09\x09+ entry.getValue() + "\r\n");
\x09\x09}
\x09\x09ps.flush();
\x09\x09System.setOut(old);
\x09\x09String source = new String(baos.toByteArray());
\x09\x09String[] strs = set2.toArray(new String[set2.size()]);
\x09\x09for (String s :strs)
\x09\x09\x09fetchSameKeyValuePair(source,s);
\x09}
\x09private static void fetchSameKeyValuePair(String source,String key) {
\x09\x09Pattern p = Pattern.compile("(key=[a-zA-Z0-9]*,value=" + key + ")");
\x09\x09Matcher m = p.matcher(source);
\x09\x09while (m.find()) {
\x09\x09\x09System.out.println(m.group(1));
\x09\x09}
\x09}
}