[ 
https://issues.apache.org/jira/browse/COLLECTIONS-602?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Bruno P. Kinoshita resolved COLLECTIONS-602.
--------------------------------------------
       Resolution: Fixed
    Fix Version/s: 4.2

Patch applied. All tests pass, coverage of the get method kept at 100%, no 
issues found in CPD, PMD, FindBugs, or Checkstyle. Nice and simple improvement, 
if the object exists in the map, it will avoid some work. The class javadocs 
cover thread safety, and this change is aligned with what is stated there.

Thanks for taking your time to submit this patch [~jmark].

Cheers
Bruno

> Improve efficiency of DefaultedMap.get
> --------------------------------------
>
>                 Key: COLLECTIONS-602
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-602
>             Project: Commons Collections
>          Issue Type: Bug
>            Reporter: John Mark
>            Assignee: Bruno P. Kinoshita
>              Labels: help-wanted
>             Fix For: 4.2
>
>         Attachments: COLLECTIONS-602.patch
>
>
> The current implementation of 
> {{org.apache.commons.collections4.map.DefaultedMap.get()}} is inefficient 
> since it always require two map lookups. There is no need to check 
> map.containsKey() every time. I would suggest implementing this method 
> similar to the way that Java 8 does with {{Map.getOrDefault()}}. My proposed 
> implementation would be something like this:
> {code}
> public V get(final Object key) {
>     V v;
>     return ((v = map.get(key) != null) || map.containsKey(key)) ? v : 
> value.transform((K) key);
> }
> {code}
> It can be further optimized to not even call map.containsKey() if map is 
> known to not contain null values.



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

Reply via email to