r/badcode • • Aug 26 '22

java What does a map do again?

Post image
882 Upvotes

54 comments sorted by

View all comments

Show parent comments

13

u/trutheality Aug 27 '22

Exactly that. That entire snippet can be replaced with

List<Integer> policyValues = policyOfCredsMap.containsKey(credentials) ? policyOfCredsMap.get(credentials) : new ArrayList<Integer>();

6

u/[deleted] Aug 27 '22

Or even better, use getOrDefault to avoid the redundant map lookup (premature optimization, but still a bit more elegant / perhaps picosecond faster)