介绍

晚上无聊的时候,我做了一个測试题,測试题的大体意思是:删除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最小的那条记录的更多相关文章

  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. jquery禁用select和取消禁用

    $("#id").attr("disabled","disabled"); $("#id").removeAttr(&q ...

  2. AC日记——[Noi2011]阿狸的打字机 bzoj 2434

    2434 思路: 构建ac自动机: 抽离fail树: 根据字符串建立主席树: 在线处理询问: 询问x在y中出现多少次,等同于y有多少字母的fail能走到x: 1a,hahahahah: 代码: #in ...

  3. mysql 文本搜索

    全文本搜索 MySQL支持几种基本的数据库引擎,但并非所有的引擎都支持全文本搜索.两个最常使用的引擎为MyISAM和InnoDB,前者支持全文本搜索,后者就不支持. 理解全文本搜索 在前面的学习中,我 ...

  4. Oracle alter table modify column Syntax example

    http://www.dba-oracle.com/t_alter_table_modify_column_syntax_example.htm For complete tips on Oracle ...

  5. Fiddler 高级用法:Fiddler Script 与 HTTP 断点调试

    转载自 https://my.oschina.net/leejun2005/blog/399108 1.Fiddler Script 1.1 Fiddler Script简介 在web前端开发的过程中 ...

  6. BZOJ 1878 [SDOI2009]HH的项链(扫描线+树状数组)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1878 [题目大意] 给出一个数列,给出m个查询,每次查询一个区间中不相同的数字个数 [ ...

  7. 【kd-tree】bzoj1176 [Balkan2007]Mokia

    裸题不多说,注意在sqrt(n*log(n))次插入后重构树以保持深度. #include<cstdio> #include<cmath> #include<algori ...

  8. hadoop中URI理解

    1)在编写MR程序的时候经常会有如下代码: String uri=“....”: Configuration conf=new Configuration(): FileSystem fs=FileS ...

  9. 使用UNetbootin工具制作的CentOS 6.9镜像U盘在启动安装过程中出现:unable to read package metadata.this may be due to a missing repodata directory

    1.制作: 2.重命名文件 (前) (后) 这些文件是拷贝另一个得来的,并且后面的命名是根据repomd.xm这个文件来的. 参考: http://blog.csdn.net/maijunjin/ar ...

  10. asp.net 域名注册查询接口 支持批量后缀查询

    最近在完成公司网站www.xuhongkj.com的时候,需要用到域名查询的功能,网上查了一些资料,几乎都是ASP版的,而且功能有限,不能满足我的要求. 百度后,结合网上的例子,整理出了该功能! as ...