介绍

晚上无聊的时候,我做了一个測试题,測试题的大体意思是:删除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");

  1. package shuai.study.map;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Comparator;
  6. import java.util.HashMap;
  7. import java.util.Iterator;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.Map.Entry;
  11. import java.util.Set;
  12. import java.util.TreeMap;
  13.  
  14. /**
  15. * @author shengshu
  16. *
  17. */
  18. public class UniqueMap {
  19.  
  20. // Remove repetition from Map, this is core part in this Class
  21. public static Map<String, String> removeRepetitionFromMap(Map<String, String> map) {
  22. Set<Entry<String, String>> set = map.entrySet();
  23.  
  24. List<Entry<String, String>> list = new ArrayList<Entry<String, String>>(set);
  25.  
  26. Collections.sort(list, new Comparator<Entry<String, String>>() {
  27. @Override
  28. public int compare(Entry<String, String> entry1, Entry<String, String> entry2) {
  29. return Integer.valueOf(entry1.getValue().hashCode()) - Integer.valueOf(entry2.getValue().hashCode());
  30. }
  31. });
  32.  
  33. // list.size() is dynamic change
  34. for (int index = 0; index < list.size(); index++) {
  35. String key = list.get(index).getKey();
  36. String value = list.get(index).getValue();
  37.  
  38. int next_index = index + 1;
  39.  
  40. if (next_index < list.size()) {
  41. String next_key = list.get(next_index).getKey();
  42. String next_value = list.get(next_index).getValue();
  43.  
  44. // Remove repetition record whose key is more bigger
  45. if (value == next_value) {
  46. if (key.hashCode() < next_key.hashCode()) {
  47. map.remove(next_key);
  48. list.remove(next_index);
  49. } else {
  50. map.remove(key);
  51. list.remove(index);
  52. }
  53.  
  54. // Due to removing repetition in List, so index will be reduced
  55. index--;
  56. }
  57. }
  58. }
  59.  
  60. return map;
  61. }
  62.  
  63. // Transfer Map to Sorted Map
  64. public static Map<String, String> transferToSortedMap(Map<String, String> map) {
  65. // Define comparator for TreeMap
  66. Map<String, String> new_sort_map = new TreeMap<String, String>(new Comparator<String>() {
  67. @Override
  68. public int compare(String key1, String key2) {
  69. return key1.hashCode() - key2.hashCode();
  70. }
  71. });
  72.  
  73. new_sort_map.putAll(map);
  74.  
  75. return new_sort_map;
  76. }
  77.  
  78. public static void printMap(Map<String, String> map) {
  79. Iterator<Entry<String, String>> iterator = map.entrySet().iterator();
  80.  
  81. while (iterator.hasNext()) {
  82. Entry<String, String> entry = iterator.next();
  83.  
  84. String key = entry.getKey();
  85. String value = entry.getValue();
  86.  
  87. System.out.println(key + " --> " + value);
  88. }
  89. }
  90.  
  91. public static void main(String[] args) {
  92. Map<String, String> map = new HashMap<String, String>();
  93. map.put("A", "1");
  94. map.put("B", "2");
  95. map.put("C", "2");
  96. map.put("D", "3");
  97. map.put("E", "3");
  98.  
  99. Map<String, String> new_map = UniqueMap.removeRepetitionFromMap(map);
  100.  
  101. // new_sort_map is what we want
  102. Map<String, String> new_sort_map = UniqueMap.transferToSortedMap(new_map);
  103.  
  104. // Print new_sort_map
  105. UniqueMap.printMap(new_sort_map);
  106. }
  107. }

【方法1】删除Map中Value反复的记录,而且仅仅保留Key最小的那条记录的更多相关文章

  1. 【方法2】删除Map中Value反复的记录,而且仅仅保留Key最小的那条记录

    依据guigui111111的建议:先把Map按Key从大到小排序,然后再把Key和Value互换.这也是一种非常好的思路,我写了一下代码,顺便贴上来,供大家參考与分享. package shuai. ...

  2. 删除oracle 表中重复数据sql语句、保留rowid最小的一条记录

    delete from tablename a where rowid > ( select min(rowid) from table_name b where b.id = a.id and ...

  3. 删除重复数据并保留id最小的一条记录

    delete from  test where id not in ( select a.id from (select min(id) as id from test group by form_i ...

  4. Map去重,去重value相同的元素,保留key最小的那个值

    Map<Integer,String>,Integer代表时间撮,String代表文本信息去重函数:就是删除Map中value相同的元素,只保留key最小的那个元素 public stat ...

  5. Java之——删除ArrayList中的反复元素的2种方法

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47414935 ArrayList是Java中最经常使用的集合类型之中的一个.它同意 ...

  6. js删除map中元素

    js中删除map中元素后,map的长度不变,这时需要我们自己处理 delete vacc[0]; delete vacc[1]; ClearNullArr(vacc); //清除vacc中的null值 ...

  7. mysql删除表中重复数据,只保留一个最小的id的记录

    语句: delete from table1 where id not in (select minid from (select min(id) as minid from table1 group ...

  8. 初探oracle删除重复记录,只保留rowid最小的记录

    如题,初探oracle删除重复记录,只保留rowid最小的记录(rowid可以反映数据插入到数据库中的顺序) 一.删除重复记录可以使用多种方法,如下只是介绍了两种方法(exist和in两种). 1.首 ...

  9. oracle删除重复记录,只保留rowid最小的记录

    初探oracle删除重复记录,只保留rowid最小的记录   如题,初探oracle删除重复记录,只保留rowid最小的记录(rowid可以反映数据插入到数据库中的顺序) 一.删除重复记录可以使用多种 ...

随机推荐

  1. 浙江省第十二届省赛 Beauty of Array(思维题)

    Description Edward has an array A with N integers. He defines the beauty of an array as the summatio ...

  2. 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 ...

  3. 洛谷 P1598 垂直柱状图【字符串】

    题目描述 写一个程序从输入文件中去读取四行大写字母(全都是大写的,每行不超过72个字符),然后用柱状图输出每个字符在输入文件中出现的次数.严格地按照输出样例来安排你的输出格式. 输入输出格式 输入格式 ...

  4. HDU 1236 排名(结构体+排序)

    今天的上机考试虽然有实时的Ranklist,但上面的排名只是根据完成的题数排序,没有考虑 每题的分值,所以并不是最后的排名.给定录取分数线,请你写程序找出最后通过分数线的 考生,并将他们的成绩按降序打 ...

  5. sed 手册 http://www.gnu.org/software/sed/manual/sed.html

    http://www.gnu.org/software/sed/manual/sed.html

  6. [BZOJ 3233] 找硬币

    Link: BZOJ 3233 传送门 Solution: 在本蒟蒻看来算是一道比较神的$dp$了 一开始转移方程都没看出来…… 首先,如果确定了最大面值,是能推出其他面值的所有可能值的 从而发现最大 ...

  7. [BZOJ 1833] 数字计数

    Link: BZOJ 1833 传送门 Solution: 比较明显的数位DP 先预处理出1~9和包括前导0的0的个数:$pre[i]=pre[i-1]*10+10^{digit-1}$ (可以分为首 ...

  8. 【可持久化Trie】【set】bzoj3166 [Heoi2013]Alo

    枚举每个数,计算以其为次大数的最大区间,显然,只需要用这个区间的答案 对 答案进行更新即可. 找到每个数右侧.左侧第1.2个比它大的数,然后分类讨论一下即可. 找到的过程中把数sort以后,从大到小把 ...

  9. C语言计算器

    地址:  https://wenda.so.com/q/1371173683061754?src=140

  10. NDK之HelloWord!

    使用工具:Android Studio 2.2.2 1. 配置local.properties添加NDK路径.    效果:当然,你也可以手输写进去. 2. 项目gradle.properties追加 ...