介绍

晚上无聊的时候,我做了一个測试题,測试题的大体意思是:删除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. Nodejs调用Aras Innovator服务,处理AML并返回AML

    公司已经布署了Aras Innovator服务器,如果需要与Aras Innovator进行交互,需要进行自主开发程序,例如使用C#.VB.Java等,都是可以与它进行交互的 C#:调用Aras In ...

  2. python 如何放心干净的卸载模块

    windows系统: C:\selenium-2.43.0>python setup.py install --record ./record.txt C:\selenium-2.43.0> ...

  3. HDU 2567 寻梦(字符串,插入)

    #include<iostream> #include<stdio.h> #include<string.h> #include<cmath> usin ...

  4. HDU 4528 小明系列故事――捉迷藏

    广搜. 根据题意,可以知道状态总共有$4*n*m$种.每一个位置四种状态:两个都没有发现:发现$E$没发现$D$:发现$D$没发现$E$:两个都发现. 每次移动的花费都是$1$,队列里面状态的费用是单 ...

  5. 51nod 子序列的个数(动态规划)

    子序列的个数 给定一个正整数序列,序列中元素的个数和元素值大小都不超过105, 求其所有子序列的个数.注意相同的只算一次:例如 {1,2,1}有子序列{1} {2} {1,2} {2,1}和{1,2, ...

  6. 【找规律】Codeforces Round #392 (Div. 2) C. Unfair Poll

    C. Unfair Poll time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. 【费用流】bzoj1520 [POI2006]Szk-Schools

    注意:建图的时候,一定要把旧标号相同的分开. #include<cstdio> #include<algorithm> #include<cstring> #inc ...

  8. [POI2018]Plan metra

    题目大意: 一棵$n(n\le5\times10^5)$个结点的树,每条边的边权均为正整数,告诉你$2\sim n-1$号结点到$1$号点和$n$号点的距离$d1[i]$和$d2[i]$.求是否存在这 ...

  9. Java高级架构师(一)第07节:远程使用以及冲突解决

  10. ueditor-angular(百度编辑器angular版)中,关于插入图片后不操作,图片无法正常提交的问题;

    由于项目后台管理页面中需要使用编辑器,所以选择了百度编辑器这个常用的东西: 本人是小白,第一次使用百度编辑器,具体的配置是由后台的兄弟完成的,还给了demo,所以在项目开发中也一直都没发现什么问题: ...