1

I am a newbie in programming...

I have

List<DataProvider> dataProviders = new ArrayList<DataProvider>();

and expected output is

[DataProvider [date=2017-04-05 00:24:47.0, pendingtickets=1, closedtickets=0, createdtickets=1], DataProvider [date=2017-04-04 01:34:58.0, pendingtickets=1, closedtickets=0, createdtickets=1], DataProvider [date=2017-04-04 16:48:34.0, pendingtickets=1, closedtickets=0, createdtickets=1], DataProvider [date=2017-04-05 16:49:54.0, pendingtickets=1, closedtickets=0, createdtickets=1]]

i want to get a list which with no duplicated date and all the duplicated list value should sum up..

6
  • So you just want a List of distinct duplicate DataProviders? Commented Apr 15, 2017 at 14:40
  • No i want all of them with separate duplicates Commented Apr 15, 2017 at 14:41
  • Yes, that's what distinct means. Commented Apr 15, 2017 at 14:43
  • @user3576689 on what basis you want to compare objects . Do you wish to compare on all parameters or any specified fields Commented Apr 15, 2017 at 15:12
  • @SmashCode on same date . Commented Apr 15, 2017 at 15:14

4 Answers 4

2

If I understood what you want, you can make something like this:

Map<Date, DataProvider> res = new HashMap<Date, DataProvider>();
for (DataProvider dp : dataProviders) {
    if (!res.containsKey(dp.getDate())) {
        res.put(dp.getDate(), dp);
    }

    else{ 
        res.get(dp.getDate()).setPendingtickets(res.get(dp.getDate()).getPendingtickets() + dp.getPendingtickets());
        res.get(dp.getDate()).setClosedtickets(res.get(dp.getDate()).getClosedtickets() + dp.getClosedtickets());
        res.get(dp.getDate()).setCreatedtickets(res.get(dp.getDate()).getCreatedtickets());
    }
}
List<DataProvider> result = new ArrayList<DataProvider>(res.values());
Sign up to request clarification or add additional context in comments.

Comments

1

Try this

package com.stackoverflow.responses;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

class Dataprovider{
    private Date date;
    private int pendingTickets;
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }
    public int getPendingTickets() {
        return pendingTickets;
    }
    public void setPendingTickets(int pendingTickets) {
        this.pendingTickets = pendingTickets;
    }
    public Dataprovider(Date date, int pendingTickets) {
        super();
        this.date = date;
        this.pendingTickets = pendingTickets;
    }
    @Override
    public String toString() {
        return "[ pendingTickets=" + pendingTickets + "]";
    }
}

public class DateSort {

    /*
     * It is to count number of duplicates prevailing in given list
     * */
    public static int getCount(List<Dataprovider> dp,Date date){
        int count = 0;
        for(int i = dp.size()-1;i > -1; i--){
            if(dp.get(i).getDate().compareTo(date) == 0){
                count++;
            }
        }
        return count;
    }
    /*
     * returning list of particular index ie: date*/
    private static List<Dataprovider> getList(Map<Date, List<Dataprovider>> map, Date date) {
        return map.get(date);
    }
    public static void main(String[] args) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<Dataprovider> dataproviders = new ArrayList<Dataprovider>();
        try {
            dataproviders.add(new Dataprovider(dateFormat.parse("2016-01-10"), 1));
            dataproviders.add(new Dataprovider(dateFormat.parse("2016-01-10"), 0));
            dataproviders.add(new Dataprovider(dateFormat.parse("2016-01-09"), 0));
            dataproviders.add(new Dataprovider(dateFormat.parse("2016-01-07"), 1));
            dataproviders.add(new Dataprovider(dateFormat.parse("2016-01-07"), 1));
        } catch (ParseException e) {
            System.out.println(e.getMessage());
        }
        /*
         * if you just need the count of duplicates existing 
         * you could just use this result
         * */
        List<Dataprovider> output = dataproviders.stream().filter(n->DateSort.getCount(dataproviders, n.getDate())>1).collect(Collectors.toList());
        System.out.println(output);
        Map<Date,List<Dataprovider>> map = new HashMap<Date,List<Dataprovider>>();
        for(Dataprovider dp : dataproviders){
            /*
             * if map doesn't contain particular date 
             * adding date to map*/
            if(!map.containsKey(dp.getDate())){
                List<Dataprovider> temp = new ArrayList<Dataprovider>();
                temp.add(dp);
                map.put(dp.getDate(), temp);
            }else{
                List<Dataprovider> temp = new ArrayList<Dataprovider>();
                temp.addAll(getList(map,dp.getDate()));
                temp.add(dp);
                map.put(dp.getDate(), temp);
            }
        }
        /*
         * filtering and adding result to another map
         * */
        Map<Date,Dataprovider> outputMap = new HashMap<Date,Dataprovider>();
        for(Date date : map.keySet()){
            if(map.get(date).size()>1){
                int count = 0;
                for(int i = 0; i < map.get(date).size();i++){
                    count = count+map.get(date).get(i).getPendingTickets();
                }
                Dataprovider dataprovider = new Dataprovider(date, count);
                outputMap.put(date, dataprovider);
            }else{
                outputMap.put(date,map.get(date).get(0));
            }
        }
        for(Date date : outputMap.keySet()){
            System.out.println("date :: "+date+" "+outputMap.get(date));
        }
    }
}

Comments

1

You can use a Set and implements the equals method of DataProvider to return true when the date is the same.

public class DataProvider {

private Date date;
private int pendingtickets;
private int closedtickets;
private int createdtickets;

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    DataProvider other = (DataProvider) obj;
    if (date == null) {
        if (other.date != null)
            return false;
    } else if (!date.equals(other.date))
        return false;
    return true;
}

}

Comments

1

You should be able to use the following if you want to return a List containing distinct duplicates in dataProviders:

dataProviders.stream().filter(d -> Collections.frequency(dataProviders, d) > 1).distinct().collect(Collectors.toList());

To use this, you'll need to override Object#equals and Object#hashCode in DataProvider.java as well:

@Override
public boolean equals(Object o) {
    if (!(o instanceof DataProvider)) {
        return false;
    }

    return date.equals(((DataProvider) o).date);
}

@Override
public int hashCode() {
    return Objects.hash(date);
}

5 Comments

@user3576689 You most likely didn't override Object#equals and Object#hashCode then in DataProvider.java.
@user3576689 I've included the methods that you are required to override.
I did but it gives me null. i want to get this expected output [DataProvider [date=2017-04-04, pendingtickets=2, closedtickets=0, createdtickets=2], DataProvider [date=2017-04-05 , pendingtickets=2, closedtickets=0, createdtickets=2]]
@user3576689 What "gives you null"?
@user3576689 I won't be able to help unless you provide the correct information regarding what "gives [you] null".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.