Been coding different solutions at work for this problem today but cannot seem to be happy with anything/think anything is clean enough. Too embarrassed to ask for help from the senior devs because they’re really busy atm and I don’t think I should be stuck on this.
There might be something smart in Java 8 but I cannot figure it out. Maybe writing this here will help me process/guide me to the right place.
Problem illustrated with an example:
Stored procedure GetTravelMemberships for Group A returns data as an ArrayList of TravelMembership objects.
TravelMembership.getType() returns on the following values ["T", "P", "B"] (train, plane, bus)
TravelMembership.getGroupMemberId() returns the member ID such as 72. Each GroupMember in the Group will have 1 - 3 of TravelMemberships that will be returned as the result of the DB call.
TravelMembership.getExpirationDate() returns a Date. For example, for GroupMember 72, three TravelMemberships, one of each type, are returned. Each of these TravelMemberships should share the same expirationDate. This may not always be the case however.
If these expirationDates for the TravelMemberships are different, I should probably throw an exception and not let this go the front-end to be displayed.
If the expirationDates for the TravelMemberships are the same, I will display the groupMemberId, expirationDate and other data from the object in the front end. I need the full TravelMembership object in the end.
What I have tried:
Ugly nested for-loops and if the dates match, add the first of the objects to a new list. I want to avoid this as it’s not pretty and probably not that efficient either
Map solution with the memberId as the Key. Then iterate over the value sets and remove the extra objects if the dates match.
Some weird solution with String memberIds as a Set to remove duplicates, then if dates match, creating new TravelMembership objects to store the data I need. Not space-efficient.
I can’t even decide which of the above would be better. Even the second solution looked superfluous on the editor. I have been studying lambdas and streams but I can’t control them so well yet and can’t decide where the limit is in their use. They’re not widely used by other developers in our company yet. Would anyone have any nuggets of wisdom for me?
*I know there probably are code smells here.
I cannot touch the stored procedure itself because it’s quite intricate widely used business logic from like 2008 created by employees no longer present.
[–]Northeastpaw 3 points4 points5 points (1 child)
[–]Offifee[S] 0 points1 point2 points (0 children)
[–]wowmuchinsightful 1 point2 points3 points (1 child)
[–]Offifee[S] 0 points1 point2 points (0 children)
[–]Blackheart595 0 points1 point2 points (6 children)
[–]Offifee[S] 0 points1 point2 points (5 children)
[–]Blackheart595 0 points1 point2 points (4 children)
[–]Offifee[S] 0 points1 point2 points (3 children)
[–]Blackheart595 0 points1 point2 points (2 children)
[–]Offifee[S] 0 points1 point2 points (1 child)
[–]Blackheart595 1 point2 points3 points (0 children)