gpt4 book ai didi

java - 按当前月份排序 List 到最近六个月
转载 作者:行者123 更新时间:2023-12-05 00:42:45 25 4
gpt4 key购买 nike

我的列表中有以下数据:

    List<FeatureAnalyzeDTOResult> list = new ArrayList<>();
list.add(new FeatureAnalyzeDTOResult("october", 46));
list.add(new FeatureAnalyzeDTOResult("april", 46));
list.add(new FeatureAnalyzeDTOResult("march", 46));
list.add(new FeatureAnalyzeDTOResult("november", 30));
list.add(new FeatureAnalyzeDTOResult("may", 46));
list.add(new FeatureAnalyzeDTOResult("january", 53));
list.add(new FeatureAnalyzeDTOResult("december", 30));

我想做什么?
我正在尝试按顺序对这些数据进行排序,以便数据按月份排序,月份应从当前月份开始并计算前六个月。
例如:
目前是五月,数据应该按以下顺序排序:

[MAY, APRIL, MARCH, FEBRUARY, JANUARY, DECEMBER]    

如果缺少任何月份,它应该直接跳过它并继续下个月并完成计数。

到目前为止我尝试了什么?

我已尝试使用以下代码获取当前月份和前六个月:

        YearMonth thisMonth = YearMonth.now();
String[] month = new String[6];
for (int i = 0; i < 6; i++) {
YearMonth lastMonth = thisMonth.minusMonths(i);
DateTimeFormatter monthYearFormatter = DateTimeFormatter.ofPattern("MMMM");
month[i] = lastMonth.format(monthYearFormatter);
month[i] = month[i].toUpperCase();
}

List<String> monthList = Arrays.asList(month);
System.out.println(monthList);

我也尝试过编写 Comparator 但它没有按预期工作。我对编写 Comparator 的逻辑有点困惑。

        Comparator<FeatureAnalyzeDTOResult> comp = (o1, o2)
-> monthList.indexOf(o2.getMonth().toUpperCase()) - monthList.indexOf(o1.getMonth().toUpperCase());
list.sort(comp);

输出如下:

     [Feature: december Count: 30 
, Feature: january Count: 53
, Feature: march Count: 46
, Feature: april Count: 46
, Feature: may Count: 46
, Feature: october Count: 46
, Feature: november Count: 30]

这里是 FeatureAnalyzeDTOResult 类供引用:

class FeatureAnalyzeDTOResult {

private String month;
private int count;

public FeatureAnalyzeDTOResult(String feature, int count) {
this.month = feature;
this.count = count;
}

public FeatureAnalyzeDTOResult() {
}
public String getMonth() {
return month;
}

public void setMonth(String feature) {
this.month = feature;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

@Override
public String toString() {
StringBuilder string = new StringBuilder();
string.append("Feature: ").append(getMonth()).append(" Count: ").append(getCount()).append(" \n");
return string.toString();
}

最佳答案

这里有另一种方法:

  • Month 获取月份列表java.time API
  • 中的枚举
  • 使用 Collections.rotate 轮换列表和当前月份值
  • 使用 Collections.reverse 反转列表
  • 根据上述列表的索引创建一个比较器来比较月份
  • 流式传输、排序和限制

类似

import java.util.Collections;
import java.util.Comparator;

public static void main(String[] args) {
List<FeatureAnalyzeDTOResult> list = new ArrayList<>();
list.add(new FeatureAnalyzeDTOResult("october", 46));
list.add(new FeatureAnalyzeDTOResult("april", 46));
list.add(new FeatureAnalyzeDTOResult("march", 46));
list.add(new FeatureAnalyzeDTOResult("november", 30));
list.add(new FeatureAnalyzeDTOResult("may", 46));
list.add(new FeatureAnalyzeDTOResult("january", 53));
list.add(new FeatureAnalyzeDTOResult("december", 30));


List<Month> months = new ArrayList<>(Arrays.asList(Month.values()));
Collections.rotate(months, 12 - YearMonth.now().getMonthValue());
Collections.reverse(months);

Comparator<FeatureAnalyzeDTOResult> comp =
Comparator.comparingInt(f -> months.indexOf(Month.valueOf(f.getMonth().toUpperCase())));

list.stream().sorted(comp).limit(6).forEach(System.out::println);
}

关于java - 按当前月份排序 List<Object> 到最近六个月,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72204683/

25 4 0