package demo;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1 = "2016-10";
String s2 = "2016-11";
String s3 = "2016-12";
String s4 = "2017-1";
List sList = new ArrayList();
sList.add(s1);
sList.add(s2);
sList.add(s3);
sList.add(s4);
System.out.println("初始年月sList: " + sList);
Map yearsDic = getYearsAndCount(sList);
System.out.println("每一年下面的月份數yearsDic: " + yearsDic);
//建立年份和月份集合
List yearList = new ArrayList();
List monthList = new ArrayList();
for (int i=0; i< sList.size(); i++)
{
String year = sList.get(i).split("-")[0];
String month = sList.get(i).split("-")[1];
if(!yearList.contains(year))
{
yearList.add(year);
}
monthList.add(month);
}
System.out.println("年份集合yearList: " + yearList);
System.out.println("月份集合monthList: " + monthList);
}
/*返回年份集合,每一年占多少列*/
private static Map getYearsAndCount(List sList)
{
Map yearCount = new HashMap();
for (int i = 0; i < sList.size(); i++)
{
String year = sList.get(i).split("-")[0];
if (yearCount.containsKey(year))
{
Integer count = yearCount.get(year);
yearCount.put(year, count+1);//增加列
}else {
yearCount.put(year, 1);//初始值1
}
}
return yearCount;
}
}