日期:2014-05-20 浏览次数:20551 次
import java.util.HashSet; import java.util.TreeMap; public class Test { public static void main(String[] args) { int[] arr1 = new int[]{2,1,3,2,3,2,4,7,3}; int[] arr2 = new int[]{1,3,2,3,1,3,3,2,1}; TreeMap<Integer, HashSet<Integer>> map = new TreeMap<Integer, HashSet<Integer>>(); HashSet<Integer> set = new HashSet<Integer>(); for(int i = 0;i < arr2.length;i++){ set = new HashSet<Integer>(); if(map.containsKey((Integer)arr2[i])){ set = map.get((Integer)arr2[i]); set.add(arr1[i]); map.put(arr2[i], set); }else { set.add(arr1[i]); map.put(arr2[i], set); } } Integer[] keyArr = new Integer[map.size()]; map.keySet().toArray(keyArr); for(int i = 0;i < map.size();i++){ System.out.println(keyArr[i] + "," + map.get(keyArr[i]).size()); } } }