gpt4 book ai didi

java - 如何使用比较器接口(interface)按字符串键对映射进行排序

转载 作者:行者123 更新时间:2023-12-01 18:17:53 26 4
gpt4 key购买 nike

我知道以前也有人问过类似的问题,但我有一个具体的问题,我不能弄错。

我想按一周中的几天对 map 进行排序,其中一周中的几天是 map 的键。如何在比较方法中比较没有自然顺序的字符串?排序末尾的 map 应该是:周一、周二、周三、周四。

这是到目前为止我的代码,但我陷入了比较方法。

    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()));
}
}
}

提前致谢

最佳答案

如果您必须使用包含自定义订单值的其他静态 map ,那么枚举将是一个很好的解决方案。

像这样定义一个枚举

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

与我在枚举中定义的顺序相同。当然,仅当您的 Map 中的键范围有限(例如您的情况是一周中的几天)时,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;
}

}

关于java - 如何使用比较器接口(interface)按字符串键对映射进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28547326/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com