2011-06-07 101 views
4

這不是我真正的代碼,我只是爲了瞭解接下來要做什麼而進行了模擬。

我有班級人物年齡,身高體重。
現在在我的課組
我創建了兩個四物對其屬性的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://stackoverflow.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

回答

1

首先,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

1

您需要能夠比較您的Person對象。如果對它們進行比較規範的方式,讓他們實現Comparable<Person>(即給他們一個compareTo(Person)方法。

如果做到這一點,你可以使用人作爲密鑰一個SortedMap(如TreeMap的)。

如果有多個方面兩個人可以相比,實現Comparator<Person>作爲一個單獨的對象。

然後給這個比較對的SortedMap建設。

這不會排序您的HashMap(一個HashMap具有總是一個看似隨機順序),但給你另一個排序數據結構。

-1
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; 
    }  
} 
4

你可以得到它迭代一個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. 
    } 
    } 
); 

當你按年齡增加人員,他們會被插入集合中的順序。

0
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(); 
    } 
} 
相關問題