7-1 12-06查找重复最多的整数 (20 分)
读取个数不定的整数,查找其中出现频率最高的数字,当输入为0时,表示输入结束。如果输入的频率最高的数字不是一个,而是多个,则把多个数字都显示出来。
输入格式:
输入多个整数,用空格隔开,读到0时,读取结束
输出格式:
输出该数字和重复的次数。
输入样例:
31 123 12 4 31 1 4 0
结尾无空行
输出样例:
Number 4 occurred most:2
Number 31 occurred most:2
结尾无空行
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
/**
*
*/
/*
*/
import java.lang.reflect.Field;
import java.util.*;
import java.util.Map.Entry;
/**
* http://lxw1234.com
* lxw的大数据田地
* @author lxw1234.com
* http://lxw1234.com/archives/2015/08/458.htm
*
*/
class MapUtil {
public static<T> LinkedHashMap<T,Integer> sortedMapByValue(Map<T ,Integer> map){
return sortedMapByValue(map,false);
//map按照value升序排序
// List<Map.Entry<T, Integer>> mapList =
// new ArrayList<>(map.entrySet());
comparingInt reverse
,Comparator.reverseOrder()
// mapList.sort(Comparator.comparingInt(Map.Entry::getValue));
mapList.sort(Comparator.comparing(Map.Entry::getValue).reversed());
Non-static method cannot be referenced from a static context
mapList.sort(Comparator.comparing(Map.Entry.comparingByValue()).reversed());
mapList.sort(Comparator.comparing(Map.Entry::comparingByValue));
// mapList.sort(Map.Entry.comparingByValue());
逆序的排序
new Comparable<>()
// mapList.sort((o1,o2)-> o2.getValue().compareTo(o1.getValue()));
Map.Entry.comparingByValue()
https://www.cnblogs.com/woyaobianfei/p/9187127.html
// LinkedHashMap<T,Integer> sortedMapByValue = new LinkedHashMap<>();
// for (Map.Entry<T, Integer> x : mapList) {
//
// sortedMapByValue.put(x.getKey(), x.getValue());
// }
System.out.println("map after sort by value asc " + sortedMapByValue + " ..");
// return sortedMapByValue;
}
public static<T> LinkedHashMap<T,Integer> sortedMapByValue(Map<T ,Integer> map,
boolean reversed){
//map按照value升序排序
List<Map.Entry<T, Integer>> mapList =
new ArrayList<>(map.entrySet());
// comparingInt reverse
// ,Comparator.reverseOrder()
// mapList.sort(Comparator.comparing(Map.Entry::getValue).reversed());
// Non-static method cannot be referenced from a static context
// mapList.sort(Comparator.comparing(Map.Entry.comparingByValue()).reversed());
// mapList.sort(Comparator.comparing(Map.Entry::comparingByValue));
// mapList.sort(Map.Entry.comparingByValue());
// 逆序的排序
// new Comparable<>()
if(reversed){
mapList.sort((o1,o2)-> o2.getValue().compareTo(o1.getValue()));
}else{
mapList.sort(Comparator.comparingInt(Map.Entry::getValue));
}
// Map.Entry.comparingByValue()
// https://www.cnblogs.com/woyaobianfei/p/9187127.html
LinkedHashMap<T,Integer> sortedMapByValue = new LinkedHashMap<>();
for (Map.Entry<T, Integer> x : mapList) {
sortedMapByValue.put(x.getKey(), x.getValue());
}
// System.out.println("map after sort by value asc " + sortedMapByValue + " ..");
return sortedMapByValue;
}
// 获取LinkedHashMap中的头部元素(最早添加的元素):
//
// 时间复杂度O(1)
public static <K, V> Map.Entry<K, V> getHead(LinkedHashMap<K, V> map) {
return map.entrySet().iterator().next();
}
// https://blog.csdn.net/weixin_34146986/article/details/92718973
// 获取LinkedHashMap中的末尾元素(最近添加的元素):
//
// 时间复杂度O(n)
public static <K, V> Map.Entry<K, V> getTail(LinkedHashMap<K, V> map) {
Iterator<Entry<K, V>> iterator = map.entrySet().iterator();
Entry<K, V> tail = null;
while (iterator.hasNext()) {
tail = iterator.next();
}
return tail;
}
// 通过反射获取LinkedHashMap中的末尾元素:
//
// 时间复杂度O(1),访问tail属性
public static <K, V> Entry<K, V> getTailByReflection(LinkedHashMap<K, V> map)
throws NoSuchFieldException, IllegalAccessException {
Field tail = map.getClass().getDeclaredField("tail");
tail.setAccessible(true);
return (Entry<K, V>) tail.get(map);
}
}
class CntNum {
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
Scanner scanner = new Scanner(System.in);
Map<Integer, Integer> map = new HashMap<>();
while (scanner.hasNextInt()) {
int i = scanner.nextInt();
if (i == 0) break;
if (map.containsKey(i)) {
map.put(i, map.get(i) + 1);
} else {
map.put(i, 1);
}
}
LinkedHashMap<Integer, Integer> integerIntegerLinkedHashMap = MapUtil.sortedMapByValue(map, true);
// integerIntegerLinkedHashMap.
// LinkedHashMap 第一个
// https://blog.csdn.net/weixin_34146986/article/details/92718973
Map.Entry<Integer, Integer> head = MapUtil.getHead(integerIntegerLinkedHashMap);
Map.Entry<Integer, Integer> tailByReflection = MapUtil.getTailByReflection(integerIntegerLinkedHashMap);
// System.out.println("head");
// System.out.println(head);
integerIntegerLinkedHashMap.forEach((k, v) -> {
if (v.equals(head.getValue())) {
// System.out.println(k+" : "+v+" , ");
// System.out.println(String.format("数字: %d , 几次 : %d", k, v));
System.out.println(String.format("Number %d occurred most:%d", k, v));
// Number 4 occurred most:2
//
}
});
// System.out.println("tailByReflection");
// System.out.println(tailByReflection);
// for ()
// integerIntegerLinkedHashMap.for
}
}
class Main{
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
CntNum.main(args);
}
}
//1 2 3 4 6 7 8 9 1 2 3 4
// 0
// map after sort by value asc {6=1, 7=1, 8=1, 9=1, 1=2, 2=2, 3=2, 4=2} ..
// head
// 6=1
// tailByReflection
// 4=2
//1 2 3 4 6 7 8 9 1 2 3 4
// 0
// head
// 1=2
// tailByReflection
// 9=1
//1 2 3 4 6 7 8 9 1 2 3 4
// 0
// head
// 1=2
// 数字: 1 , 几次 : 2
// 数字: 2 , 几次 : 2
// 数字: 3 , 几次 : 2
// 数字: 4 , 几次 : 2
// tailByReflection
// 9=1