我有一个生肖地图,按照以下顺序存储生肖
Map<String,String> ZodiacMap = new HashMap<String,String>();
zodiacMap.put("Aries", "March 21 - April 19");
zodiacMap.put("Taurus", "April 20 - May 20");
zodiacMap.put("Gemini", "May 21 - June 20");
zodiacMap.put("Cancer", "June 21 - July 22");
zodiacMap.put("Leo", "July 23 - August 22");
zodiacMap.put("Virgo", "August 23 - September 22");
zodiacMap.put("Libra", "September 23 - October 22");
zodiacMap.put("Scorpio", "October 23 - November 21");
zodiacMap.put("Capricorn", "November 22 - December 21");
zodiacMap.put("Aquarius", "January 20 - February 18");
zodiacMap.put("Pisces", "February 19 - March 20");
在用户界面中,一些十二生肖被添加为收藏,并在另一幅添加了黄道十二宫的地图中将其保留在地图顶部。
在这里
Tarus,Libra,Scrorpio,Aquarius
添加为收藏我必须验证添加为收藏夹的十二生肖是否处于排序顺序,同时还要验证其余的十二生肖是否处于排序顺序。
zodiacMap1.put("Taurus", "April 20 - May 20");
zodiacMap1.put("Libra", "September 23 - October 22");
zodiacMap1.put("Scorpio", "October 23 - November 21");
zodiacMap1.put("Aquarius", "January 20 - February 18");
zodiacMap1.put("Aries", "March 21 - April 19");
zodiacMap1.put("Gemini", "May 21 - June 20");
zodiacMap1.put("Cancer", "June 21 - July 22");
zodiacMap1.put("Leo", "July 23 - August 22");
zodiacMap1.put("Virgo", "August 23 - September 22");
zodiacMap1.put("Capricorn", "November 22 - December 21");
zodiacMap1.put("Pisces", "February 19 - March 20");
我不知道如何为阿布洛夫问题写逻辑。
请同时提供示例代码。
除了使用map(例如enum?),还有其他方法可以做到这一点
最佳答案
这是一个enum
,可以与开始日期进行比较。我只是在示例中添加了Aries
,但是目标是拥有所有的十二生肖。
java-8
enum Zodiac {
Aries("March 21", "April 19");
LocalDate begin, end;
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMMM DD");
Zodiac(String begin, String end){
this.begin = LocalDate.parse(begin, dtf);
this.end = LocalDate.parse(end, dtf);
}
public int compareToOtherZodiac(Zodiac o) {
return this.begin.compareTo(o.begin);
}
}
java-7
enum Zodiac {
Aries("March 21", "April 19");
Date begin, end;
DateFormat dtf = new SimpleDateFormat("MMMM DD");
Zodiac(String begin, String end) {
try {
this.begin = dtf.parse(begin);
this.end = dtf.parse(end);
} catch (ParseException pe) {
pe.printStackTrace();
}
}
public int compareToOtherZodiac(Zodiac o) {
return this.begin.compareTo(o.begin);
}
}
关于java - 比较并检查两个 map 的顺序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36103303/