簡體   English   中英

遍歷列表並根據日期分組

[英]Iterate through the list and grouping based on dates

我正在努力實現,在這里尋找基於日期對列表進行分組的方法。 我失去了如下屬性:

List<Attribute> attributes

其中Attribute.java如下

public class Attribute
{
    Integer getValue();
    List<String> getString();
    Date getDate();
}

我正在尋找一種方法,在遍歷Attribute列表的同時,我可以基於具有相同IntegerValue的日期(過去)創建元素列表(當前日期)和元素映射。

我的代碼如下:

List<Attribute> currentElement = new ArrayList<Attribute>();
Map<Integer, List<Attribute>> historicalElement = new HashMap<Integer, List<Attribute>>();

//iterating the entire list
for(final Attribute attribute : attributes) 
{
  if(attribute.getDate() == currentDate)
  {
      currentElement.add(attribute);
  }
  if(attribute.getDate() < currentDate)
  {
      historicalElement.put(attribute.getValue(), attribute)     
  }
}

該聲明

historicalElement.put(attribute.getValue(), attribute)     

不會因為工作

The method put(Integer, List<Attribute>) in the type Map<Integer,List<Attribute>> is not applicable for the arguments (Integer,   Attribute).

有什么方法可以實現該映射,而不是將強制類型轉換為List。

謝謝 !!!

投射到列表完全沒有幫助。 您只會得到ClassCastException 最簡單的方法可能是這樣的:

if(attribute.getDate() < currentDate)
{
    List<Attribute> list = historicalElement.get(attribute.getValue());
    if(list == null){
        list = new ArrayList<>();
        historicalElement.put(attribute.getValue() , list);
    }

    list.add(attribute);  
}

編輯:保羅的答案在這里更好。

看起來像番石榴多圖的工作,您可以在其中執行以下操作:

Map<Integer, List<Attribute>> historicalElement = Multimaps.newListMultimap();
for(final Attribute attribute : attributes) {
    historicalElement.put(attribute.getValue(), attribute)
}

應該這樣做。

好吧,除了您還要按日期分組? 有點棘手。

首先,您需要修正日期比較。 您不使用==運算符比較日期。

現在,在地圖中添加新條目時,您首先需要檢查現有鍵。 如果不存在,則使用新值創建一個新的ArrayList

if(attribute.getDate().compareTo(currentDate) < 0) {
    if (historicalElement.containsKey(attribute.getValue())) {
        historicalElement.get(attribute.getValue()).add(attribute);
    } else {
        historicalElement.put(attribute.getValue(), 
             new ArrayList<Attribute>(Arrays.asList(attribute)));     
    }
}

如果您使用的是Java 8,則可以直接使用Map#merge()方法來避免進行額外的測試:

if(attribute.getDate().compareTo(currentDate) < 0) {
    historicalElement.merge(
                attribute.getValue(), 
                new ArrayList<Attribute>(Arrays.asList(attribute)), 
                ArrayList::addAll);
}

您還可以在此處使用Stream API和lambda:

List<Attribute> currentElement = attributes.stream()
      .filter(a -> a.getDate().compareTo(currentDate) == 0)
      .collect(Collectors.toList());
Map<Integer, List<Attribute>> historicalElement = attributes.stream()
      .filter(a -> a.getDate().compareTo(currentDate) < 0)
      .collect(Collectors.groupingBy(Attribute::getValue));

你不是試圖把單一 AttributeMap它需要一個List屬性,在這里:

//iterating the entire list
for(final Attribute attribute : attributes) 
{
  if(attribute.getDate() == currentDate)
  {
      currentElement.add(attribute);
  }
  if(attribute.getDate() < currentDate)
  {
      historicalElement.put(attribute.getValue(), attribute)  // HERE   
  }
}

如果希望它是單個屬性,則應更改為:

來自: Map<Integer, List<Attribute>> historicalElement = new HashMap<Integer, List<Attribute>>();

到: Map<Integer, List<Attribute>> historicalElement = new HashMap<Integer, Attribute>() ;

暫無
暫無

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

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