修改映射条目的值,而无需替换整个映射条目。

huangapple go评论66阅读模式
英文:

Edit value of a map entry without replace whole map entry

问题

我目前正在寻找一种类似于Map的数据结构,但不同之处在于map条目的值是可编辑的。

我的map看起来像这样:Map<String, List<String>>。但是在代码中,有时我想向map条目的列表中添加一个项目。但这行不通,因为我认为我不能在不用replaceput替换整个条目的情况下编辑map条目的值。

是否有一种类似的数据结构,外观与Map类似,但我可以编辑条目的值?

代码:

for(String p : paths){
    String[] arr = p.split("\\\\", 2);
    //注意:如果p只有一个元素并且没有File.seperator该怎么办?
    List<String> list = geteilt.get(arr[0]);

    if(list != null){
        if(arr.length > 1){
            //这是我的问题。我想将一个String添加到列表中
            list.add(arr[1]);
        }
    } else {
        if(arr.length > 1){
            geteilt.put(arr[0], List.of(arr[1]));
        } else {
            geteilt.put(arr[0], List.of());
        }
    }
}
英文:

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&lt;String, List&lt;String&gt;&gt;. 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(&quot;\\\\&quot;, 2);
            //achtung was tun wen p nur ein element nur mehr ist und keine File.seperator besitzt???
            List&lt;String&gt; list = geteilt.get(arr[0]);

            if(list != null){
                if(arr.length &gt; 1){
                    //Here is my problem. I want to add a String to the list
                    list.add(arr[1]);
                    
                }
            } else {
                if(arr.length &gt; 1){
                    geteilt.put(arr[0], List.of(arr[1]));
                } else {
                    geteilt.put(arr[0], List.of());
                }
            }

        }

答案1

得分: 1

我认为我不能在不用replaceput替换整个条目的情况下编辑映射条目的值。

这是不正确的。以下是一个示例来证明这一点:

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("原始: " + map);

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

输出:

原始: {xyz=[x, y, z], abc=[a, b]}
更新后: {xyz=[x, y, z], abc=[a, c, p]}
英文:

> 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&lt;String, List&lt;String&gt;&gt; map = Map.of(&quot;abc&quot;, new ArrayList&lt;&gt;(Arrays.asList(&quot;a&quot;, &quot;b&quot;)), &quot;xyz&quot;,
				new ArrayList&lt;&gt;(Arrays.asList(&quot;x&quot;, &quot;y&quot;, &quot;z&quot;)));
		System.out.println(&quot;Original: &quot; + map);

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

Output:

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

答案2

得分: 0

你不需要为你的情况专门使用新的数据结构,即 Map<String, List>。

要编辑 Map 中的条目:

  1. 获取你想要修改的键的值。

  2. 返回的值将是 List,你可以使用通常的列表方法修改这个值,改变将会在 Map 的值中反映出来,因为 Map 的值只是对该列表的引用。

英文:

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

答案3

得分: 0

如果您使用的是Java 8或更高版本,您可以使用computecomputeIfPresent
例如:

        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); // 输出 {1=[a, b, c], 2=[a, b, c]}
        test.computeIfPresent("1", (k, v) -> {
            v.set(2, "changed");
            return v;
        });
        System.out.println(test); // 输出 {1=[a, b, changed], 2=[a, b, c]}

如果您使用的是任何旧版本的Java,我的建议是使用一个工具方法。

英文:

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

        Map&lt;String, List&lt;String&gt;&gt; test = new HashMap&lt;&gt;();
        test.put(&quot;1&quot;, Arrays.asList(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;));
        test.put(&quot;2&quot;, Arrays.asList(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;));
            
        System.out.println(test); // prints {1=[a, b, c], 2=[a, b, c]}
        test.computeIfPresent(&quot;1&quot;, (k, v) -&gt; {
            v.set(2, &quot;changed&quot;);
            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.

huangapple
  • 本文由 发表于 2020年5月4日 02:18:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/61579490.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定