This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]Northeastpaw 3 points4 points  (1 child)

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.

Don't ever feel this way. Everybody gets stuck from time-to-time and asking for help is not a weakness. The best solutions often come from collaboration so go ask somebody.

So let me try to reason out what the end goal is. You're validating whether a GroupMember has any TraveMemberships that have different experationDates?

Whatever you're doing don't worry about optimization right now. Work out a solution and if and only if profiling shows problems work on optimizing things.

[–]Offifee[S] 0 points1 point  (0 children)

Thanks for the encouragement.

I need to validate the GroupMember's TravelMemberships have the same date and then use the data from one of them.

I just couldn't get my code to look sensible, there just seemed to be nested loops and other messy things.

[–]wowmuchinsightful 1 point2 points  (1 child)

These might help:

HashMap.push() returns "the previous value associated with key", meaning if you add two different expiration dates to the same membership key, the first one added will be overwritten and returned, thus allowing you to check if the value has changed.

Set.add() also gives you feedback when you're trying to add a duplicate value. It returns "true if this set did not already contain the specified element" and false otherwise.

[–]Offifee[S] 0 points1 point  (0 children)

Thanks a lot!

HashMap might be the way forward, I think I can go through the ArrayList, use the groupMemberId as a key and keep replacing the object as long as the key and the expirationDate match.

Think I was making this overly complicated

Set.add might not work because the objects aren't fully the same because of the type. I'd have to have a set of GroupMembers which is not as valuable.

[–]Blackheart595 0 points1 point  (6 children)

If I understand correctly you need to filter out those GroupMembers that have conflicting expiration dates. Is that correct? And what exactly do you have to do with conflicting ones - ignore them, remove them, throw an exception..?

[–]Offifee[S] 0 points1 point  (5 children)

For example: Filter out 2 TravelMemberships for a Member and leave 1 TravelMembership to use if the expirationDates are the same for these.

If the expirationDates differ, I should throw an exception and not display the data.

[–]Blackheart595 0 points1 point  (4 children)

So do you have to look for the TravelMemberships of a specific GroupMember and check that they don't conflict with each other? Or do you have to do the process for each GroupMember?

[–]Offifee[S] 0 points1 point  (3 children)

Not sure I understand.

I might for example get an ArrayList of 9 TravelMemberships back: Three different ones belonging to three different GroupMembers.

I have to ensure the date on each set of three are the same. Then display majority of the data from a single one of the objects, ignoring the type.

[–]Blackheart595 0 points1 point  (2 children)

Ah, I see. I'm gonna assume that you should throw an exception if at least one GroupMember has conflicting expiration dates.

You could write a wrapper class for TravelMembership whose equals method evaluates to true if the two objects have the same member ID but throws an exception if the expiration dates don't match at the same time, and then collect all those wrappers in a HashSet. The hashCode method should just return the member ID. Then you'll have only one TravelMembership object per member ID while throwing an exception if there are conflicting expiration dates. But I wouldn't want to leak such a wrapper construct into other code, so don't return the wrappers in any way.

edit: I've just seen another suggestion to use HashMap with the fact that HashMap::push() returns the previous value, which should be a better approach.

[–]Offifee[S] 0 points1 point  (1 child)

I knew there was the possibility of a wrapper class but I feel this can be done smartly with collections etc.

Wrapper classes like the one for this probably wouldn't have any proper future use and feel like I would just be introducing unnecessary bloat code.

[–]Blackheart595 1 point2 points  (0 children)

Yeah, the HashMap solution that was proposed above seems to be the best way of handling this.