2014-09-01 61 views
1

我有一張地圖,和他的關鍵和值obejct像要排序根據地圖對象鍵

List<AppointmentRequest> list=AppointmentRequest.findAllAppointmentRequests(); 
    for(AppointmentRequest a:list){ 
     Store store=Store.findStore(a.getStoreId()); 
     map.put(AppointmentRequest.findAppointmentRequest(a.getId()),store); 

    } 

AppointmentRequest包含日期列 現在我想排序根據此列,以顯示它在JSP頁面此地圖,任何人都可以幫助我嗎?

回答

1

使用TreeMap

紅黑樹基於NavigableMap實現。該地圖根據其按鍵的自然順序或在地圖創建時提供的比較器進行排序,具體取決於使用哪個構造函數。

向構造函數提供一個Comparator,以便按日期列對鍵進行排序。

它可以看起來像:

Map<AppointmentRequest,Store> map = 
    new TreeMap(new Comparator<? super AppointmentRequest> comparator { 
     int compare(AppointmentRequest o1, AppointmentRequest o2) { 
      // return -1,0 or 1 based on the relative order of o1 and o2 
     } 
    }); 
+0

能否請你解釋一下嗎? – 2014-09-01 18:03:10

+0

@ user3498071請參閱編輯 – Eran 2014-09-01 18:09:08