如何使用比较器接口按字符串键对Map进行排序

时间:2015-02-16 17:47:20

标签: java sorting collections comparator

我知道之前已经提出了类似的问题,但我有一个特定的问题,我不能错了。

我希望按星期几来对地图进行排序,其中星期几是地图的关键字。如何比较比较方法中没有自然顺序的字符串?排序结束时的地图应该是:周一,周二,周三,周四。

到目前为止,这是我的代码,但我仍然坚持使用比较方法。

    class OpeningTimes {

       private String openingTime;
       private String closingTime;
       public String getOpeningTime() {
           return openingTime;
       }
       public void setOpeningTime(String openingTime) {
           this.openingTime = openingTime;
       }
       public String getClosingTime() {
           return closingTime;
       }
       public void setClosingTime(String closingTime) {
           this.closingTime = closingTime;
       }

       @Override
       public String toString() {
           return new StringBuilder( "Opening Times: " + this.openingTime + " Closing Times: " + this.closingTime).toString();
       }
   }

比较

class OpeningTimesComparator implements Comparator<Map.Entry<String, OpeningTimes>>{

   @Override
   public int compare(Entry<String, OpeningTimes> o1, Entry<String, OpeningTimes> o2) {
       return 0; // what is the logic?
   }}

转轮:

public class TestClass {

    public static void main(String[] args) {

        Map<String, OpeningTimes> openingTimesMap = new TreeMap<String, OpeningTimes>();

        OpeningTimes openTime1 = new OpeningTimes();
        openTime1.setOpeningTime("9PM");
        openTime1.setClosingTime("10PM");

        OpeningTimes openTime2 = new OpeningTimes();
        openTime2.setOpeningTime("11PM");
        openTime2.setClosingTime("9PM");

        OpeningTimes openTime3 = new OpeningTimes();
        openTime3.setOpeningTime("13PM");
        openTime3.setClosingTime("14PM");

        OpeningTimes openTime4 = new OpeningTimes();
        openTime4.setOpeningTime("15PM");
        openTime4.setClosingTime("13PM");

        openingTimesMap.put("Tue", openTime2);
        openingTimesMap.put("Thu", openTime4);
        openingTimesMap.put("Mon", openTime1);
        openingTimesMap.put("Wed", openTime3);


        for (Entry<String, OpeningTimes> openingTimesSingle : openingTimesMap.entrySet()) {
            System.out.println("Key: " + openingTimesSingle.getKey() + " Value: " + openingTimesMap.get(openingTimesSingle.getKey()));
        }

        List<Map.Entry<String, OpeningTimes>> list = new ArrayList<Map.Entry<String, OpeningTimes>>(openingTimesMap.entrySet());

        Collections.sort(list, new OpeningTimesComparator());

        for (Entry<String, OpeningTimes> openingTimesSingle : openingTimesMap.entrySet()) {
            System.out.println("Key: " + openingTimesSingle.getKey() + " Value: " + openingTimesMap.get(openingTimesSingle.getKey()));
        }
    }
}

提前致谢

1 个答案:

答案 0 :(得分:4)

Enum将是一个很好的解决方案,如果不是你必须使用包含自定义订单值的其他静态地图。

定义这样的枚举

enum Week{
  SUN,MON,TUE,WED,THU,FRI,SAT   
}

创建EnumMap的实例并添加条目,当您遍历keySet时,将按照枚举中提到的顺序检索条目

    Map<Week, String> map = new EnumMap<Week, String>(Week.class);

        map.put(Week.WED, "Wednesday");
        map.put(Week.SUN, "Sunday");
        map.put(Week.MON, "Monday");
        map.put(Week.THU, "Thursday");
        map.put(Week.SAT, "Saturday");
        map.put(Week.FRI, "Friday");
        map.put(Week.TUE, "Tuesday");


        for(Week week : map.keySet()){
            System.out.println(week + " -- " +map.get(week));
        }

输出

SUN -- Sunday
MON -- Monday
TUE -- Tuesday
WED -- Wednesday
THU -- Thursday
FRI -- Friday
SAT -- Saturday

我在枚举中定义的顺序。当然,EnumMap只有在地图中有一系列有限的键时才有用且有效,例如在你的情况下一周中的几天。希望这会有所帮助:)

选项2: -

这个假设inputKeys完全由您管理。请注意这一点,因为如果给定密钥不存在映射,它可能会给你NPE。

private static Map<String,Integer> orderMappings = new HashMap<>();

    static{
        orderMappings.put("Sunday", 1);
        orderMappings.put("Monday", 2);
        orderMappings.put("Tuesday", 3);
        orderMappings.put("Wednesday", 4);
        orderMappings.put("Thursday", 5);
        orderMappings.put("Friday", 6);
        orderMappings.put("Saturday", 7);
    }

    class OrderComparator implements Comparator<String>{

        @Override
        public int compare(String key1, String key2) {
            int p1 = orderMappings.get(key1);
            int p2 =  orderMappings.get(key2);
            return p1-p2;
        }

    }