简体   繁体   English

使用foreach将arrayList与自身进行比较,不包括重复项

[英]Comparing an arrayList to itself using foreach and not including duplicates

I'm comparing an arrayList to itself using foreach. 我正在使用foreach将arrayList与自身进行比较。 I have an arrayList containing tips for waiters, each object has a date "dd-MM-yyy" and an amount (double), Now i want to add all transactions for the same day, so i get a total for the day that can be divided between the waiters. 我有一个包含服务员提示的arrayList,每个对象都有一个日期“dd-MM-yyy”和一个金额(double),现在我想在同一天添加所有交易,所以我得到了当天的总数在服务员之间分配。 Without duplicates. 没有重复。 I've looked all over especially here, but I can't seem to find a solution. 我特别喜欢这里,但我似乎无法找到解决方案。 I really hope you guys can help, I know it's a bit embarrassing, seeing as the problem being so simple, but I've been working on it for a couple of days now and I'm stuck. 我真的希望你们可以提供帮助,我知道这有点令人尴尬,因为这个问题很简单,但是我已经开始工作了几天而且我被困住了。

I had a longer algorithm but it wouldn't work and I couldn't find any solutions online, so i broke it all down to it's most basic components and checked for each step and pretty early on this problem occured: I'm using a local arrayList to make sure that I'm not comparing the same days to eachother over and over again. 我有一个更长的算法,但它不起作用,我找不到任何在线解决方案,所以我打破了它的最基本的组件,检查每一步,很早就出现了这个问题:我正在使用本地arrayList,以确保我不是一遍又一遍地比较相同的日子。 The if(!alreadyMade.contains(tips1.getTime()) followed by alreadyMade.add(tips1.getTime()) seems to be producing duplicates, which in my mind makes no sense. All I want is to add all the transactions for the same day from the same arrayList. if(!alreadyMade.contains(tips1.getTime())后跟hasMade.add(tips1.getTime())似乎正在产生重复项,这在我看来毫无意义。我只想添加所有的事务同一天从同一个arrayList。

public void dist(){ public void dist(){

    double day = 0;
    List<String> alreadyMade = new ArrayList<>();
    for (Tips tips : data.getTips()) {
        for (Tips tips1 : data.getTips()) {
            if(tips.getTime().equals(tips1.getTime())) {
                if (!alreadyMade.contains(tips1.getTime())){
                    alreadyMade.add(tips1.getTime());
                    day += tips.getTips();
                }
            }
        }
        System.out.println(day);
        day = 0;
    }
}

I wanted the print to be for a single day, but it is printing a lot of numbers that doesn't make sense 我想要打印一天,但它打印了很多没有意义的数字

I think you're trying to do something like this: 我想你正试图做这样的事情:

        Map<String,Double> alreadyMade = new HashMap<>();
    for (Tips tips : new ArrayList<Tips>()) {
        // If this time doesn't exist in the map then add it to the map with the
        // value tips.getTips().  If this time does exist in the map then add 
        // the value of tips.getTips() to the value that is already in the map.
        alreadyMade.merge(tips.getTime(), tips.getTips(), (Double a, Double b) -> a + b);
    }
    // go through each map entry.  The keys are the times and the values are the tip totals for that time.
    for (Map.Entry<String, Double> entry : alreadyMade.entrySet()) {
        System.out.println("Time: " + entry.getKey() + " Tips: " + entry.getValue());
    }

Note: I couldn't test this because I'm running Java 7 and this map function isn't available until java 8. 注意:我无法测试这个,因为我正在运行Java 7,直到java 8才能使用这个map函数。

In Java 8+ you can use the stream API to group by the time: 在Java 8+中,您可以使用流API按时间分组:

Map<Date, Integer> alreadyMade = data.getTips().stream()
  .collect(groupingBy(Tip::getTime, summingInt(Tip::getTips)));

I would do it like the following: 我会像下面这样做:

This is your Tip class(I think) 这是你的提示课(我认为)

public class Tip{
    Date date;
    float tip;
    public Tip(Date date, float tip){
        this.date = date;
        this.tip = tip;
    }
}

And this is the ("Algorithm") 这就是(“算法”)

//To Format the Dates
SimpleDateFormat ft = new SimpleDateFormat("dd-MM-yyyy");

//Input
ArrayList<Tip> tips = new ArrayList<Tip>();

//Just some Data for testing
tips.add(new Tip(ft.parse("11-04-2019"), 2.40F));
tips.add(new Tip(ft.parse("25-04-2019"), 3.30F));
tips.add(new Tip(ft.parse("25-04-2019"), 0.90F));



//Output
ArrayList<Date> dates = new ArrayList<Date>();
ArrayList<Float> sum = new ArrayList<Float>();

for(Tip tip : tips){  //Go through each Tip
  int match = dates.indexOf(tip.date);  //Look if the date is already in the array (if not -> -1)

  if(match == -1){  //If not add it
    dates.add(tip.date);
    sum.add(tip.tip);
  }else {  //If yes set it
    sum.set(match, sum.get(match) + tip.tip);
  }

}

//Output to console
for(int i = 0; i < dates.size(); i++){
  System.out.println(ft.format(dates.get(i)).toString() + " " + String.valueOf(sum.get(i)));
}

There is also a solution with maps or pairs but I never worked with them (not a professional coder). 还有一个地图或对的解决方案,但我从未与他们合作过(不是专业编码人员)。 Also make sure to try-catch the ParseException. 还要确保尝试捕获ParseException。 I Hope thats what you meant. 我希望那就是你的意思。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM