【方法1】删除Map中Value重复的记录,并且只保留Key最小的那条记录

简介: <p>介绍</p> <p>    晚上无聊的时候,我做了一个测试题,测试题的大体意思是:删除Map中Value重复的记录,并且只保留Key最小的那条记录。</p> <p>例如:</p> <p>I have a map with duplicate values:</p> <p>    ("A", "1");<br>     ("B", "2");<br>     ("C", "

介绍

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


相关文章
|
3月前
|
存储
`map()`方法在什么场景下会比 `forEach()`方法更高效?
综上所述,当需要对数组元素进行复杂的转换并生成新数组、进行链式调用和函数式编程、处理元素之间存在明确映射关系的情况以及与其他数组方法结合使用时,`map()`方法比`forEach()`方法更高效,能够使代码更加简洁、清晰和易于维护。
83 32
WK
|
3月前
|
Python
Python中format_map()方法
在Python中,`format_map()`方法用于使用字典格式化字符串。它接受一个字典作为参数,用字典中的键值对替换字符串中的占位符。此方法适用于从字典动态获取值的场景,尤其在处理大量替换值时更为清晰和方便。
WK
142 36
|
3月前
|
存储 JavaScript 前端开发
如何选择使用`map()`方法和`forEach()`方法?
选择使用`map()`方法还是`forEach()`方法主要取决于操作的目的、是否需要返回值、代码的可读性和维护性等因素。在实际开发中,需要根据具体的业务需求和场景来灵活选择合适的方法,以实现更高效、更易读和更易维护的代码。
45 3
|
3月前
|
存储 Java API
Java交换map的key和value值
通过本文介绍的几种方法,可以在Java中实现Map键值对的交换。每种方法都有其优缺点,具体选择哪种方法应根据实际需求和场景决定。对于简单的键值对交换,可以使用简单遍历法或Java 8的Stream API;对于需要处理值不唯一的情况,可以使用集合存储或Guava的Multimap。希望本文对您理解和实现Java中的Map键值对交换有所帮助。
68 1
|
4月前
|
存储 JavaScript 前端开发
《进阶篇第8章:vuex》包括理解vuex、安装vuex、搭建vuex环境、四个map方法的使用、模块化+名命空间
《进阶篇第8章:vuex》包括理解vuex、安装vuex、搭建vuex环境、四个map方法的使用、模块化+名命空间
35 3
|
4月前
|
Java
vue2知识点:vuex中四个map方法的使用,包括:mapState、mapGetters、mapMutations、mapActions
vue2知识点:vuex中四个map方法的使用,包括:mapState、mapGetters、mapMutations、mapActions
235 1
|
5月前
|
存储 JavaScript 前端开发
`forEach()`方法和`map()`方法哪个执行效率更高?
`forEach()`方法和`map()`方法哪个执行效率更高?
|
5月前
|
JavaScript 前端开发
JavaScript Array map() 方法
JavaScript Array map() 方法
|
5月前
数组方法中的`forEach()`方法和`map()`方法有什么区别?
数组方法中的`forEach()`方法和`map()`方法有什么区别?
|
5月前
|
JavaScript 前端开发
JavaScript 中 五种迭代数组的方法 every some map filter forEach
本文介绍了JavaScript中五种常用数组迭代方法:every、some、filter、map和forEach,并通过示例代码展示了它们的基本用法和区别。