对其属性的HashMap对象进行排序而不是值

问题描述:

这不是我真正的代码,我只是为了了解接下来要做什么而进行了模拟。

我有班级人物年龄,身高体重。
现在在我的课组
我创建了两个四物对其属性的HashMap对象进行排序而不是值

Person programmer, student, clerk, tech; 

我的HashMap点名

Map<Person, Integer> rollCall = new HashMap<Person, Integer>(); 

添加所有这些使用人,作为整数类型的人数

rollCall.put(programmer, 1); 
rollCall.put(clerk, 2); 
rollCall.put(student, 1); 
rollCall.put(tech, 3); 

我见过很多人在排序Has hMap使用TreeMap的值我想排序Person的属性而不是值。我想按照他们的年龄对所有这些人进行排序(即programmer.getAge();)。我不确定我是否会使用只适用于集合而非地图的编译器。 。 请帮忙... 。

+2

看看这里:http://*.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java – Heisenbug 2011-06-07 01:07:23

+0

有没有简单的方法我'我害怕。(我认为下面的一些答案假设你想对键('Person')进行排序) – toto2 2011-06-07 02:04:45

+0

是我想对键排序而不是值 – Aahil 2011-06-07 02:37:37

首先,TreeMap按键排序,而不是数值。所以这已经对你有利了。您在TreeMap中使用的任何对象必须实现Comparable,或者您必须提供Comparator作为构造函数参数。您所需要做的就是根据您的getAge()属性对比compareTo()方法(从Comparable)或compare()方法(从Comparator)进行比较。

TreeMap构造函数需要Comparator描述here.Comparator将用于排序映射中的键。

+0

我不愿意搞砸我的Person类。 在这种情况下,我必须创建一个新的Comprator类并使用Compare(Object o1,Object o2)方法。但是这里的问题是Comprator只能用集合而不是Maps来工作?有没有办法使用CompMap的TreeMap?任何例子? – Aahil 2011-06-07 02:32:48

+1

我添加了一个链接到我上面提到的构造函数。这个构造函数接受一个'Comparator'作为参数,并用它来比较这些键。 – 2011-06-07 02:46:25

您需要能够比较您的Person对象。如果对它们进行比较规范的方式,让他们实现Comparable<Person>(即给他们一个compareTo(Person)方法。

如果做到这一点,你可以使用人作为密钥一个SortedMap(如TreeMap的)。

如果有多个方面两个人可以相比,实现Comparator<Person>作为一个单独的对象。

然后给这个比较对的SortedMap建设。

这不会排序您的HashMap(一个HashMap具有总是一个看似随机顺序),但给你另一个排序数据结构。

import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Collections; 
import java.util.Comparator; 
import java.util.HashMap; 
import java.util.Iterator; 
import java.util.LinkedList; 
import java.util.List; 
import java.util.Map; 

/* 
* Sort HashMap that contains Student object 
*/ 

public class SortHashMap implements Comparator<Student> 
{ 
    public static void main(String[] args) 
    { 
     Map map = new HashMap(); 
     map.put("s1", new Student(5,"utpal")); 
     map.put("s2", new Student(4,"ramesh")); 
     map.put("s3", new Student(10,"tushar")); 
     map.put("s4", new Student(2,"anindya")); 
     Collection<Student> students = map.values(); 
     List list = new ArrayList(students); 
     Collections.sort(list,new SortHashMap()); 

     for (Iterator it = list.iterator(); it.hasNext();) 
     {   
      Student stdn = (Student)it.next();    
      System.out.println("Student id : "+stdn.id); 
      System.out.println("Student Name : "+stdn.name);    
     } 
    } 
    @Override 
    public int compare(Student s1, Student s2) 
    { 
     return s1.name.compareTo(s2.name); 
    } 
} 

class Student 
{  
    int id; 
    String name; 
    Student(int id,String name) 
    { 
     this.id = id; 
     this.name = name; 
    }  
} 

你可以得到它迭代一个Map<Person,Integer>年龄增加或通过使用自定义比较递减顺序:

Map<Person, Integer> rollCall = new TreeMap<Person, Integer>(
    new Comparator<Person>() { 
    @Override public int compare(Person p1, Person p2) { 
     return p1.getAge() - p2.getAge(); // Acending. 
     // or p2.getAge() - p1.getAge(); // Descending. 
    } 
    } 
); 

当你按年龄增加人员,他们会被插入集合中的顺序。

import java.util.Comparator; 
import java.util.HashMap; 
import java.util.Iterator; 
import java.util.Map; 
import java.util.TreeMap; 

public class PersonSort { 

    private MySort sort = new MySort(); 
    private Map<Person, String> map = new HashMap<Person, String>(); 
    private Map<Person, String> treeMap = new TreeMap<Person, String>(sort); 

    Person e1 = new Person(500, "Saurabh"); 
    Person e2 = new Person(400, "Kishan"); 
    Person e3 = new Person(900, "Ashwini"); 

    public void myMap() { 

     map.put(e3, "Ash"); 
     map.put(e2, "Krish"); 
     map.put(e1, "Sau"); 

     Iterator it = map.keySet().iterator(); 
     System.out.println("UnSorted Map"); 
     while(it.hasNext()) { 
      System.out.println(map.get(it.next())); 
     } 

     treeMap.putAll(map); 
     System.out.println("SortedMap"); 
     Iterator it1 = treeMap.keySet().iterator(); 
     while(it1.hasNext()) { 
      System.out.println(treeMap.get(it1.next())); 
     } 
    } 

    public static void main(String[] args) { 
     PersonSort es = new PersonSort(); 
     es.myMap(); 
     } 
} 

class Person { 
    Person(int id, String name) { 
     this.id = id; 
     this.name = name; 
    } 
    private int id; 
    private String name; 
    //Getters and Setters 
} 

class MySort implements Comparator<Object> { 
    public int compare(Object o1, Object o2) { 
     return ((Person) o1).getId() - ((Person)o2).getId(); 
    } 
}