介绍

晚上无聊的时候,我做了一个測试题,測试题的大体意思是:删除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. Spring的自动装配

    在Spring中对自定义的引用类型注入时可以实现自动赋值.但是必须依赖set方法:  自动装配功能有两种: <!-- autowire:"byType" --根据class匹 ...

  2. Hive知识

    HIVEQL CREATE DATABASE financials(创建数据库) SHOW DATABASES(显示数据库) SHOW TABLES IN 数据库(列出数据库的所有表) SHOW DA ...

  3. 用户点击行为实时分析系统spark

    系统设计技术有:Hadoop2.xZookeeperFlumeHiveHbaseKafkaSpark2.xSpark StreamingStructured StreamingMySQLHueJava ...

  4. centos7下自定义服务启动和自动执行脚本

    systemctl list-units --type=service #查看所有已启动的服务 systemctl enable httpd.service #加入开机自启动服务 systemctl ...

  5. [BZOJ 2752] 高速公路

    Link: BZOJ 2752 传送门 Solution: 虽然有期望,但实际上就是除了个总数…… 此题计算总代价明显还是要使用对每个$w_i$计算贡献的方式: $w_i的贡献为w_i*(i-l+1) ...

  6. 洛谷 P2742 [USACO5.1]圈奶牛Fencing the Cows

    题目描述 农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏.他建造的围栏必须包括他的奶牛喜欢吃草的所有地点.对于给出的这些地点的坐标,计算最短的能够围住这些点的围栏的长度. 输入输出格式 输入 ...

  7. POJ 2932 Coneology(扫描线)

    [题目链接] http://poj.org/problem?id=2932 [题目大意] 给出N个两两没有公共点的圆,求所有不包含于其它圆内部的圆 [题解] 我们计算出所有点在圆心所有y位置的x值, ...

  8. java web(学习笔记)项目路径问题

    最近刚接触java web特别是是关于项目路径这一块很晕,就把自己遇到的一些疑惑和理解写下来. 首先贴上路径,这里用的是eclipse. 其中我们要注意看WebContent目录,这是web程序的根目 ...

  9. 3.2常用类(java学习笔记)String与StringBuffer

    一.String String又称不可变字符序列. 我们看JDK源码中用于字符存储的数组有final修饰,final修饰变量就代表变量不能改变. 我们可以看API文档中对String的描述. Stri ...

  10. ios-真机调试出错信息

    更新证书错误Code Sign error: Provisioning profile ‘XXXX'can't be found                   在Xcode中当你在更新了你得证书 ...