Reputation: 2467
I have a Map Map<Integer, List>
with key, values. I need to convert this into List and pass it to the calling function. Once I have the list i need to convert this back to Map<Integer, List>
The reason I am converting the map to list is because i need to create webservice for this method. As I cannot expose the Map to webservice I need to convert this to list.
How to achieve this..?
Upvotes: 1
Views: 4009
Reputation: 1043
A map contains key value pair but list does not contains key value pair. So u can create a bean class to set key and values and then create list which contains objects of the bean class.
Upvotes: 0
Reputation: 16
From what i read you are trying to pass map via list (as your service limitation).
In case you don't find any better solution, you can always use two lists. One for keys and one for values.
(but you are risking breaking map consistency with this approach).
Upvotes: 0
Reputation: 9260
To get the values (Strings):
List<List<String>> listVals = new ArrayList<List<String>>(map.values());
To get the keys:
List<Integer> listKeys = new ArrayList<Integer>(map.keySet());
Upvotes: 0
Reputation: 1273
You can create a class with two attributes (Integer, List) and create a List with one object by key.
Also, you can use:
new ArrayList(Map.entrySet());
and convert the resulting set to a list.
Upvotes: 0
Reputation: 14399
A map has two functions called keySet()
and values()
. They return the keys and values of the map respectively. The keyes are returned as a Set
and the values as a Collection
.
You can create a list from either of these.
Here is an example:
Map<Integer, List> map = // map creation;
List<Integer> keyList = new ArrayList<Integer>(map.keySet());
List<List> valueList = new ArrayList<List>(map.values());
Upvotes: 2