检查按月分类的星座

时间:2016-03-26 09:17:18

标签: java

有一个网页包含按月排序的生肖列表,如("Aries", "March 21 - April 19"),("Taurus", "April 20 - May 20"), ("Gemini", "May 21 - June 20"),("Cancer", "June 21 - July 22") .....     有一个选项可以将一些星座添加到收藏夹中,最喜欢的标志将在第一个列出的顺序列出,其余的十二生肖也将按照排序顺序列在收藏夹之后。     例如,我通过UI将以下标志添加到收藏夹

("Taurus", "April 20 - May 20"),("Libra", "September 23 - October 22"),("Aquarius", "January 20 - February 18")

在UI中,它将以下列方式显示

"Taurus", "April 20 - May 20"
"Libra", "September 23 - October 22"
"Aquarius", "January 20 - February 18"

"Aries", "March 21 - April 19"
"Gemini", "May 21 - June 20"
"Cancer", "June 21 - July 22"
"Leo", "July 23 - August 22"
"Virgo", "August 23 - September 22"
"Scorpio", "October 23 - November 21"
"Capricorn", "November 22 - December 21"
"Pisces", "February 19 - March 20"

我的主要目的是验证添加的收藏夹是按月分类的。其余的十二生肖也按月分类列出。 并从xml文件中验证已排序的月份。将一些生肖添加到收藏夹后,我将收到包含黄道带列表的xml响应。并且正在解析xml以获得带有“March”的星座21 - 4月19日“ 并将其保存在地图中。

Map<String,String> ZodiacMap = new HashMap<String,String>();    

最喜欢的生肖

zodiacMap1.put("Taurus", "April 20 - May 20");
zodiacMap1.put("Libra", "September 23 - October 22");
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("Scorpio", "October 23 - November 21");
zodiacMap1.put("Capricorn", "November 22 - December 21");
zodiacMap1.put("Pisces", "February 19 - March 20");

从地图上我需要验证最喜欢的生肖按月分类,以验证提醒生肖是按月分类。

它是如何变得可变...... ????

1 个答案:

答案 0 :(得分:3)

有很多种方法可以做到这一点。这是一个例子。

创建一个名为Zodiac的类,其字段为:

  • 名称,示例值:&#34; Gemini&#34;
  • 月(开始日期),示例值:5(5月)
  • 文字,示例价值:&#34; 5月21日 - 6月20日&#34;

同时实施Comparator<Zodiac>,将Zodiac字段与.month字段进行比较。

例如:

Comparator<Zodiac> byMonthComparator = new Comparator<>() {
    int compare(Zodiac o1, Zodiac o2) {
        return Integer.compare(o1.month, o2.month);
    }
}

使用您刚刚创建的自定义比较器将Zodiac个实例放入SortedSet。此类集中的项目将按月排序。

例如:

SortedSet<Zodiac> all = new TreeSet<>();
all.add(new Zodiac("Gemini", 5, "May 21 - June 20");
all.add(...);
...