Using HashMap as Collection in Java

JAXB does not allow you to undo existing xml structures in HashMap if they are not quite as JAXB expects them.

JAXB does a great job of handling, for example. LinkedList and fill them out.

I was thinking of creating an interface using the getKey() method and wrapping around the HashMap , taking all the objects that implement this interface. The shell can then use the getKey() method for all key-related map functions. After that, the shell can easily implement the Collection or List interface.

Since this idea does not seem innovative to me, I assume that it already exists in some kind of package, but I do not understand it correctly ... Can someone please name a good library that can do it or do it I have to do it?

+4
source share
2 answers

You might consider expanding ForwardingList for guava and using HashMap in reverse order. I do not know of any implementation that will leave you with only the actual mapping.
Another alternative is to create a JAXB XmlAdapter to adapt the values ​​to your map. I think this is more appropriate.

+3
source

If you are trying to convey Map content as a Collection , use Map.entrySet() . This gives you the object Set<Map.EntrySet<K,V>> ; that is, a set whose elements are key / value pairs for the Map parameter. To restore a Map from a collection, you need to iterate through the set and put for each element.

0
source

All Articles