Add Elements to an ArrayList within a HashMap [duplicate] - java

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");

Related

How do I remove something from an array list when looping through it in java? [duplicate]

This question already has answers here:
Remove elements from collection while iterating
(9 answers)
Closed 2 years ago.
I have an arraylist like this:
ArrayList<Integer> numbers = new ArrayList<Integer>();
And let's say the values inside are like:
[0,1,2,3,4,5]
So I'm looping through and I want to remove values from the arraylist while I'm looping through:
for(int i=0; i<numbers.size();i++){
if(numbers.get(i)>2){
numbers.remove(i);
}
}
How would I do this?
You can call numbers.remove(i) and then decrement i to follow the change in the position within the list: numbers.remove(i); i--;.
If you use iterators, foreach or the : operator you can't remove values, because they use unmodifiable lists.
If you have a simple way of determining whether a number should be removed, you can use numbers.removeIf((number)->number>2) (like the problem in your example).

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 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

Removes and returns the first occurrence of the specified string in array using Java [duplicate]

This question already has answers here:
Remove a specific string from an array of string
(5 answers)
Closed 8 years ago.
I am trying to delete from an array the first occurence, not all elements like the searched element The array is something like:
String[] names = {"Becky", "Rosa", "Tina", "Jill", "Rosa", "Bill"};
And I want to be able to say remove(Rosa) and only find and remove the first element in the array named Rosa.
So you might want to consider using an Arraylist for dynamic removal and resizing
import java.util.ArrayList;
ArrayList<string> persons = new ArrayList<string>();
persons.add("Becky");
//... adding in people to the list
//then if you want to remove someone name "becky"
persons.removeall.(collection.singleton("Becky"))

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

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());

Categories