Gregor
Gregor

Reputation: 449

Edit value of a map entry without replace whole map entry

I am currently searching for some DataStrucutre that is similar to a Map, but with the diffrenece, that the value of a mapentry is editable.

My map looks like this: Map<String, List<String>>. But sometimes in the code I want to add an item to the list of a map entry. But this doesn't work, because I think I can't edit a value of a map entry without replacing the whole entry with replace or put.

Is there some similar DataStructure that looks similar as Map but I can edit the value of an entry?

Code:

for(String p : paths){
            String[] arr = p.split("\\\\", 2);
            //achtung was tun wen p nur ein element nur mehr ist und keine File.seperator besitzt???
            List<String> list = geteilt.get(arr[0]);

            if(list != null){
                if(arr.length > 1){
                    //Here is my problem. I want to add a String to the list
                    list.add(arr[1]);

                }
            } else {
                if(arr.length > 1){
                    geteilt.put(arr[0], List.of(arr[1]));
                } else {
                    geteilt.put(arr[0], List.of());
                }
            }

        }

Upvotes: 0

Views: 889

Answers (3)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79620

I think I can't edit a value of a map entry without replacing the whole entry with replace or put.

This is not correct. Given below is an example to prove this:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<String, List<String>> map = Map.of("abc", new ArrayList<>(Arrays.asList("a", "b")), "xyz",
                new ArrayList<>(Arrays.asList("x", "y", "z")));
        System.out.println("Original: " + map);

        map.get("abc").set(1, "c");
        map.get("abc").add("p");
        System.out.println("Updated: " + map);
    }
}

Output:

Original: {xyz=[x, y, z], abc=[a, b]}
Updated: {xyz=[x, y, z], abc=[a, c, p]}

Upvotes: 1

fjsv
fjsv

Reputation: 714

If you are using java 8 or above, you can use compute or computeIfPresent. For instance:

        Map<String, List<String>> test = new HashMap<>();
        test.put("1", Arrays.asList("a", "b", "c"));
        test.put("2", Arrays.asList("a", "b", "c"));

        System.out.println(test); // prints {1=[a, b, c], 2=[a, b, c]}
        test.computeIfPresent("1", (k, v) -> {
            v.set(2, "changed");
            return v;
        });
        System.out.println(test); // prints {1=[a, b, changed], 2=[a, b, c]}

If you are using any older version of java my suggestion is an util method.

Upvotes: 0

am10
am10

Reputation: 499

You don't need any new data structure specifically for your case i.e. Map < String, List < String> >

To edit entry of Map:

  1. Get the value for a key that you want to modify.

  2. The returned value will be List you can modify this value by using the usual list method, and the change will be reflected as Map values are just referenced to the List.

Upvotes: 0

Related Questions