在ArrayList中查找重复值

时间:2017-04-15 14:36:19

标签: java arrays list arraylist hashset

我是编程新手......

我有

company = create(:company)
number_of_users = 5
create_list(:user, number_of_users, company: company)

和期望的输出是

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

我想获得一个没有重复[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]] 的列表,所有重复的列表值应该总结..

4 个答案:

答案 0 :(得分:2)

如果我理解你想要的东西,你可以做这样的事情:

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

答案 1 :(得分:1)

如果您想要在List中返回包含不同重复项的dataProviders,则应该能够使用以下内容:

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

要使用此功能,您还需要覆盖Object#equals中的Object#hashCodeDataProvider.java

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

答案 2 :(得分:1)

您可以使用Set并实现DataProvider的equals方法,以便在日期相同时返回true。

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

}

答案 3 :(得分:1)

试试这个

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