How do I convert a Map to List in Java? - Stack Overflow
java - convert map to list<string> - as "key-value" to each list entry - Stack Overflow
How to fetch a List which is a "value" in Map?
java - How to convert hash map keys into list? - Stack Overflow
List<Value> list = new ArrayList<Value>(map.values());
assuming:
Map<Key,Value> map;
The issue here is that Map has two values (a key and value), while a List only has one value (an element).
Therefore, the best that can be done is to either get a List of the keys or the values. (Unless we make a wrapper to hold on to the key/value pair).
Say we have a Map:
Map<String, String> m = new HashMap<String, String>();
m.put("Hello", "World");
m.put("Apple", "3.14");
m.put("Another", "Element");
The keys as a List can be obtained by creating a new ArrayList from a Set returned by the Map.keySet method:
List<String> list = new ArrayList<String>(m.keySet());
While the values as a List can be obtained creating a new ArrayList from a Collection returned by the Map.values method:
List<String> list = new ArrayList<String>(m.values());
The result of getting the List of keys:
Apple Another Hello
The result of getting the List of values:
3.14 Element World
List<String> list = map.entrySet()
.stream()
.map(entry -> entry.getKey() + "-" + entry.getValue())
.sorted()
.collect(Collectors.toList());
Just a slightly different variant to the other answers. if the insertion/iteration order is important then I'd rather use a LinkedHashMap in the first place as opposed to using a HashMap then sorting it which is actually not always guaranteed to work.
Example:
Map<Integer, String> map = new LinkedHashMap<>();
...
...
...
then stream over the entrySet and collect to a list implementation:
List<String> list = map.entrySet()
.stream()
.map(e -> e.getKey() + "-" + e.getValue())
.collect(Collectors.toList());
Hi folks, I hope you're doing awesome in life. I got stuck in a situation which is -
I've got a map which is of type -
Map<Integer,List<Integer>> m = new HashMap<>();
Now the thing is I want to create a (key,value) in map like -
(Key->Value)
(1->[1,2,3]),
(2 ->[2,3,4]),
(-1->[5,6,7]),
(0->[4])
And I want to add these key value pair in the Map by using the given below statement -
If(m.conatinskey(x)){ m.Put(x,m.get(x).add(any value)); ***** line ****** } else{ List<Integer> temp = new ArrayList<>(); temp.add(any integer value); m.add(temp); }
Now when I'm trying to execute the line statement I'm getting an error stating that my error has been simplified and I need verbose for better explanation but when I'm writing the statement as -
m.get(x).add(any value);
I am not getting any error . Can someone please let me know what's the issue as in my line statement I'm first trying to get the key and at thay key index I'm trying to add the element in the list ?
Your uniqueRecs has String type of the key. You have to do:
List<String> keys = new ArrayList<>(uniqueRecs.keySet());
or
List<ARecord> values = new ArrayList<>(uniqueRecs.values());
What worked for me that i wanted to modify an existing list was:
list.addAll(map.keySet());