Retrieve values from hashmap (.values()) [duplicate] - java

This question already has answers here:
How do I convert a Map to List in Java?
(13 answers)
Closed 9 years ago.
I would be pleasure if you can suggest.
Could you suggest how properlly I can retriev all values from hashmap.
In such simplee example:
Map<String, Values> someMap = ....;
List<Values> valuesFromMap = (List<Values>) someMap.values();
After that I've got that, Java couldn't cast to java.util.List
Thank you in advanced.

The values method returns a Collection, not a List. Use
Collection<Values> valuesFromMap = someMap.values();

someMap.values() returns Collection. If you need to convert into List than do that as follows:
List<Values> valuesFromMap = new ArrayList<>(someMap.values());

Related

How can list of values be changed insisde a map? (Java) [duplicate]

This question already has answers here:
Java ArrayList replace at specific index
(6 answers)
Closed 2 years ago.
I have this kind of map:
Map<String, List<String>> map = new HashMap<>();
And I'm trying to edit one of my list's values. This is how I tried to achieve that:
map.get(key).toArray()[index] = newString;
I convert my list into an array and assign new String. But this line of code does nothing. Is there any solution to it? I think I can just create a new list which will have a different value, but I hope there is a simpler way to achieve that.
Thank you in advance
The List.toArray method creates an array that is a copy of the list. You can use the List.set method to set an item at an index
List<String> list = map.get(key);
list.set(index, newValue);
or, without a temporary variable:
map.get(key).set(index, newValue);

How do I go from Stream<ArrayList<Object>>> to ArrayList<Object> in Java? [duplicate]

This question already has answers here:
Java 8 Streams FlatMap method example
(7 answers)
Closed 3 years ago.
I have a Stream<ArrayList<Object>> and I want to "extract" the ArrayList from it and assign it to a variable. How do I do that?
My resulting variable needs to be of type ArrayList<Object> so I can iterate over it and do stuff.
If you want to get one ArrayList then use
ArrayList<Object> result = strm.flatMap(ArrayList::stream)
.collect(Collectors.toCollection(ArrayList::new));
Stream.flatMap method lets you replace each value of a stream with
another stream and then concatenates all the generated streams into a single stream.
List<Object> objectList = new ArrayList<>();
List<Object> collect = Stream.of(objectList)
.flatMap(m -> m.stream())
.collect(Collectors.toList());

Add Elements to an ArrayList within a HashMap [duplicate]

This question already has answers here:
Adding a value to a list to an already existing key in Map
(3 answers)
Closed 4 years ago.
How to add an Element to an ArrayList within a HashMap?
This is a question, that I have asked myself many times and forgot it after solving it. I guess many have the same one so here is the simple answer to it.
// Example
HashMap<String, ArrayList<String>> someElements = new HashMap<String, ArrayList<String>>();
// fill the HashMap with the key that you need
// initiate it with an empty ArrayList
someElements.put("keyString" , new ArrayList<String>());
// Later when wanting to add an element to the ArrayList of a key use
someElements.get("keyString").add("TheStringValue");

JAVA8: Map list of objects to String[] [duplicate]

This question already has answers here:
How to convert a Java 8 Stream to an Array?
(9 answers)
Closed 5 years ago.
I have a List of 'Client' objects each one with a field "email".
I need something like:
List<String> listEmails = clients.stream().map(client->client.getEmail())
.collect(Collectors.toList());
...but returning directly a String[].
Is there a proper way to map a List<Client> to a String[] listEmails using Java 8 streams?
Sure :
String[] result = clients
.stream()
.map(client->client.getEmail())
.toArray(String[]::new)

How to put value in HashMap? [duplicate]

This question already has answers here:
How to initialize an array in Java?
(11 answers)
Closed 6 years ago.
I have a Map
Map<String,String[]> data = HashMap<String,String[]>();
and want to put value in it. like
"key":["value1","value2"]
I try it but get error:
data.put("key",["value1","value2"]);//syntax error
You could try using the Array initializer for fixed size string arrays like following
Map<String, String[]> data = new HashMap<>();
data.put("key", new String[]{"val1", "val2"});
This should definitely work

Categories