自定义Map.Entry的Comperator实现字符频率降序排序
leetCode (#451,middle) java实现
class Solution {
public String frequencySort(String s) {
Map<Character,Integer> map = new HashMap<>();
//放入map
for(char c :s.toCharArray()){
if(map.containsKey(c)){
map.put(c,map.get(c)+1);
}else
map.put(c,1);
}
//根据value的大小排序
Comparator<Map.Entry<Character,Integer>> comparator =
new Comparator<Map.Entry<Character, Integer>>() {
@Override
public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) {
return o2.getValue() - o1.getValue();
}
};
//放入链表
List<Map.Entry<Character,Integer>> list = new ArrayList<>(map.entrySet());
//根据实现的comp排序
Collections.sort(list,comparator);
//定义数组
StringBuilder sb = new StringBuilder();
for(Map.Entry<Character,Integer> entry : list){
for(int i = 0 ; i < entry.getValue();i++){
sb.append(entry.getKey());
}
}
return sb.toString();
}
}
自定义Map.Entry的Comperator实现字符频率降序排序的更多相关文章
- 用shell处理以下内容 1、按单词出现频率降序排序! 2、按字母出现频率降序排序! the squid project provides a number of resources toassist users design,implement and support squid installations. Please browsethe documentation and support
此题目有多种解法,sed.awk.tr等等,都可以解决此题,命令运用灵活多变. 编写shell脚本no_20.sh 解法1: #!/bin/bash ###-------------CopyRight ...
- shell脚本,按字母出现频率降序排序。
[root@localhost oldboy]# cat file the squid project provides a number of resources toassist users de ...
- shell脚本,按单词出现频率降序排序。
[root@localhost oldboy]# cat file the squid project provides a number of resources toassist users de ...
- java算法面试题:有一个字符串,其中包含中文字符、英文字符和数字字符,请统计和打印出各个字符的个数 按值的降序排序,如果值相同则按键值的字母顺序
package com.swift; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; publi ...
- 集合(一)-Java中Arrays.sort()自定义数组的升序和降序排序
默认升序 package peng; import java.util.Arrays; public class Testexample { public static void main(Stri ...
- C语言:对长度为7的字符串,除首尾字符外,将其余5个字符按ASCII降序排序。-计算并输出3~n之间所有素数的平方根之和。
//对长度为7的字符串,除首尾字符外,将其余5个字符按ASCII降序排序. #include <stdio.h> #include <ctype.h> #include < ...
- Java 将Map按Value值降序排列
1 /** 2 * 将集合按照降序排列-FLOAT 3 * @param nowPartTwoData 4 * @return 5 */ 6 private static List<Map.En ...
- Map接口,Map.Entry,hashMap类,TreeMap类,WeakHashMap。
Collection接口之前接触过,每次保存的对象是一个对象,但是在map中保存的是一对对象,是以key->value形式保存的. 定义: public interface Map<K,V ...
- 10 HashMap,Map.Entry,LinkedHashMap,TreeMap,Hashtable,Collections类
Map集合的功能概述 添加功能 * V put(K key,V value):添加元素. * 如果键是第一次存储,就直接存储元素,返回null * 如果键不 ...
随机推荐
- Confluence 6 安全相关问题提交链接
找到和报告安全漏洞 Atlassian 有关安全漏洞的报告细节,请参考如何报告一个安全问题(How to Report a Security Issue)链接. 发布 Confluence 安全公共 ...
- Confluence 6 配置数字格式
在 Confluence 中使用了 2 种数字格式: 整形数字格式.例如: ############### 小数数字格式.例如:###############.########## Confluenc ...
- Confluence 6 重构索引缓慢
你的索引构建是否需要很长时间?索引构建需要的时间是由下面的一些因素确定的: 你 Confluence 安装实例中的页面数量. 附件的数量,类型和大小. Confluence 安装实例可用的内存大小. ...
- Java的动手动脑(七)
日期:2018.11.18 博客期:025 星期日 Part 1:使用 Files.walkFileTree()来找出指定文件夹下大小大于1KB的文件 package temp; import jav ...
- ionic3 打包Xcode 9 Swift Language Version (SWIFT_VERSION) Ask 报错
解决方案 选择4.0 然后报错17个,类似以下这样的错误 'AVMediaTypeVideo' has been renamed to 'AVMediaType.video' 根据提示更改 AVMed ...
- J2SE基础小结
1. 九种基本数据类型的大小,以及他们的封装类. 类型 基本类型 大小(字节) 默认值 封装类 整数型 byte 1 (byte)0 Byte short 2 (short)0 Short int 4 ...
- map函数和reduce函数的区别
①从参数方面来讲:map()函数: map()包含两个参数,第一个是参数是一个函数,第二个是序列(列表或元组).其中,函数(即map的第一个参数位置的函数)可以接收一个或多个参数.reduce()函数 ...
- Java 获取当前系统的时间
获取当前系统的时间,每隔一秒,打印一次. import java.util.Date; public class TestDate { public static void main(String[] ...
- inline namespace
无意中看到C++11中的新特性inline namespace, 先附上官方的解释 Inline namespace The inline namespace mechanism is intende ...
- python 在WINDOS虚拟环境部署
#查看电脑的版本 C:\Users\lys>pip -V pip 8.1.1 from e:\python\python3.5\lib\site-packages (python 3.5) #安 ...