java keyset 排序_Java中使用TreeMap将Map的KeySet排序

使用TreeMap简单地HashMap的key进行排序,使用ArrayList对HashMap的value排序。

分别使用Comparator接口和Comparable接口实现key的排序。

package com.inspiration.examples.collection.sort;

import java.util.ArrayList;

import java.util.Collection;

import java.util.Collections;

import java.util.Comparator;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Set;

import java.util.TreeMap;

import java.util.Map.Entry;

public class SortExample {

public static void listSort() {

ListstuList = new ArrayList();

Student s1 = new Student(1, "wang");

Student s2 = new Student(2, "li");

stuList.add(s1);

stuList.add(s2);

Collections.sort(stuList);

for (Student stu : stuList) {

System.out.println(stu);

}

// map sort which implements the Comparable Interface

public static void mapKeySetSort() {

MaphashMap = new HashMap();

for (int i = 0; i < 100; i++) {

Student stu = new Student(i, "wang" + i);

hashMap.put(stu, i);

}

/*

* System.out.println("the hashMap out put----"); Set

* set=hashMap.keySet(); for(Student stu:set){ System.out.println(stu);

* }

*/

System.out.println("the treeMap out put----");

TreeMaptreeMap = new TreeMap(

hashMap);

Setset2 = treeMap.keySet();

for (Student stu : set2) {

System.out.println(stu);

}

// map sort which implements the Comparator Interface

public static void mapKeySetSort2() {

MaphashMap = new HashMap();

for (int i = 0; i < 100; i++) {

Teacher tea = new Teacher(i, "wang" + i);

hashMap.put(tea, i);

}

/*

* System.out.println("the hashMap out put----"); Set

* set=hashMap.keySet(); for(Teacher tea:set){ System.out.println(tea);

* }

*/

System.out.println("the treeMap out put----");

TreeMaptreeMap = new TreeMap(

new TeacherComparator());

treeMap.putAll(hashMap);

Setset2 = treeMap.keySet();

for (Teacher tea : set2) {

System.out.println(tea);

}

public static void sortValues() {

Mapkeyfreqs = new HashMap();

keyfreqs.put("w", 1);

keyfreqs.put("b", 2);

ArrayList> l = new ArrayList>(

keyfreqs.entrySet());

Collections.sort(l, new Comparator>() {

public int compare(Map.Entryo1,

Map.Entryo2) {

return (o1.getValue() - o2.getValue());

}

});

for (Entrye : l) {

System.out.println(e.getKey() + "::::" + e.getValue());

}

public static void main(String[] args) {

// listSort();

// System.exit(1);

// mapSort();

// mapKeySetSort2();

}

package com.inspiration.examples.collection.sort;

public class Student implements Comparable{

private int id;

private String name;

public Student(int id, String name) {

super();

this.id = id;

this.name = name;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

@Override

public int compareTo(Student o) {

// return o.id-this.id;

return this.id-o.id;

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + id;

return result;

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Student other = (Student) obj;

if (id != other.id)

return false;

return true;

}

@Override

public String toString() {

return "Student [id=" + id + ", name=" + name + "]";

}

package com.inspiration.examples.collection.sort;

public class Teacher {

private int id;

private String name;

public Teacher(int id, String name) {

super();

this.id = id;

this.name = name;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + id;

return result;

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Teacher other = (Teacher) obj;

if (id != other.id)

return false;

return true;

}

@Override

public String toString() {

return "Teacher [id=" + id + ", name=" + name + "]";

}

package com.inspiration.examples.collection.sort;

import java.util.Comparator;

public class TeacherComparator implements Comparator{

@Override

public int compare(Teacher o1, Teacher o2) {

return o1.getId()-o2.getId();

}

TreeMap本身使用红黑树算法实现。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值