I've got a simple LinkedHashMap indexes
:
[indexes: [909-720-15, EC5640001, BC2480001, BC2510001, BC2570001, EA0850002, EA0850003, EA1310005, EA1310006, EA1310008, EA1310009, EA1310010, EA1310011, EA1310012, EB7320001, EB7320002, EB7320003, EB7320004, EB7400001, EB7400002, ED93301, ED93302, ED93501]]
How I can convert this map to the List
?
If I use
List valueList = new ArrayList(indexes.values());
I receive a nested list: [[909-720-15, EC5640001, BC2480001, BC2510001, BC2570001, EA0850002, EA0850003, EA1310005, EA1310006, EA1310008, EA1310009, EA1310010, EA1310011, EA1310012, EB7320001, EB7320002, EB7320003, EB7320004, EB7400001, EB7400002, ED93301, ED93302, ED93501]]
I initialize my LinkedHashMap in a separate Groovy component:
def indexes = [indexes:indexes]
And pass it in a body of a POST call to the Java component
Maps and Lists are fundamentally different data types, so there is no clear general mechanism for converting between the two.
Any way that preserves all the information present in the map would yield a list of Map.Entry
objects or of some other representation of the (key, value) pairs in the map. For example,
List<Map.Entry<KeyType, ValueType>> entryList = new ArrayList<?>(indexes.entrySet());
(Where KeyType
and ValueType
are placeholders for the actual types of your keys and values. It's unclear what these are in your case, but maybe String
.)
But as I understand it, you want instead the value associated with one specific key, expressed as a List. In fact, you seem to have said that the value in question is already a List, in which case you do not necessarily need to "convert" the map to a list, as simply retrieving the wanted value from it may be sufficient:
List<ValueType> indexList = indexes.get("indexes"); // if indexes were properly paramterized
As I wrote in comments, you really ought not to work with raw versions of generic types, but if that's out of your control then you'll need to cast. Supposing that the values in your map are lists of String
s, as seems to be the case, this would be one reasonable approach:
List<String> indexList = (List<String>) indexes.get("indexes");
Note in particular that just because you receive the data in the form of a raw Map
(if indeed you do) does not mean that you cannot transition to properly parameterized types, especially since you'll need to cast anyway.