我的串列中有以下資料:
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();
}
uj5u.com熱心網友回復:
這里有一種替代方法:
- 從API的
Month列舉中獲取月份串列java.time 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);
}
uj5u.com熱心網友回復:
假設當前月份將獲得整數 0(或 11),然后為每個月分配一個連續的 int。
例如,五月 = 0,六月 = 1,.... 一月 = 8...
然后,對您的輸入陣列進行排序,然后從那里開始,問題真的很簡單
uj5u.com熱心網友回復:
這是我的做法:
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);
}
uj5u.com熱心網友回復:
你快到了。你缺少的是兩件事:
- 您忘記過濾掉超過 6 個月前的月份。
- 你得到了相反的比較(所以你看到的是升序而不是降序)。
以下同時進行過濾和排序:
Comparator<FeatureAnalyzeDTOResult> comp =
Comparator.comparingInt(o -> monthList.indexOf(o.getMonth().toUpperCase()));
list = list.stream()
.filter(o -> monthList.contains(o.getMonth().toUpperCase()))
.sorted(comp)
.toList();
System.out.println(list);
輸出:
[Feature: may Count: 46
, Feature: april Count: 46
, Feature: march Count: 46
, Feature: january Count: 53
, Feature: december Count: 30
]
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/474641.html
