Home > Enterprise >  Replacing the places of the values using split and replace method
Replacing the places of the values using split and replace method

Time:11-12

This is my method, which return List of String value like this: 10x2021

And this is the value that I have sent to my method.

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

What I want to do is I want the output to look like 2021-10 , 2021-9, not 10x2021 . I could not use the methods of the string, exactly as I wanted. How to solve this problem?

CodePudding user response:

This should fix it for you:

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