【方法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);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值