(java基础知识)HashMap排序,Comparator接口详解

对于List,可以调用Collections工具类的sort()方法,直接进行排序。HashMap,就没这么幸福了。。
其实,只要了解了Comparator这个接口之后,HashMap的排序也就不难了,无论是根据key,还是根据value排序。
这个接口也很简单,只有一个抽象方法int compare();需要我们去实现。这个方法,就是实现你制订的比较规则。(其实这个接口里面还有一个方法boolean equals()但是API里面说不是现在这个方法也总是安全的。。这个方法只有当你的程序有两个以上的Comparator的实现之后才用的上,用于比较)
具体去实现的时候,有两种常用途径。
1、匿名内部类
这是最简单实用的一种方法了。为了方便实现根据key和value的排序,我把这个map设计成Map<Integer,Integer>

  1. import java.util.*;
  2. class Test
  3. {
  4. public static void main(String[] args)
  5. {
  6. Map<Integer,Integer> map=new HashMap<>();
  7. map.put(1,2);
  8. map.put(8,3);
  9. map.put(3,4);
  10. map.put(5,7);
  11. map.put(4,6);
  12. map.put(9,8);
  13. //这一步很关键,因为要利用sort(List<T> list,Comparator<? super T> c)方法。
  14. List<Map.Entry<Integer,Integer>> list=new ArrayList<>();
  15. //把map转化为Map.Entry然后放到用于排序的list里面
  16. list.addAll(map.entrySet());
  17. //开始排序了,利用匿名内部类,直接创建Comparator接口的对象,不用再写实现类了。
  18. Collections.sort(list,new Comparator<Map.Entry<Integer,Integer>>()
  19. {
  20. public int compare(Map.Entry<Integer,Integer> m,Map.Entry<Integer,Integer> n)
  21. {
  22. //比较的规则,这是根据key,从小到大排序。如果从大到小就调换m和n的位置。
  23. return m.getKey()-n.getKey();
  24. }
  25. });
  26. //遍历在list中排序之后的HashMap
  27. for(Iterator<Map.Entry<Integer,Integer>> it=list.iterator();it.hasNext();)
  28. {
  29. System.out.println(it.next());
  30. }
  31. }
  32. }
import java.util.*;
class Test
{
	public static void main(String[] args) 
	{
		Map<Integer,Integer> map=new HashMap<>();
		map.put(1,2);
		map.put(8,3);
		map.put(3,4);
		map.put(5,7);
		map.put(4,6);
		map.put(9,8);
		//这一步很关键,因为要利用sort(List<T> list,Comparator<? super T> c)方法。
		List<Map.Entry<Integer,Integer>> list=new ArrayList<>();
		//把map转化为Map.Entry然后放到用于排序的list里面
		list.addAll(map.entrySet());
		//开始排序了,利用匿名内部类,直接创建Comparator接口的对象,不用再写实现类了。
		Collections.sort(list,new Comparator<Map.Entry<Integer,Integer>>()
		{
			public int compare(Map.Entry<Integer,Integer> m,Map.Entry<Integer,Integer> n)
			{
				//比较的规则,这是根据key,从小到大排序。如果从大到小就调换m和n的位置。
				return m.getKey()-n.getKey();
			}
		});
		//遍历在list中排序之后的HashMap
		for(Iterator<Map.Entry<Integer,Integer>> it=list.iterator();it.hasNext();)
		{
			System.out.println(it.next());
		}
	}
}

输出:

1=2
3=4
4=6
5=7
8=3
9=8


2、内部类/内部静态类
比起第一种方法,这种算是中规中矩的方法了。当然没有第一种简洁,但是更有利于我们理解Comparator这个接口。

  1. import java.util.*;
  2. class Test
  3. {
  4. //静态内部类
  5. private static class MyComparator implements Comparator<Map.Entry<Integer, String>>
  6. {
  7. public int compare(Map.Entry<Integer, String> m,Map.Entry<Integer, String> n)
  8. {
  9. //根据value排序,规则是字符串的长短
  10. return n.getValue().length()-m.getValue().length();
  11. }
  12. }
  13. public static void main(String[] args)
  14. {
  15. Map<Integer,String> map=new HashMap<>();
  16. map.put(1,"a");
  17. map.put(8,"bbb");
  18. map.put(3,"cc");
  19. map.put(5,"abcd");
  20. map.put(4,"a");
  21. map.put(9,"abcde");
  22. //这一步很关键,因为要利用sort(List<T> list,Comparator<? super T> c)方法。
  23. List<Map.Entry<Integer,String>> list=new ArrayList<>();
  24. //把map转化为Map.Entry然后放到用于排序的list里面
  25. list.addAll(map.entrySet());
  26. //调用内部类的构造器,如果这个内部类是静态内部类,就比这个好办点了。。
  27. Test.MyComparator mc=new MyComparator();
  28. //开始排序,传入比较器对象
  29. Collections.sort(list,mc);
  30. //遍历在list中排序之后的HashMap
  31. for(Iterator<Map.Entry<Integer,String>> it=list.iterator();it.hasNext();)
  32. {
  33. System.out.println(it.next());
  34. }
  35. }
  36. }
import java.util.*;
class Test
{
	//静态内部类
	private static class MyComparator implements Comparator<Map.Entry<Integer, String>>
	{
		public int compare(Map.Entry<Integer, String> m,Map.Entry<Integer, String> n)
		{
			//根据value排序,规则是字符串的长短
			return n.getValue().length()-m.getValue().length();
		}
	}
	public static void main(String[] args) 
	{
		Map<Integer,String> map=new HashMap<>();
		map.put(1,"a");
		map.put(8,"bbb");
		map.put(3,"cc");
		map.put(5,"abcd");
		map.put(4,"a");
		map.put(9,"abcde");
		//这一步很关键,因为要利用sort(List<T> list,Comparator<? super T> c)方法。
		List<Map.Entry<Integer,String>> list=new ArrayList<>();
		//把map转化为Map.Entry然后放到用于排序的list里面
		list.addAll(map.entrySet());
		//调用内部类的构造器,如果这个内部类是静态内部类,就比这个好办点了。。
		Test.MyComparator mc=new MyComparator();
		//开始排序,传入比较器对象
		Collections.sort(list,mc);
		//遍历在list中排序之后的HashMap
		for(Iterator<Map.Entry<Integer,String>> it=list.iterator();it.hasNext();)
		{
			System.out.println(it.next());
		}
	}
}

输出:

9=abcde

5=abcd

8=bbb

3=cc

1=a

4=a

在这个程序中,用内部类和用静态内部类没啥太大的区别,就是在调用方法的时候有所区别,显然静态内部类在调用的时候要方便点。。如果是内部类的话,要先创建一个外部类对象,再用这个外部类对象调用内部类构造器,创建内部类对象。

以上就是Comparator接口的主要用法,用来帮助进行HashMap的排序,当然他的老本行还是用来对list进行个性化排序。

为了让小编推荐一下,我就再写个例子,给大家具体演示一下~

  1. import java.util.*;
  2. class Test
  3. {
  4. public static void main(String[] args)
  5. {
  6. List<String> list=new ArrayList<String>();
  7. list.add("z");
  8. list.add("qwe");
  9. list.add("bb");
  10. list.add("abcd");
  11. Collections.sort(list,new Comparator<String>()
  12. {
  13. public int compare(String m,String n)
  14. {
  15. //根据字符串长短排序
  16. return m.length()-n.length();
  17. }
  18. });
  19. //遍历输出排序之后的list
  20. for(Iterator<String> it=list.iterator();it.hasNext();)
  21. {
  22. System.out.println(it.next());
  23. }
  24. }
  25. }
import java.util.*;
class Test
{
	public static void main(String[] args) 
	{
		List<String> list=new ArrayList<String>();
		list.add("z");
		list.add("qwe");
		list.add("bb");
		list.add("abcd");
		Collections.sort(list,new Comparator<String>()
		{
			public int compare(String m,String n)
			{
				//根据字符串长短排序
				return m.length()-n.length();
			}
		});
		//遍历输出排序之后的list
		for(Iterator<String> it=list.iterator();it.hasNext();)
		{
			System.out.println(it.next());
		}
	}
}

输出:

z

bb

qwe

abcd

看完我写的这篇博文,相信大家对hashmap还有list的排序的理解都得到了加深~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值