1、求出字符串abbddbb中重复最多的数据
代码:
String ss = "abbddbb"; char[] chars = ss.toCharArray(); TreeMap<Character,Integer> treeMap = new TreeMap<>(Map.Entry<Character,Integer>( new Comparator<Map.Entry<Character,Integer>>() { @Override public int compare(Map.Entry<Character,Integer> e1, Map.Entry<Character,Integer> e2) { int res = e1.getValue().compareTo(e2.getValue()); return res != 0 ? res : 1; } }); for (int i = 0; i < chars.length; i++) { if(treeMap.isEmpty()){ treeMap.put(chars[0],1); }else{ if(treeMap.containsKey(chars[i])){ treeMap.put(chars[i],treeMap.get(chars[i])+1); }else{ treeMap.put(chars[i],1); } } } log.info("treeMap-->>"+treeMap);