使用拆分和替换方法替换值的位置

Replacing the places of the values using split and replace method

这是我的方法,return 字符串值列表如下:10x2021

这是我发送到我的方法的值。

 List<HistoryItem> historyItems = Arrays.asList
                (
                        new HistoryItem(364, "10x2021", Long.valueOf("009"))

                );



public static List<String> foo(List<HistoryItem> historyItems) {
    List<HistoryItem> collect = historyItems.stream().filter(item -> item.getCreditStatus().equals(Long.valueOf("009")) || item.getCreditStatus().equals(Long.valueOf("0010")))
            .collect(Collectors.toList());
    List<String> reportingPeriods = new ArrayList<>();
    for (HistoryItem historyItem : historyItems) {
        reportingPeriods.add(historyItem.getReportingPeriod());
    }

    return reportingPeriods;
}

我想要做的是让输出看起来像 2021-10 , 2021-9, not 10x2021 。我无法完全按照我的意愿使用字符串的方法。如何解决这个问题?

这应该可以为您解决:

List<String> reportingPeriods = historyItems.stream().map(hI -> { 
String[] splitted = hI.getReportingPeriod().split("x");
return splitted [1] + "-" + splitted [0]; }).collect(Collectors.toList());