2

On the way of refactoring the below code snippet to java8 using streams

private Date getDate(List<BrowseHistory> historyList, String subscribe, String cancelled) {
    for(int i=0; i<= historyList.size(); i++) {
        if(isContainsStatus(historyList.get(i), subscribe) && isExist(historyList, cancelled, i)) {
            return historyList.get(i).getCreated()
        }
    }
    return null;
}

private boolean isExist(List<BrowseHistory> historyList, String status, int i) {
        return historyList.size() == i+1 || (isContainsStatus(historyList.get(i+1), status) || isContainsStatus(historyList.get(i+1), Status.PEND.toString()));
    }

private boolean isContainsStatus(BrowseHistory history, String status) {
        return history.getStatus().contains(status);
    }

I could able to refactor till the if block, with java8 filter.

Below is the Java8 refactored code

private Date getDate(List<BrowseHistory> historyList, String subscribe, String cancelled) {
return IntStream.range(0, historyList.size())
        .filter(i -> isContainsStatus(historyList.get(i), subscribe) && isStatusExist(historyList, cancelled, i))
        .mapToObj(historyList::get)
        .map(BrowseHistory::getCreated)
        .findFirst() // find first that got through those filters
        .orElse(null);

}

Edited the java8 refactored code based on the below answer.

4
  • 4
    You're returning exactly the same in both branches of the if. Besides, I think you have a problem with the index as it will be out of bounds in the last iteration (or maybe in the previous one). Why are you iterating by the index instead of directly over the elements of the list? Commented Feb 4, 2018 at 3:45
  • I dont think it will throw index out of bound because of the inner if, and coming to the index iteration I always wants to check the next element in the list if I'm in the current element. Is there any way to do this with iterating over the elements? Commented Feb 4, 2018 at 13:46
  • what is userSubscriptionPeriodList ? Commented Feb 4, 2018 at 16:08
  • @Eugene I have edited the posted question. Thanks Commented Feb 4, 2018 at 18:49

1 Answer 1

4

There should be identical method in java 8 to the one that you had there pre-java-8.

private Date getDate(List<BrowseHistory> historyList, String subscribe, String cancelled) {
    return IntStream.range(0, historyList.size())
            .filter(i -> isContainsStatus(historyList.get(i), subscribe) && isStatusExist(historyList, cancelled, i))
            .mapToObj(historyList::get)
            .map(BrowseHistory::getCreated)
            .findFirst() // find first that got through those filters
            .orElse(null);
}

Few notes though:

  • In for loop conditional i <= historyList.size() I would rather use i < historyList.size()
  • The Java-8 solution identical to your method isn't too good looking, there might be another solutions, but this would be one.
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks KKaar, your suggestion is much helpful. edited the posted question based on the suggestion provided by you
No problem @Digital, if there is still something that's bothering related to this question just let know. Kind of everything is doable with Java-8. Some solutions just are not so good looking so the actual solution in J8 might not be the best one to read to. Edited answer to relate the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.