日期:2014-05-20 浏览次数:20725 次
Map<Integer, Integer> map = new HashMap<Integer, Integer>(); Scanner sc = new Scanner(System.in); int num = 0; while (true) { System.out.printf("please input a number([0] to exit): "); try { num = Integer.parseInt(sc.nextLine()); if (num == 0) break; } catch (Exception e) { System.out.println("error input, input again."); continue; } if (map.containsKey(num)) { map.put(num, map.get(num)+1); } else { map.put(num, 1); } } List<Entry<Integer, Integer>> list = new ArrayList<Entry<Integer, Integer>>(map.entrySet()); Collections.sort(list, new Comparator<Entery<Integer, Integer>>() { compare(Entry<Integer, Integer> e1, Entry<Integer, Integer> e2) { return e2.getValue().compareTo(e1.getValue()); } }); int max = list.get(0).getValue(); for (EntrySet<Integer, Integer> e : list) { if (e.getValue() == max) { System.out.printf("num=%d, times=%d\n", e.getKey(), e.getValue()); } }