2015-07-04 61 views
0

我有這樣的對象:訂購HashMap中不起作用

public class Customer { 

    private String id; 
    private String name; 
    private String cf; 
    private String pi; 
    private String telephone; 
    private String email; 
    private String website; 
    private String sector; 
    private String address; 

    //constructor and getter, setter method 

} 

和地圖客戶到主:

Map<String, Customer> customerMap = new HashMap<>(); 
customerMap.put("1", customer1); 
customerMap.put("2", customer2); 
... 

sortMapByName(customerMap); 

我想通過名稱屬性來訂購吧。

我使用該解決方案在此鏈接:How to sort a Map in Java

代碼:

public void sortMapByName(Map<String, Customer> unsortMap) { 

    // Convert Map to List 
    List<Map.Entry<String, Customer>> list = new LinkedList<>(unsortMap.entrySet()); 

    // Sort list with comparator, to compare the Map values 
    Collections.sort(list, new Comparator<Map.Entry<String, Customer>>() { 

     @Override 
     public int compare(Map.Entry<String, Customer> o1, Map.Entry<String, Customer> o2) { 
      return (o1.getValue().getName()).compareTo(o2.getValue().getName()); 
     } 
    }); 

    // Convert sorted map back to a Map 
    listCustomer = new LinkedHashMap<>(); 
    for (Map.Entry<String, Customer> entry : list) { 
     listCustomer.put(entry.getKey(), entry.getValue()); 
    } 

} 

這是行不通的,爲什麼呢?

UPDATE

請試試吧。

Customer.java

public class Customer { 

private String id; 
private String name; 
private String cf; 
private String pi; 
private String telephone; 
private String email; 
private String website; 
private String sector; 
private String address; 

public Customer() { 
} 

public Customer(String id, String name, String cf, String pi, String telephone, String email, String website, String sector, String address) { 
    this.id = id; 
    this.name = name; 
    this.cf = cf; 
    this.pi = pi; 
    this.telephone = telephone; 
    this.email = email; 
    this.website = website; 
    this.sector = sector; 
    this.address = address; 
} 

public String getId() { 
    return id; 
} 

public void setId(String id) { 
    this.id = id; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public String getCf() { 
    return cf; 
} 

public void setCf(String cf) { 
    this.cf = cf; 
} 

public String getPi() { 
    return pi; 
} 

public void setPi(String pi) { 
    this.pi = pi; 
} 

public String getTelephone() { 
    return telephone; 
} 

public void setTelephone(String telephone) { 
    this.telephone = telephone; 
} 

public String getEmail() { 
    return email; 
} 

public void setEmail(String email) { 
    this.email = email; 
} 

public String getWebsite() { 
    return website; 
} 

public void setWebsite(String website) { 
    this.website = website; 
} 

public String getSector() { 
    return sector; 
} 

public void setSector(String sector) { 
    this.sector = sector; 
} 

public String getAddress() { 
    return address; 
} 

public void setAddress(String address) { 
    this.address = address; 
} 

Main.java

public class Main { 

public static void main(String[] args) { 

    Map<String, Customer> unsortedMap = new HashMap<>(); 

    Customer one = new Customer("1", "B", "bbb", "1234", "bbb", "gmail.com", "none", "student", "Italy"); 
    Customer two = new Customer("2", "C", "ccc", "1234", "ccc", "gmail.com", "none", "student", "Italy"); 
    Customer three = new Customer("3", "A", "aaa", "1234", "aaa", "gmail.com", "none", "student", "Italy"); 

    unsortedMap.put("1", one); 
    unsortedMap.put("2", two); 
    unsortedMap.put("3", three); 

    System.out.print("Before: \n"+unsortedMap); 

    Map<String, Customer> sortedMap = sortMapByName(unsortedMap); 

    System.out.print("\n\nAfter: \n"+sortedMap); 
} 

public static Map<String, Customer> sortMapByName(Map<String, Customer> unsortMap) { 

    // Convert Map to List 
    List<Map.Entry<String, Customer>> list = new LinkedList<>(unsortMap.entrySet()); 

    // Sort list with comparator, to compare the Map values 
    Collections.sort(list, new Comparator<Map.Entry<String, Customer>>() { 

     @Override 
     public int compare(Map.Entry<String, Customer> o1, Map.Entry<String, Customer> o2) { 
      return (o1.getValue().getName()).compareTo(o2.getValue().getName()); 
     } 
    }); 

    // Convert sorted map back to a Map 
    Map<String, Customer> sortedMap = new LinkedHashMap<>(); 
    for (Iterator<Map.Entry<String, Customer>> it = list.iterator(); it.hasNext();) { 
     Map.Entry<String, Customer> entry = it.next(); 
     sortedMap.put(entry.getKey(), entry.getValue()); 
    } 

    return sortedMap; 

} 

} 
+0

什麼不行? – sstan

+0

哈希映射存儲鍵值爲對象散列值的值。請參閱http://stackoverflow.com/questions/21974361/what-java-collection-should-i-use使用TreeMap –

+0

@RobertWadowski,您沒有閱讀他的代碼。它採用HashMap,但他將這些值放入LinkedHashMap中,這會在迭代時保留插入順序。 – Dogs

回答

2

您需要使用有序映射的方法。將類型從void更改爲Map<String, Customer,並在末尾添加return listCustomer;。原始地圖在當前代碼中保持不變。而不是原來的無序一個

public Map<String, Customer> sortMapByName(Map<String, Customer> unsortMap) { 

    ... 
    return listCustomer; 
} 

如果由於某種原因,listCustomer是一個類的成員,請確保您使用。

UPDATE: 當打印地圖,可以遍歷所有的映射條目:

for (Iterator<String> iterator = sortedMap.keySet().iterator(); iterator.hasNext();) { 
    Customer cust = sortedMap.get(iterator.next()); 
    System.out.println(cust); 
} 

你想也需要重寫toString()方法Customer打印像一些有用的東西,東西:

@Override 
public String toString() { 
    return "Customer [id=" + id + ", name=" + name + ", cf=" + cf + ", pi=" 
      + pi + ", telephone=" + telephone + ", email=" + email 
      + ", website=" + website + ", sector=" + sector + ", address=" 
      + address + "]"; 
} 
+1

他應該做你正在說的。但他將他的結果分配給「listCustomer」,這似乎是在排序方法之外定義的。所以也許這是故意的?所以也許他的問題是別的? – sstan

+0

我有更新的問題,請嘗試代碼..更新:作品! – Dave

+1

@DavideFruci工作正常。你只是沒有清楚地打印地圖來顯示'name'字段。查看我的更新。 – manouti