You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Borut Jogan (Jira)" <ji...@apache.org> on 2020/08/01 15:08:00 UTC

[jira] [Commented] (COLLECTIONS-533) Add a MultiValuedLinkedHashMap to preserve insertion order

    [ https://issues.apache.org/jira/browse/COLLECTIONS-533?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17169310#comment-17169310 ] 

Borut Jogan commented on COLLECTIONS-533:
-----------------------------------------

Hi, I just signed up because an ArrayListValuedLinkedHashMap would be very useful for my job, and I would like to say my two cents on the matter of the insertion order.

Other implementations of the MultiValuedMap clearly state the backing Map and the Collection used for the value. If the value is a HashSet or an ArrayList, I expect that the elements mapped *to that key* will be retrieved in a certain order (or not, in the HashSet case).

So, as a potential user of an ArrayListValuedLinkedHashMap, the issue that [~dipanjan21] pointed out is for me the expected behavior (at least for my use case), because the "LinkedHashMap" part refers to the keys, not the pair key+value. So when iterating over the collection I expect:
 # Get the *first* element associated with the *first* key which was inserted.
 # Get the second element associated with the first key which was inserted.
 # ...
 # Get the n-th element associated with the first key which was inserted.
 # Get the *first* element associated with the *second* key which was inserted, etc.

so, when doing

 
{code:java}
MultiValuedMap<String, String> map = new ArrayLisValuedLinkedHashMap<>();
map.put((K)"a", (V)"a1");
map.put((K)"b", (V)"b1");
map.put((K)"a", (V)"a2");
{code}
From my point of view I would really expect:
 * a -> a1
 * a -> a2
 * b -> b1

The whole point of using an ArrayList, or a HashSet or a TreeSet for the Map's value would have no meaning if this wouldn't be the case (again: for my use case).

 

I see the behavior that you expect as another interpretation of the meaning of "insertion order" we want to consider (only the keys or the pair key+value), and the two meanings are not mutually exclusive, so I also think that a MultiValuedLinkedHashMap as per your interpretation and an ArrayListValuedLinkedHashMap as per mine interpretation could coexist in the Commons Collections Framework and could serve different use cases.

 

I hope that my reasoning makes sense!

> Add a MultiValuedLinkedHashMap to preserve insertion order
> ----------------------------------------------------------
>
>                 Key: COLLECTIONS-533
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-533
>             Project: Commons Collections
>          Issue Type: New Feature
>          Components: Map
>            Reporter: Benedikt Ritter
>            Priority: Major
>              Labels: github
>             Fix For: 4.x
>
>
> Placeholder ticket for https://github.com/apache/commons-collections/pull/3



--
This message was sent by Atlassian Jira
(v8.3.4#803005)