javaarraylisthashmap

How to group by month based on date using java and calculate total count?


How to group by month based on date using java and calculate total count?

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

I dont know how to modify this map and list.Please give me suggestion its a correct way or not or any other comparator method

i expected this output

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

Solution

  • I have done some modifications to your code hope it will help.I have created a new class records called Records to hold all your records.

    I have assumed that your date are in string form since you are not using 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;
    }
    
    }
    

    The Output Will be :

    0   2016    Oct 1
    1   2015    Oct 4
    

    modify the code according to your requirement.