0

I need to find a object in java based in the property of its many subojects.

What I currently have is ugly as hell and I'm sure there's a more efficient way to do so. Probably with a library like hamcrest, or maybe directly with Java (my knowledge in Java is not the best).

This is what I have so far:

private HotelResult findHotelResult(List<HotelResult> hotelResultsList, HotelSelection hotelSelection) {
    for (HotelResult hotelResult : hotelResultsList)
        for (RoomOption roomOption : hotelResult.getRoomOptions())
            for (RoomTypeIds roomTypeIds : roomOption.getRoomTypeIds())
                for (RoomRateIds roomRateIds : roomTypeIds.getRoomRateIds())
                    if ( roomRateIds.getId().equals(hotelSelection.getResultId()) )
                        return hotelResult;

    (...)
}

Thank you in advance.

3
  • 2
    If you are using java 8 or above, have you considered using the stream API? Commented Dec 9, 2019 at 11:19
  • Things like this make me integrate Kotlin code to my projects. However, above comment has good point - streams are good alternative Commented Dec 9, 2019 at 11:36
  • It's a good idea, looks like what I need. Commented Dec 9, 2019 at 14:27

2 Answers 2

1

Try this if you are using Java 8 o higher...

private HotelResult findHotelResult(List<HotelResult> hotelResultsList, HotelSelection hotelSelection) {
    Optional<HotelResult> found = hotelResultsList.stream().filter((r) -> {
        Optional<RoomRateIds> optId = r.getRoomOptions().stream().flatMap(o -> o.getRoomTypeIds().stream())
                .flatMap(rate -> rate.getRoomRateIds().stream())
                .filter(id -> id.getId().equals(hotelSelection.getResultId())).findFirst();
        return optId.isPresent();
    }).findFirst();

    return found.orElse(null);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Works like a charm. I have to apply it to different places now, will be good to have this as an example to get introduced into stream. Thank you for taking the time to write it.
1

There is no more efficient way of doing so, at least not without re-organizing your data model from what it currently is into something completely different.

There is nothing wrong with nice, cleanly laid out nested loops, making very clear what is happening. You could perhaps replace them with a sequence of forEach( ...forEach( ... forEach( ... ) ) ) but then you will have an undecipherable tangle of parentheses to deal with, the purpose of the code will be less clear, debugging the code will become next to impossible, and performance will suffer.

The only thing that could be improved in the code that you have shown us would be to either get rid of the unnecessary blank lines, or get rid of the egyptian-style curly braces, or both, since every single curly brace in that code is unnecessary.

That, alone, might make the code look less as if it could use some improvement. Since it doesn't.

1 Comment

Using stream worked well in the end, but you are right that how I posted might look easier to the eyes. However, since I can get even more complex scenarios than this, I wanted to see some tool that would help me to deal with it differently. Btw, I do have it without the keys and spaces, but wanted to make sure it was easy to read here by everyone :)

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.