2012-10-25 137 views
1

我有一個散列表陣列,每個散列表包含24小時制的關鍵值對按時間順序對散列圖陣列進行排序

我想按照時間的升序對這個數組進行排序。我怎麼能做到這一點?

這裏是我的代碼片段:

HashMap[] arr = new HashMap[100]; 

for(int i=0;i<100;i++) { 
    HashMap<String,String> child=new HashMap<String,String>(); 
    child.put("some_time","21:09"); //time changes per iteration(time is in 24-hour format) 
    arr[i]=child; 
} 
+2

你爲什麼不乾脆使用HashMap與100的任何原因條目而不是數組? – assylias

+0

我認爲散列表不是合適的數據結構來排序 –

+1

@assylias,BhavikShah我懷疑每個有問題的hashmaps包含多個條目;其中一個條目是排序時間 - 他想按它排序。 –

回答

1

以下是完整的代碼,將在時間排序的數組這是hh:mm格式:

HashMap<String,String>[] harr = new HashMap[10]; 
final DateFormat df = new SimpleDateFormat("kk:mm"); 
// prepare your data 
for(int i=0;i<harr.length;i++) { 
    HashMap<String,String> child=new HashMap<String,String>(); 
    int ss = (int)(Math.random() * (59 + 1)); 
    //time changes per iteration(time is in 24-hour format) 
    child.put("some_time", String.format("21:%02d", ss)); 
    harr[i]=child; 
} 
System.out.printf("map array is: %s%n", Arrays.deepToString(harr)); 

// now apply sort using a custom method 
Arrays.sort(harr, new Comparator<HashMap<String,String>>() { 
    public int compare(HashMap<String,String> o1, HashMap<String,String> o2) { 
     String t1 = o1.get("some_time"); 
     String t2 = o2.get("some_time"); 
     try { 
      Date dt1 = df.parse(t1); 
      Date dt2 = df.parse(t2); 
      return dt1.compareTo(dt2); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 
     return 0; 
    } 
}); 
System.out.printf("sorted map array is: %s%n", Arrays.deepToString(harr)); 
+0

感謝您的回答,到目前爲止它的工作正常,但有一個時間小時包含12例如12:01,12:34,12:55等條目不排序,並出現在陣列的開始。 – dd619

+0

@ dd619:請嘗試使用'最後的DateFormat df =新的SimpleDateFormat(「kk:mm」);'而不是'「hh:mm」'。 – anubhava

+1

太棒了!看起來你對java api非常瞭解,謝謝! – dd619

0

由於Bhavik指出的那樣,你可以不使用JDK到它的全部潛力 - 看看SortedMap這可能正是你要找的內容;可能與您自己的執行Comparator

SortedMap arr = new TreeMap<String,HashMap<String,String>>(); 
for (int i=0 ; i<100 ; i++) 
{ 
    Map<String,String> child = HashMap<String,String>(); 
    child.put("some_time" , "21:09"); 
    arr.put("21:09" , child); 
} 

那麼你可以使用arr.values().iterator()讓你排序child仁。

乾杯,

+0

SortedMap將如何解決該問題?他想對數組進行排序(並且SortedMap按鍵排序,而不是值)。 – assylias

+1

因此,Assylias。 –

2

您可以使用Arrays.sort(T[], Comparator<T>)。這使您可以通過任何類型的陣列和寫自己的自定義比較方法是這樣的:

Arrays.sort(arr, new Comparator<HashMap>() { 
    public int compare(HashMap o1, HashMap o2) { 
     // Compare values you're interested in and return int as specified by Comparator API 
    } 
}); 

the API詳情,以瞭解返回。

0

一般的做法是編寫Comparator以基於密鑰訂購一對HashMap對象,然後將其作爲參數傳遞給Arrays.sort(T[], Comparator<T>)方法。

釷比較會是這個樣子:

Comparator<HashMap> DATE_ORDER = new Comparator<HashMap>() { 
     public int compare(Comparator<HashMap>h1, Comparator<HashMap>h2) { 
      String time1 = h1.get("some_time"); 
      String time2 = h2.get("some_time"); 
      return time1.compareTo(time2); // assuming that the time strings 
              // can be ordered that way 
     } 
    }; 

說了這麼多,你的問題有試圖使用地圖時,他們確實應該編寫自定義類「嗅覺」。

1

在繼續使用這種方法之前,請先考慮評論並確定hashmaps數組是否是正確的選擇。正如我指出的那樣,如果您有一堆地圖,每個地圖都包含大量信息,並且一個條目就是您的日期,那麼這可能是正確的做法,在這種情況下,對數組進行排序的最簡單方法是使用Arrays.sort方法:

HashMap[] arr=new Hashmap[100]; 

for(int i=0;i<100;i++){ 
    HashMap<String,String> child=new HashMap<String,String>(); 
    ... // put all the info into the HashMap 
    child.put("some_time","21:09"); //time changes per iteration(time is in 24-hour format) 
    arr[i]=child; 
} 

Arrays.sort(arr, new Comparator<HashMap>() { 
    public int compare(HashMap o1, HashMap o2) { 
     String d1 = o1.get("some_time"); 
     String d2 = o2.get("some_time"); 

     //compare the two dates. If you're always in the same format, e.g. HH:MM (24 hours, two-digit hour, two-digit year), you might even be able to simply compare strings: 
     return d1.compareTo(d2); 
    } 
}); 
相關問題