2016-08-26 46 views
0

如何使用java根据日期按月分组并计算总数?如何使用java根据日期按月分组并计算总数?

public static void main(String[] args) { 
    Map<String, Object>out=new HashMap<String, Object>(); 

    Map<String,Object> hm=new HashMap<String, Object>(); 
    List<String> al= new ArrayList<String>(); 
    al.add("51b6f5fde4b0dd92df2c3270"); 
    al.add("51b866e9e4b021170dd1ae1c"); 
    hm.put("sDate","02-Oct-2015"); 
    hm.put("status","S"); 
    hm.put("SMSSentId", al); 
    out.put("Student1", hm); 

    Map<String,Object> hm1=new HashMap<String, Object>(); 
    List<String> al1= new ArrayList<String>(); 
    al1.add("51b6f5fde4b0dd92df2c3271"); 
    al1.add("51b866e9e4b021170dd1ae12"); 
    hm1.put("sDate","03-Oct-2015"); 
    hm1.put("status","S"); 
    hm1.put("SMSSentId", al1); 
    out.put("Student2", hm1); 

    Map<String,Object> hm2=new HashMap<String, Object>(); 
    List<String> al2= new ArrayList<String>(); 
    al2.add("51b6f5fde4b0dd92df2c3271"); 
    hm2.put("sDate","03-Oct-2016");//Year changed 
    hm2.put("status","S"); 
    hm2.put("SMSSentId", al2); 
    out.put("Student3", hm2); 
    //System.out.println(out); 

    for (Map.Entry<String, Object> entry : out.entrySet()) 
    { 
     // System.out.println(entry.getKey() + "/" + entry.getValue()); 
     for (Map.Entry<String, Object> entry1 : hm.entrySet()) 
     { 
      System.out.println(entry1.getKey() + "/" + entry1.getValue()); 

      if(entry1.getKey().equals("SMSSentId")) 
      { 

       int a= ((List<String>) entry1.getValue()).size(); 

       System.out.println(a); 
      } 
     } 
    } 
} 

我不知道如何修改此地图和list.Please给我建议它以正确的方式或不 或任何其他比较方法

我预计今年产量

# | Month | Year | TotalSMSSent 
1 Oct  2015 4 
2 Oct  2016 1 
+0

为什么要创建3个HashMap实例? 'hm,hm1,hm2'?然后只遍历'hm'? –

+0

你能帮我修改吗? –

+0

其实我想比较日期和基于我想要计算总SMSSentId,如果它是相同的月份和同一年 –

回答

1

我已经对你的代码做了一些修改,希望它能帮上忙。我创建了一个叫做记录的新类记录来保存你的所有记录。

我假定你的约会对象以字符串形式,因为你没有使用Java Date

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

public class Sorted { 

public static void main(String[] args) { 
    Map<String, Records> out = new HashMap<String, Records>(); 

    List<String> al = new ArrayList<String>(); 
    al.add("51b6f5fde4b0dd92df2c3270"); 
    al.add("51b866e9e4b021170dd1ae1c"); 
    Records record = new Records("02-Oct-2015", "S", al); 
    out.put("student1", record); 

    al = new ArrayList<String>(); 
    al.add("51b6f5fde4b0dd92df2c3271"); 
    al.add("51b866e9e4b021170dd1ae12"); 
    record = new Records("03-Oct-2015", "S", al); 
    out.put("Student2", record); 

    al = new ArrayList<String>(); 
    al.add("51b6f5fde4b0dd92df2c3271"); 
    record = new Records("03-Oct-2016", "S", al); 
    out.put("Student3", record); 
    process(out); 

} 

public static void process(Map<String, Records> records) { 
    HashMap<String, HashMap<String, Integer>> m = new HashMap<String, HashMap<String, Integer>>(); 
    HashMap<String, Integer> month = null; 
    for (String recordKey : records.keySet()) { 
     Records r = records.get(recordKey); 
     String s[] = r.getsDate().split("-"); 
     if (!m.containsKey(s[2])) { 
      month = new HashMap<String, Integer>(); 
      m.put(s[2], month); 
     } 
     HashMap<String, Integer> m1 = m.get(s[2]); 
     if (!m1.containsKey(s[1])) { 
      m1.put(s[1], records.get(recordKey).getSMSSent().size()); 
     } else { 
      int flag = m1.get(s[1]); 
      m1.put(s[1], flag + records.get(recordKey).getSMSSent().size()); 
     } 
    } 
    display(m); 
} 

public static void display(HashMap<String, HashMap<String, Integer>> d) { 
    int i = 0; 
    for (String s : d.keySet()) { 
     Map<String, Integer> m = d.get(s); 
     for (String s1 : m.keySet()) { 
      System.out.print(i++); 
      System.out.print("\t"); 
      System.out.print(s); 
      System.out.print("\t"); 
      System.out.print(s1 + "\t" + m.get(s1)); 

      System.out.println(""); 
     } 
    } 
} 

} 

class Records { 
private String sDate; 
private String status; 
private List<String> SMSSent; 

public Records(String sDate, String status, List<String> sMSSent) { 
    super(); 
    this.sDate = sDate; 
    this.status = status; 
    SMSSent = sMSSent; 
} 

public String getsDate() { 
    return sDate; 
} 

public void setsDate(String sDate) { 
    this.sDate = sDate; 
} 

public String getStatus() { 
    return status; 
} 

public void setStatus(String status) { 
    this.status = status; 
} 

public List<String> getSMSSent() { 
    return SMSSent; 
} 

public void setSMSSent(List<String> sMSSent) { 
    SMSSent = sMSSent; 
} 

} 

输出将是:

0 2016 Oct 1 
1 2015 Oct 4 

根据您的要求修改代码。

+0

不错的工作我祝贺你我花了很多时间,但我无法得到它。问题是它没有排序,当我们在另一个日期和月份添加一个假设2016年8月2日它会显示,但ID和一年不打印为什么?用于排序的 –

+0

可以使用另一个Map接口的实现。现在你可以检查所有的HashMap替换TreeMap – Adhikari

+0

@AraviS编辑的display()方法。 – Adhikari

0

请以下为一个想法。

final SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy"); 

     Map<String, Object> out=new HashMap<String, Object>(); 

     Map<String,Object> hm=new HashMap<String, Object>(); 
     List<String> al= new ArrayList<String>(); 
     al.add("51b6f5fde4b0dd92df2c3270"); 
     al.add("51b866e9e4b021170dd1ae1c"); 
     hm.put("sDate","02-Oct-2015"); 
     hm.put("status","S"); 
     hm.put("SMSSentId", al); 
     out.put("Student1", hm); 

     Map<String,Object> hm1=new HashMap<String, Object>(); 
     List<String> al1= new ArrayList<String>(); 
     al1.add("51b6f5fde4b0dd92df2c3271"); 
     al1.add("51b866e9e4b021170dd1ae12"); 
     hm1.put("sDate","03-Oct-2015"); 
     hm1.put("status","S"); 
     hm1.put("SMSSentId", al1); 
     out.put("Student2", hm1); 

     Map<String,Object> hm2=new HashMap<String, Object>(); 
     List<String> al2= new ArrayList<String>(); 
     al2.add("51b6f5fde4b0dd92df2c3271"); 
     hm2.put("sDate","03-Oct-2016");//Year changed 
     hm2.put("status","S"); 
     hm2.put("SMSSentId", al2); 
     out.put("Student3", hm2); 

     List<Integer> years = Arrays.asList(2015,2016); 
     List<Integer> months = Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12); 

     Map<String, Integer> report = new LinkedHashMap<String, Integer>(); 
     for (Integer year : years) { 
      for (Integer month : months) { 

       Integer smsCount = 0; 
       // loop on Students 
       for (Map.Entry<String, Object> outEntry : out.entrySet()) { 

        Map studentData = (Map)outEntry.getValue(); 

        String sentDateAsString = (String)studentData.get("sDate"); 
        Date sentDate = dateFormat.parse(sentDateAsString); 
        Calendar cal = Calendar.getInstance(); 
        cal.setTime(sentDate); 
        if (cal.get(Calendar.MONTH) == (month - 1) && cal.get(Calendar.YEAR) == year) { 
         List smsList = (List)studentData.get("SMSSentId"); 
         smsCount += smsList.size(); 
        } 
       } 

       report.put(String.format("Month %d-Year %d", month, year), smsCount); 
      } 
     } 

     System.out.println(report.toString()); 

输出将是如下(请注意,我还没有格式化输出,我刚打印出来的报告HashMap中)。

2015年1 - 2015年= 0,第2年 - 2015年= 0,第3年 - 2015年= 0,第4年 - 2015年= 0,第5年 - 2015年= 0, = 0,月份7 - 年份2015 = 0,月份8 - 2015年= 0,月份9 - 年份2015 = 0,月份10年份2015 = 4,月份11 - 2015年= 0, = 0,第1年2016年= 0,第2年2016年= 0,第3年2016年= 0,第4年2016年= 0,第5年2016年= 0,第6年2016年= 0 ,2016年7月2016年= 0,8年2016年= 0,9年2016年= 0,月10年2016 = 1,月11年2016年= 0,月12年2016 = 0 }

您可以尽可能地自定义代码,以达到您的目的。希望能帮助到你。