对HashMap对象的键值对内容进行排序
2016-07-24 08:22
429 查看
1、首先,HashMap集合对象存储的是无序的键值对是不能对HashMa集合对象排序,但是我们可以取出HashMap集合对象的键值对内容,对这个进行排序。
2、HashMap对象可通entrySet()将键值对内容取出返回的是Set<Entry<K,V>>集合,然后可以同过ArrayList的构造函数(public ArrayList(Collection<? extends E> c))将set集合转出成List集合
3、看代码:
<span style="font-size:14px;">package cn.com.lcx.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import cn.com.lcx.model.Student;
import cn.com.lcx.model.Teacher;
public class MapSort {
/**
* @param args
*/
public static void main(String[] args) {
Student[] stuArr= {new Student("lwx-1", 15, 60),
new Student("lwx-2", 17, 90),
new Student("lwx-3", 10, 60),
new Student("lwx-4", 25, 50),
new Student("lwx-5", 15, 90),
new Student("lwx-6", 25, 70)};
Teacher[] teaArr = {new Teacher("tea-1", "语文", 50, 100),
new Teacher("tea-2", "数学", 20, 40),
new Teacher("tea-2", "数学", 20, 40),
new Teacher("tea-2", "数学", 20, 40),
new Teacher("tea-3", "英语", 20, 60),
new Teacher("tea-4", "英语", 80, 10)};
HashMap<Student,Teacher> map = new HashMap<Student,Teacher>();
/**
* 组装HashMap
* key Student对象 value Teacher对象
*/
for(int i=0;i<stuArr.length;i++){
map.put(stuArr[i], teaArr[i]);
}
System.out.println("HashMap原始排序----------------");
//排序前HashMap数据
printMap(map);
/**
* map集合内容的entry对象放到list集合中。
* map集合对象是无序的不能排序,我们可以把map集合对象的 键值对内容取出来排序
*/
List<Entry<Student,Teacher>> mapList = new ArrayList<Entry<Student,Teacher>>(map.entrySet());
/**
* 1、对HashMap内容按key Student对象排序,年龄升序 成绩降序
* 对List集合中的内容进行自定义排序,一种是List集合存放的类型实现Comparable接口,
* 第二种实现Comparator接口自定义排序规则类。因为Entry类已经定义好且没有实现Comparable接口,所以用第二方法
* 此处没有单独定义排序规则类,为了方法采用了匿名内部类生成排序规则对象
*/
Collections.sort(mapList,new Comparator<Entry<Student,Teacher>>() {
@Override
public int compare(Entry<Student, Teacher> o1,
Entry<Student, Teacher> o2) {
/**
* 按年龄升序(if条件和返回值相同) 年龄相同 按成绩降序(if条件和返回值相反)
*/
if(o1.getKey().getAge()> o2.getKey().getAge()){
return 1;
}else if(o1.getKey().getAge()< o2.getKey().getAge()){
return -1;
}else{
if(o1.getKey().getScore()> o2.getKey().getScore()){
return -1;
}else if(o1.getKey().getScore()< o2.getKey().getScore()){
return 1;
}else{
return 0;
}
}
}
});
System.out.println("HashMap内容按key值排序----------------");
printEntry(mapList);
/**
* 2、对HashMap内容按value Teacher对象排序,年龄降序 学生人数升序
*/
Collections.sort(mapList,new Comparator<Entry<Student,Teacher>>() {
@Override
public int compare(Entry<Student, Teacher> o1,
Entry<Student, Teacher> o2) {
/**
* 按年龄降序(if条件和返回值相同) 年龄相同 按学生人数升序(if条件和返回值相反)
*/
if(o1.getValue().getAge()> o2.getValue().getAge()){
return -1;
}else if(o1.getValue().getAge()< o2.getValue().getAge()){
return 1;
}else{
if(o1.getValue().getStuNum()> o2.getValue().getStuNum()){
return 1;
}else if(o1.getValue().getStuNum()< o2.getValue().getStuNum()){
return -1;
}else{
return 0;
}
}
}
});
System.out.println("HashMap内容按value值排序----------------");
printEntry(mapList);
}
/**
* 打印map集合信息
* @param map
*/
public static void printMap(Map map){
//Set<Map.Entry<Student,Teacher>> s = map.entrySet();
for(Entry<Student,Teacher> entry : (Set<Entry<Student,Teacher>>)map.entrySet()){
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
/**
* 打印List集合信息
* @param mapList
*/
public static void printEntry(List<Entry<Student,Teacher>> mapList){
//Set<Map.Entry<Student,Teacher>> s = map.entrySet();
for(Entry<Student,Teacher> entry : mapList){
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
}
</span>验证结果:
4、其实Map集合中有自带排序功能的集合TreeMap,它是按key进行排序的,如果key值是基本类型 TreeMap对象就会自动排序 因为基本类型的包装类都实现了Comparable接口,如果是自定义的类类型,那么该类必须实现Comparable接口,然后按照自定义的排序规则 自动进行排序。
2、HashMap对象可通entrySet()将键值对内容取出返回的是Set<Entry<K,V>>集合,然后可以同过ArrayList的构造函数(public ArrayList(Collection<? extends E> c))将set集合转出成List集合
3、看代码:
<span style="font-size:14px;">package cn.com.lcx.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import cn.com.lcx.model.Student;
import cn.com.lcx.model.Teacher;
public class MapSort {
/**
* @param args
*/
public static void main(String[] args) {
Student[] stuArr= {new Student("lwx-1", 15, 60),
new Student("lwx-2", 17, 90),
new Student("lwx-3", 10, 60),
new Student("lwx-4", 25, 50),
new Student("lwx-5", 15, 90),
new Student("lwx-6", 25, 70)};
Teacher[] teaArr = {new Teacher("tea-1", "语文", 50, 100),
new Teacher("tea-2", "数学", 20, 40),
new Teacher("tea-2", "数学", 20, 40),
new Teacher("tea-2", "数学", 20, 40),
new Teacher("tea-3", "英语", 20, 60),
new Teacher("tea-4", "英语", 80, 10)};
HashMap<Student,Teacher> map = new HashMap<Student,Teacher>();
/**
* 组装HashMap
* key Student对象 value Teacher对象
*/
for(int i=0;i<stuArr.length;i++){
map.put(stuArr[i], teaArr[i]);
}
System.out.println("HashMap原始排序----------------");
//排序前HashMap数据
printMap(map);
/**
* map集合内容的entry对象放到list集合中。
* map集合对象是无序的不能排序,我们可以把map集合对象的 键值对内容取出来排序
*/
List<Entry<Student,Teacher>> mapList = new ArrayList<Entry<Student,Teacher>>(map.entrySet());
/**
* 1、对HashMap内容按key Student对象排序,年龄升序 成绩降序
* 对List集合中的内容进行自定义排序,一种是List集合存放的类型实现Comparable接口,
* 第二种实现Comparator接口自定义排序规则类。因为Entry类已经定义好且没有实现Comparable接口,所以用第二方法
* 此处没有单独定义排序规则类,为了方法采用了匿名内部类生成排序规则对象
*/
Collections.sort(mapList,new Comparator<Entry<Student,Teacher>>() {
@Override
public int compare(Entry<Student, Teacher> o1,
Entry<Student, Teacher> o2) {
/**
* 按年龄升序(if条件和返回值相同) 年龄相同 按成绩降序(if条件和返回值相反)
*/
if(o1.getKey().getAge()> o2.getKey().getAge()){
return 1;
}else if(o1.getKey().getAge()< o2.getKey().getAge()){
return -1;
}else{
if(o1.getKey().getScore()> o2.getKey().getScore()){
return -1;
}else if(o1.getKey().getScore()< o2.getKey().getScore()){
return 1;
}else{
return 0;
}
}
}
});
System.out.println("HashMap内容按key值排序----------------");
printEntry(mapList);
/**
* 2、对HashMap内容按value Teacher对象排序,年龄降序 学生人数升序
*/
Collections.sort(mapList,new Comparator<Entry<Student,Teacher>>() {
@Override
public int compare(Entry<Student, Teacher> o1,
Entry<Student, Teacher> o2) {
/**
* 按年龄降序(if条件和返回值相同) 年龄相同 按学生人数升序(if条件和返回值相反)
*/
if(o1.getValue().getAge()> o2.getValue().getAge()){
return -1;
}else if(o1.getValue().getAge()< o2.getValue().getAge()){
return 1;
}else{
if(o1.getValue().getStuNum()> o2.getValue().getStuNum()){
return 1;
}else if(o1.getValue().getStuNum()< o2.getValue().getStuNum()){
return -1;
}else{
return 0;
}
}
}
});
System.out.println("HashMap内容按value值排序----------------");
printEntry(mapList);
}
/**
* 打印map集合信息
* @param map
*/
public static void printMap(Map map){
//Set<Map.Entry<Student,Teacher>> s = map.entrySet();
for(Entry<Student,Teacher> entry : (Set<Entry<Student,Teacher>>)map.entrySet()){
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
/**
* 打印List集合信息
* @param mapList
*/
public static void printEntry(List<Entry<Student,Teacher>> mapList){
//Set<Map.Entry<Student,Teacher>> s = map.entrySet();
for(Entry<Student,Teacher> entry : mapList){
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
}
</span>验证结果:
4、其实Map集合中有自带排序功能的集合TreeMap,它是按key进行排序的,如果key值是基本类型 TreeMap对象就会自动排序 因为基本类型的包装类都实现了Comparable接口,如果是自定义的类类型,那么该类必须实现Comparable接口,然后按照自定义的排序规则 自动进行排序。
相关文章推荐
- mybaits逆向工程
- 2048小游戏开发过程以及感想
- Java千百问_09基础类库(002)_util包有什么功能
- HDU1969 Pie
- 二进制文件与文本文件的区别
- 声笔码7.00正式发布
- BZOJ1002: [FJOI2007]轮状病毒
- LeetCode[357] Count Numbers with Unique Digits
- mysql启动问题access denied for user 'root'@'localhost'(using password:YES)
- 模板-欢迎使用CSDN-markdown编辑器
- Windows 2003终端服务的安装与激活
- IO —— 使用打印流实现文件的复制
- Android多线程操作——线程池管理综述
- IO 知识学习 —— 内存操作流
- 33. Search in Rotated Sorted Array && 81. Search in Rotated Sorted Array II && 153. Find Minimum in Rotated Sorted Array && 154. Find Minimum in Rotated Sorted Array II
- qt pushbutton美化
- HOJ 1005题 题解
- LeetCode "Super Pow"
- hdu2209 翻纸牌游戏--BFS & 位运算 & 状态压缩(待解决)
- Spark小课堂Week1 Hello Spark