您的位置:首页 > 编程语言 > Java开发

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

2013-08-25 21:40 761 查看
对于List,可以调用Collections工具类的sort()方法,直接进行排序。HashMap,就没这么幸福了。。

其实,只要了解了Comparator这个接口之后,HashMap的排序也就不难了,无论是根据key,还是根据value排序。

这个接口也很简单,只有一个抽象方法int compare();需要我们去实现。这个方法,就是实现你制订的比较规则。(其实这个接口里面还有一个方法boolean equals()但是API里面说不是现在这个方法也总是安全的。。这个方法只有当你的程序有两个以上的Comparator的实现之后才用的上,用于比较)

具体去实现的时候,有两种常用途径。

1、匿名内部类

这是最简单实用的一种方法了。为了方便实现根据key和value的排序,我把这个map设计成Map<Integer,Integer>

[java]
view plaincopyprint?

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());
}
}
}

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这个接口。

[java]
view plaincopyprint?

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());
}
}
}

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进行个性化排序。

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

[java]
view plaincopyprint?

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());
}
}
}

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的排序的理解都得到了加深~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: