java map的重复键值的排序

1. 概述

    在java map中,一般有重复的key是使用IdentityHashMap存储,可以自动按键进行排序的是TreeMap,但是TreeMap不能使用重复的键,并且不能按照值进行排序,这就需要我们自己定义排序规则进行处理,下面记一下我做的重复键值排序,留个备份

 

2. 代码

package com.taobao.tpif.common;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.taobao.tpif.common.KeyValueComparator.Order;
import com.taobao.tpif.common.KeyValueComparator.Type;

public class MapSorter {

	public static void main(String[] args) {
		IdentityHashMap<Double, String> map = new IdentityHashMap<Double, String>();
		map.put(10.1, "aaaa");
		map.put(10.1, "bbbb");
		map.put(10.1, "cccc");
		map.put(10.08, "dddd");
		map.put(10.02, "eeee");
		map.put(10.08, "aaaa");

		System.out.println("----------[ [ [ 排序前  ] ] ]----------\n");

		for (Entry<Double, String> entry : map.entrySet()) {
			System.out.println("\t" + entry.getKey() + "\t\t" + entry.getValue());
		}

		System.out.println("\n----------[ [ [ 按键降序 ] ] ]----------\n");

		List<Map.Entry<Double, String>> list1 = new ArrayList<Map.Entry<Double, String>>(map.entrySet());
		Collections.sort(list1, new KeyValueComparator(Type.KEY, Order.DESC));

		for (Entry<Double, String> entry : list1) {
			System.out.println("\t" + entry.getKey() + "\t\t" + entry.getValue());
		}
		
		System.out.println("\n----------[ [ [ 按值升序  ] ] ]----------\n");
		
		List<Map.Entry<Double, String>> list2 = new ArrayList<Map.Entry<Double, String>>(map.entrySet());
		Collections.sort(list2, new KeyValueComparator(Type.VALUE, Order.ASC));

		for (Entry<Double, String> entry : list2) {
			System.out.println("\t" + entry.getKey() + "\t\t" + entry.getValue());
		}

		System.out.println("\n----------[ [ [ 结束啦 ] ] ]----------\n");
	}
}

class KeyValueComparator implements Comparator<Map.Entry<Double, String>> {
	enum Type {
		KEY, VALUE;
	}

	enum Order {
		ASC, DESC
	}

	private Type type;
	private Order order;

	public KeyValueComparator(Type type, Order order) {
		this.type = type;
		this.order = order;
	}

	@Override
	public int compare(Entry<Double, String> o1, Entry<Double, String> o2) {
		switch (type) {
		case KEY:
			switch (order) {
			case ASC:
				return o1.getKey().compareTo(o2.getKey());
			case DESC:
				return o2.getKey().compareTo(o1.getKey());
			default:
				throw new RuntimeException("顺序参数错误");
			}
		case VALUE:
			switch (order) {
			case ASC:
				return o1.getValue().compareTo(o2.getValue());
			case DESC:
				return o2.getValue().compareTo(o1.getValue());
			default:
				throw new RuntimeException("顺序参数错误");
			}
		default:
			throw new RuntimeException("类型参数错误");
		}
	}

}

 

3. 输出

----------[ [ [ 排序前  ] ] ]------------

	10.08		aaaa
	10.1		cccc
	10.08		dddd
	10.1		aaaa
	10.02		eeee
	10.1		bbbb

----------[ [ [ 按键降序 ] ] ]----------

	10.1		cccc
	10.1		aaaa
	10.1		bbbb
	10.08		aaaa
	10.08		dddd
	10.02		eeee

----------[ [ [ 按值升序  ] ] ]----------

	10.08		aaaa
	10.1		aaaa
	10.1		bbbb
	10.1		cccc
	10.08		dddd
	10.02		eeee

----------[ [ [ 结束啦 ] ] ]------------

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值