【方法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 ...
- 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可以反映数据插入到数据库中的顺序) 一.删除重复记录可以使用多种 ...
- MySQL 查询重复数据,删除重复数据保留id最小的一条作为唯一数据
开发背景: 最近在做一个批量数据导入到MySQL数据库的功能,从批量导入就可以知道,这样的数据在插入数据库之前是不会进行重复判断的,因此只有在全部数据导入进去以后在执行一条语句进行删除,保证数据唯一性 ...
- 一个表中的id有多个记录,把所有这个id的记录查出来,并显示共有多少条记录数
一个表中的id有多个记录,把所有这个id的记录查出来,并显示共有多少条记录数 select id ,Count(*) from table_name group by id having count( ...
随机推荐
- maven-shade-plugin
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> ...
- iOS9 适配
iOS适配的相关内容的整理 之前iOS开发者一直很庆幸自己不用像安卓开发者那样适配各种不同类型的机型,但如今随着iPhone各种机型的改变,适配也成了我们开发中必须会的内容了.首先我们来了解一下对于不 ...
- Perl 正则表达式
匹配:m/<regexp>;/ (还可以简写为 /<regexp>;/ ,略去 m)替换:s/<pattern>;/<replacement>;/转化: ...
- .net 泛型运用
DAL层 private DbContext MyContext; public BaseRepository(DbContext context) { MyContext = context; } ...
- git克隆项目到一个非空目录
这只是记录: 1. 进入非空目录,假设是 /workdir/proj1 2. git clone --no-checkout https://git.oschina.net/NextApp/platf ...
- 推荐:根据ISBN号查询图书信息的API - 豆瓣API
转帖,出处:http://blog.csdn.net/berryreload/article/details/9126645 版权声明:本文为博主原创文章,未经博主允许不得转载. 找了半天,还是豆瓣的 ...
- 排序 归并排序&;逆序对
void MergeArray(int cry[],int temp[],int begin,int middle,int end) { int i=begin; int j=middle+1; in ...
- Mysql 的存储过程和存储函数
优点: v 提高安全性 v 简化mysql查询 v 减轻带宽负担 缺点: v 增加服务器负担 v 对我们开发者来说,难度大一点 PHP中的函数 Function funname(参数){ //函数体 ...
- 《Data-Intensive Text Processing with mapReduce》读书笔记之一:前言
暑假闲得蛋痒,混混沌沌,开始看<Data-Intensive Text Processing with mapReduce>,尽管有诸多单词不懂,还好六级考多了,虽然至今未过:再加上自己当 ...
- java 报表到excel
现加个jar包 http://pan.baidu.com/s/1boe5kXh vfp8 然后代码 package makeReportToExcel; import java.io.File; ...