簡體   English   中英

多個鍵到1個鍵,但值是和

[英]Multiple keys to 1 key but values as sum

顯然,我找到了與此相關的另一個主題,但是它並沒有任何幫助。

我的問題:輸出確實

  1. 不包含所有牌照(36個中只有5個以某種方式出現在輸出中,不知道那怎么可能)
  2. 如果它包含一個車牌,則僅包含最后一個(這可能是因為我沒有正確處理多個鑰匙)

如下所示,問題出在if語句中。 那么if語句應該怎么做?

  • 如果before = true,請查看nachtMap是否已經包含等於licenseplateList.get(i)的密鑰。
  • 如果確實包含該值,則將值(kmperlineList.get(i))添加到該鍵的值(即值+ kmperlineList.get(i))。
  • 如果其中不包含該密鑰,則只需將licenseplateList.get(i)作為密鑰,然后添加值kmperlineList.get(i)

    • 如果在!= true之前,請查看after是否為真,
  • 如果after為true,則仍對nachtMap執行此操作(也包含blahblah ..,則完全相同)
  • 如果after不正確,則對dagMap執行此操作(也包含blahblah,則完全相同,但對dagMap執行此操作)

對於整個代碼,請檢查: https : //pastebin.com/jaSTm1DR

if (before) {

    // does our map already contain this license?
    if (nachtMap.containsKey(licenseplateList.get(i))) {
        kmperlineList.get(i).replaceAll(",", ".");

        // then remove that one, add new one but have the sum of the values
        // is this extra ) after ".get(i)" correct?
        Double sum = nachtMap.remove(licenseplateList.get(i)) + Double.parseDouble(kmperlineList.get(i));
        nachtMap.put(licenseplateList.get(i), sum);
    }
    // if it doesnt contain the license yet, just put it in there
    else {
        kmperlineList.get(i).replaceAll(",", ".");
        nachtMap.put(licenseplateList.get(i), Double.parseDouble(kmperlineList.get(i)));
    }
}
// if its not before 06:00
else {

    // als de tijd na 17:30 is
    if (after) {
        if (nachtMap.containsKey(licenseplateList.get(i))) {
            Double sum = nachtMap.remove(licenseplateList.get(i)) + Double.parseDouble(kmperlineList.get(i));
            nachtMap.put(licenseplateList.get(i), sum);
        } else {
            kmperlineList.get(i).replaceAll(",", ".");
            nachtMap.put(licenseplateList.get(i), Double.parseDouble(kmperlineList.get(i)));
        }

    }
    // otherwise it's always day
    else {

        // again: does the map already contain this license plate?
        if (dagMap.containsKey(licenseplateList.get(i))) {
            System.out.println("works");// debug
            kmperlineList.get(i).replaceAll(",", ".");

            // then get the sum of the values, but only 1 time the key
            Double sum = dagMap.remove(licenseplateList.get(i)) + Double.parseDouble(kmperlineList.get(i));
            dagMap.put(licenseplateList.get(i), sum);
        }

        // if the map does not contain the license plate
        else {

            kmperlineList.get(i).replaceAll(",", ".");
            dagMap.put(licenseplateList.get(i), Double.parseDouble(kmperlineList.get(i)));
        }
    }
}

您的代碼過於復雜和多余。

但是首先,似乎您的文件使用的是非美國數字格式( 1000,00 ,而不是1000.00 ),因此您應該使用具有正確Locale NumberFormat來解析數字,而不是使用Double.parseDouble來解析Java語法數字。

假設使用Java 8(因為所有早期版本現在都已經停產),您的代碼可以簡化為:

double kmperline = NumberFormat.getNumberInstance(Locale.GERMANY)
                               .parse(kmperlineList.get(i)).doubleValue();
Map<String, Double> mapToUpdate = (before || after ? nachtMap : dagMap);
mapToUpdate.merge(licenseplateList.get(i), kmperline, Double::sum);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM