java里hash比较器_java的HashMap和ArrayList比较器

如何对ArrayList中对象按照该对象某属性排序

public static void display (Collection c){

Iterator it = c.iterator ();

while (it.hasNext()){

Object o = it.next();

System.out.println(o);

}

}

public static void main(String[] args) {

Student stu1 = new Student (1,"zhangsan","male",18,"cs");

Student stu2 = new Student (2,"lisi","female",19,"cs");

Student stu3 = new Student (3,"wangwu","male",20,"cs");

Student stu4 = new Student (4,"zhaoliu","female",21,"cs");

Student stu5 = new Student (5,"xiaoming","male",22,"cs");

ArrayList List = new ArrayList();

List.add(stu1);

List.add(stu2);

List.add(stu3);

List.add(stu4);

List.add(stu5);

display(List);

加排序功能,打印时:输出学生对象的时候,需要先按照年龄排序,如果年龄相同,则按照姓名排序,如果姓名也相同,则按照学号排序。

让 Student 实现Comparable接口,或是实例化一个比较器

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

public class ComparableTest {

public static void main(String[] args) {

Comparator comparator = new Comparator(){

public int compare(Student s1, Student s2) {

//先排年龄

if(s1.age!=s2.age){

return s1.age-s2.age;

}

else{

//年龄相同则按姓名排序

if(!s1.name.equals(s2.name)){

return s1.name.compareTo(s2.name);

}

else{

//姓名也相同则按学号排序

return s1.id-s2.id;

}

}

}

};

Student stu1 = new Student (1,"zhangsan","male",28,"cs");

Student stu2 = new Student (2,"lisi","female",19,"cs");

Student stu3 = new Student (3,"wangwu","male",22,"cs");

Student stu4 = new Student (4,"zhaoliu","female",17,"cs");

Student stu5 = new Student (5,"jiaoming","male",22,"cs");

ArrayList List = new ArrayList();

List.add(stu1);

List.add(stu2);

List.add(stu3);

List.add(stu4);

List.add(stu5);

//这里就会自动根据规则进行排序

Collections.sort(List,comparator);

display(List);

}

static void display(ArrayList lst){

for(Student s:lst)

System.out.println(s);

}

}

class Student{

int age;

int id;

String gender;

String name;

String cs;

Student(int id,String name,String gender,int age,String cs){

this.age=age;

this.name=name;

this.gender=gender;

this.id=id;

this.cs=cs;

}

public String toString(){

return id+" "+name+" "+gender+" "+age+" "+cs;

}

}

HashMap重写比较器

package test;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.HashMap;

import java.util.Map;

public class HashMapSort {

public void test(){

HashMap map = new HashMap();

map.put("d", 2);

map.put("c", 1);

map.put("b", 1);

map.put("a", 3);

ArrayList> infoIds =new ArrayList>(map.entrySet());

System.out.println("排序前=================");

for (int i = 0; i < infoIds.size(); i++) {

String id = infoIds.get(i).toString();

System.out.println(id);

}

//d 2

//c 1

//b 1

//a 3

System.out.println("排序后==============");

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

public int compare(Map.Entry o1, Map.Entry o2) {

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

//return (o1.getKey()).toString().compareTo(o2.getKey());

}

});

//排序后

for (int i = 0; i < infoIds.size(); i++) {

String id = infoIds.get(i).toString();

System.out.println(id);

}

}

public static void main(String[] args){

HashMapSort so=new HashMapSort();

so.test();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值