Java 按当前月份排序 List 到最近六个月

我的列表中有以下数据:

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

What am I trying to do?
I am trying to sort this data in a sequence such that the data is sorted by month and the month should start from the current month and count the previous six months.
For example:
Currently, it is May, and the data should be sorted in the following order:

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

如果缺了任何一个月,它应该直接跳过它,转到下一个月,并完成计数.

What I have tried so far?

我try 了以下代码来获取当前月份和之前的六个月:

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

推荐答案

我会这样做:

enum Month {
    JANUARY,
    FEBRUARY,
    MARCH,
    APRIL,
    MAY,
    JUNE,
    JULY,
    AUGUST,
    SEPTEMBER,
    OCTOBER,
    NOVEMBER,
    DECEMBER
}
public static void main(String[] args) {
    List<String> data = Arrays.asList("october", "april", "march", "november", "may", "january", "december");
    Month currentMonth = Month.MAY;
    List<String> thisYear = data.stream()
                                .filter(a -> Month.valueOf(a.toUpperCase()).ordinal() <= currentMonth.ordinal())
                                .collect(Collectors.toList());
    List<String> lastYear = data.stream()
                                .filter(a -> Month.valueOf(a.toUpperCase()).ordinal() > currentMonth.ordinal())
                                .collect(Collectors.toList());
    Comparator<String> monthComparator = new Comparator<String>() {

        @Override
        public int compare(String a, String b) {    
            Month mA = Month.valueOf(a.toUpperCase());
            Month mB = Month.valueOf(b.toUpperCase());
            return mB.compareTo(mA);
        }
    };
    thisYear.sort(monthComparator);
    lastYear.sort(monthComparator);
    
    thisYear.addAll(lastYear);
    System.out.println(thisYear);
}

Java相关问答推荐

JDK22执行repackage of goal org. springframework. boot:spring—boot—maven—plugin:3.2.3:repackage failed:unsupported class file major version 66—>

无法在org. openjfx:javafx—fxml:21的下列变体之间进行 Select

给定Java枚举类,通过值查找枚举

Java流传输一个列表并创建单个对象

SpringBoot Kafka自动配置-适用于SASL_PLAYTEXT的SSLBundle 包,带SCRAM-SHA-512

在执行流和相关操作时,使用Java泛型为2个方法执行相同的操作,但对象不同

try 将JSON字符串响应从API转换为映射字符串、对象>;时出错

将JSON字符串转换为Java类

在macOS上读取文件会导致FileNotFound,即使文件存在(并且具有权限)

将ByteBuffer异步写入InputStream或Channel或类似对象

一对多关系和ID生成

判断重复的两个二维表算法?

如何在右击时 Select 新行?

H2数据库仅支持%1个结果集?

在不带instanceof或switch的java中记录模式

为什么当我输入变量而不是直接输入字符串时,我的方法不起作用?

元音变音字符:如何在 Java 中将Á<0x9c>转换为Ü?

正在打印可选[值]

Spring Boot不创建数据库表

有没有办法维护在其外部的方法中创建但仅可用于类实例的变量?