2012-10-25 136 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); 
    } 
}); 
相关问题