【方法1】删除Map中Value反复的记录,而且仅仅保留Key最小的那条记录
介绍
晚上无聊的时候,我做了一个測试题,測试题的大体意思是:删除Map中Value反复的记录,而且仅仅保留Key最小的那条记录。
比如:
I have a map with duplicate values:
("A", "1");
("B", "2");
("C", "2");
("D", "3");
("E", "3");
I would like to the map to have:
("A", "1");
("B", "2");
("D", "3");
- package shuai.study.map;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Set;
- import java.util.TreeMap;
- /**
- * @author shengshu
- *
- */
- public class UniqueMap {
- // Remove repetition from Map, this is core part in this Class
- public static Map<String, String> removeRepetitionFromMap(Map<String, String> map) {
- Set<Entry<String, String>> set = map.entrySet();
- List<Entry<String, String>> list = new ArrayList<Entry<String, String>>(set);
- Collections.sort(list, new Comparator<Entry<String, String>>() {
- @Override
- public int compare(Entry<String, String> entry1, Entry<String, String> entry2) {
- return Integer.valueOf(entry1.getValue().hashCode()) - Integer.valueOf(entry2.getValue().hashCode());
- }
- });
- // list.size() is dynamic change
- for (int index = 0; index < list.size(); index++) {
- String key = list.get(index).getKey();
- String value = list.get(index).getValue();
- int next_index = index + 1;
- if (next_index < list.size()) {
- String next_key = list.get(next_index).getKey();
- String next_value = list.get(next_index).getValue();
- // Remove repetition record whose key is more bigger
- if (value == next_value) {
- if (key.hashCode() < next_key.hashCode()) {
- map.remove(next_key);
- list.remove(next_index);
- } else {
- map.remove(key);
- list.remove(index);
- }
- // Due to removing repetition in List, so index will be reduced
- index--;
- }
- }
- }
- return map;
- }
- // Transfer Map to Sorted Map
- public static Map<String, String> transferToSortedMap(Map<String, String> map) {
- // Define comparator for TreeMap
- Map<String, String> new_sort_map = new TreeMap<String, String>(new Comparator<String>() {
- @Override
- public int compare(String key1, String key2) {
- return key1.hashCode() - key2.hashCode();
- }
- });
- new_sort_map.putAll(map);
- return new_sort_map;
- }
- public static void printMap(Map<String, String> map) {
- Iterator<Entry<String, String>> iterator = map.entrySet().iterator();
- while (iterator.hasNext()) {
- Entry<String, String> entry = iterator.next();
- String key = entry.getKey();
- String value = entry.getValue();
- System.out.println(key + " --> " + value);
- }
- }
- public static void main(String[] args) {
- Map<String, String> map = new HashMap<String, String>();
- map.put("A", "1");
- map.put("B", "2");
- map.put("C", "2");
- map.put("D", "3");
- map.put("E", "3");
- Map<String, String> new_map = UniqueMap.removeRepetitionFromMap(map);
- // new_sort_map is what we want
- Map<String, String> new_sort_map = UniqueMap.transferToSortedMap(new_map);
- // Print new_sort_map
- UniqueMap.printMap(new_sort_map);
- }
- }
【方法1】删除Map中Value反复的记录,而且仅仅保留Key最小的那条记录的更多相关文章
- 【方法2】删除Map中Value反复的记录,而且仅仅保留Key最小的那条记录
依据guigui111111的建议:先把Map按Key从大到小排序,然后再把Key和Value互换.这也是一种非常好的思路,我写了一下代码,顺便贴上来,供大家參考与分享. package shuai. ...
- 删除oracle 表中重复数据sql语句、保留rowid最小的一条记录
delete from tablename a where rowid > ( select min(rowid) from table_name b where b.id = a.id and ...
- 删除重复数据并保留id最小的一条记录
delete from test where id not in ( select a.id from (select min(id) as id from test group by form_i ...
- Map去重,去重value相同的元素,保留key最小的那个值
Map<Integer,String>,Integer代表时间撮,String代表文本信息去重函数:就是删除Map中value相同的元素,只保留key最小的那个元素 public stat ...
- Java之——删除ArrayList中的反复元素的2种方法
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47414935 ArrayList是Java中最经常使用的集合类型之中的一个.它同意 ...
- js删除map中元素
js中删除map中元素后,map的长度不变,这时需要我们自己处理 delete vacc[0]; delete vacc[1]; ClearNullArr(vacc); //清除vacc中的null值 ...
- mysql删除表中重复数据,只保留一个最小的id的记录
语句: delete from table1 where id not in (select minid from (select min(id) as minid from table1 group ...
- 初探oracle删除重复记录,只保留rowid最小的记录
如题,初探oracle删除重复记录,只保留rowid最小的记录(rowid可以反映数据插入到数据库中的顺序) 一.删除重复记录可以使用多种方法,如下只是介绍了两种方法(exist和in两种). 1.首 ...
- oracle删除重复记录,只保留rowid最小的记录
初探oracle删除重复记录,只保留rowid最小的记录 如题,初探oracle删除重复记录,只保留rowid最小的记录(rowid可以反映数据插入到数据库中的顺序) 一.删除重复记录可以使用多种 ...
随机推荐
- 浙江省第十二届省赛 Beauty of Array(思维题)
Description Edward has an array A with N integers. He defines the beauty of an array as the summatio ...
- UVALive 3882.And Then There Was One-约瑟夫问题(递推)
And Then There Was One Time limit: 3.000 seconds Let’s play a stone removing game. Initially, n ston ...
- 洛谷 P1598 垂直柱状图【字符串】
题目描述 写一个程序从输入文件中去读取四行大写字母(全都是大写的,每行不超过72个字符),然后用柱状图输出每个字符在输入文件中出现的次数.严格地按照输出样例来安排你的输出格式. 输入输出格式 输入格式 ...
- HDU 1236 排名(结构体+排序)
今天的上机考试虽然有实时的Ranklist,但上面的排名只是根据完成的题数排序,没有考虑 每题的分值,所以并不是最后的排名.给定录取分数线,请你写程序找出最后通过分数线的 考生,并将他们的成绩按降序打 ...
- sed 手册 http://www.gnu.org/software/sed/manual/sed.html
http://www.gnu.org/software/sed/manual/sed.html
- [BZOJ 3233] 找硬币
Link: BZOJ 3233 传送门 Solution: 在本蒟蒻看来算是一道比较神的$dp$了 一开始转移方程都没看出来…… 首先,如果确定了最大面值,是能推出其他面值的所有可能值的 从而发现最大 ...
- [BZOJ 1833] 数字计数
Link: BZOJ 1833 传送门 Solution: 比较明显的数位DP 先预处理出1~9和包括前导0的0的个数:$pre[i]=pre[i-1]*10+10^{digit-1}$ (可以分为首 ...
- 【可持久化Trie】【set】bzoj3166 [Heoi2013]Alo
枚举每个数,计算以其为次大数的最大区间,显然,只需要用这个区间的答案 对 答案进行更新即可. 找到每个数右侧.左侧第1.2个比它大的数,然后分类讨论一下即可. 找到的过程中把数sort以后,从大到小把 ...
- C语言计算器
地址: https://wenda.so.com/q/1371173683061754?src=140
- NDK之HelloWord!
使用工具:Android Studio 2.2.2 1. 配置local.properties添加NDK路径. 效果:当然,你也可以手输写进去. 2. 项目gradle.properties追加 ...