java - Creating a Commons Collections MultiValueMap with a custom value collection type -


the 4.0 release of apache commons collections library has added generics support. having trouble converting code take advantage of it:

i multivaluemap takes string key, , collection of strings value. but:

  1. the keys should retain insertion ordering (so create multi-valued map decorating linkedhashmap)
  2. the values should unique each key , retain insertion ordering (so want values collection type linkedhashset).

the closest can is:

multivaluemap<string, string> orderedmap = multivaluemap.multivaluemap(     new linkedhashmap<string, collection<string>>(),      linkedhashset.class ); 

but produces error:

the method multivaluemap(map<k,? super c>, class<c>) in type multivaluemap not applicable arguments (linkedhashmap<string,collection<string>>, class<linkedhashset>)

so in generics hell. suggestions welcome.

prior version 4.0, accomplished following:

multivaluemap orderedmap = multivaluemap.decorate(     new linkedhashmap<>(),      linkedhashset.class ); 

simple! provide linkedhashmap decorate multivaluemap behaviour, , specify type of collection (linkedhashset) use values. requires casting when call put() , get() , i'd able use new generic version provided 4.0.

i consulted apache commons collections mailing list, it explained me interface multivaluemap known lacking, revamped in version 4.1 (see here jira issue , associated discussion).

so in future may have better solution, in meantime, rohit jain mentioned in answer, we're going have suppress warnings. however, since key aspect of type safety multivaluemap (not custom collection type), simplest way achieve is:

@suppresswarnings({ "rawtypes", "unchecked" }) multivaluemap<string, string> orderedmap =      maputils.multivaluemap(new linkedhashmap(), linkedhashset.class); 

note use of maputils factory method, rather more direct multivaluemap had used in original question.


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -