在我尝试清除对象值时,LinkedHashMap 中的值被清除了 (Java)

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

The value in LinkedHashMap cleared when I try to clear the object value (Java)

问题

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class TestLinkedHash {

    private static LinkedHashMap<String, List<Object>> linkObj = new LinkedHashMap<String, List<Object>>();
    private static List<Object> test = new ArrayList<Object>();

    public static void main(String[] args) {
        long timestamp = System.currentTimeMillis();
        double value = 900.0;

        String key = "TPS";
        String key1 = "TEST";
        String key2 = "TOST";

        test.add(timestamp);
        test.add(value);

        linkObj.put(key, test);
        linkObj.put(key1, test);
        linkObj.put(key2, test);
        System.out.println(linkObj);

        test.clear();
        System.out.println(linkObj);

        test.add(System.currentTimeMillis());
        test.add(200.0);
        linkObj.put(key, test);
        System.out.println(linkObj);
    }
}
英文:

i got a problem when using LinkedHashMap. I try to store some value in it and then updated it.

I'm using String for the key and List<Object> for the value.
I put 2 value on the List<Object> and put them to LinkedHashMap.
the next step, i want to update one of the value in LinkedHashMap.
I clear the List<Object> and put new value to it and the update the value in LinkedHashMap.

but something strange happen, when i clear the value in List<Object>, the value in LinkedHashMap is cleared too.

anyone have any suggestion regarding this issue?

Thank you.

here is the source code:

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class TestLinkedHash {

	private static LinkedHashMap&lt;String, List&lt;Object&gt;&gt; linkObj = new LinkedHashMap&lt;String, List&lt;Object&gt;&gt;();
	private static List&lt;Object&gt; test =  new ArrayList&lt;Object&gt;();
	
	public static void main(String[] args) {
		long timestamp = System.currentTimeMillis();
		double value = 900.0;
		
		String key = &quot;TPS&quot;;
		String key1 = &quot;TEST&quot;; 
		String key2 = &quot;TOST&quot;;
		
		test.add(timestamp);
		test.add(value);
		
		linkObj.put(key, test);
		linkObj.put(key1, test);
		linkObj.put(key2, test);
		System.out.println(linkObj);	
		
		test.clear();
		System.out.println(linkObj);
		
		test.add(System.currentTimeMillis());
		test.add(200.0);
		linkObj.put(key, test);
		System.out.println(linkObj);
	}
}

答案1

得分: 1

你的集合持有对 test引用。因此,当清空 test 时,你的集合将持有指向空列表的引用。

如果你将对象的浅拷贝插入集合中,对原始对象的更改将不会影响其拷贝。然而,引用指向内存的特定段,当它发生变化时,所有的变化都可以通过引用进行查看和访问。

更新:
对象的更改是共享的,因为你修改的对象与你插入集合的对象是同一个对象。

英文:

Your collection holds references to test. Since that, when test is cleared, your collection will have references to empty list.

If you will insert the shallow copy of an object to the collection, change of original object will not affect its copy. However, reference is pointing to a certain segment of memory, whereas when it mutates, all the mutations are visible and accessible by the reference.

UPD:
The change of an object is shared, since the object you modify is the same object that you have inserted in your collection.

答案2

得分: 0

如果您在将列表克隆后再添加到映射中您会得到预期的行为我认为

package stackoverflow;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

public class TestLinkedHash {

    private static LinkedHashMap<String, List<Object>> linkObj = new LinkedHashMap<String, List<Object>>();
    private static List<Object> test = new ArrayList<Object>();

    public static void main(String[] args) {
        long timestamp = System.currentTimeMillis();
        double value = 900.0;

        String key = "TPS";
        String key1 = "TEST";
        String key2 = "TOST";

        test.add(timestamp);
        test.add(value);

        linkObj.put(key, test);
        linkObj.put(key1, test.stream().collect(Collectors.toList()));
        linkObj.put(key2, test.stream().collect(Collectors.toList()));
        System.out.println(linkObj);

        test.clear();
        System.out.println(linkObj);

        test.add(System.currentTimeMillis());
        test.add(200.0);
        linkObj.put(key, test);
        System.out.println(linkObj);
    }
}

输出:

{TPS=[1598276243552, 900.0], TEST=[1598276243552, 900.0], TOST=[1598276243552, 900.0]}
{TPS=[], TEST=[1598276243552, 900.0], TOST=[1598276243552, 900.0]}
{TPS=[1598276243626, 200.0], TEST=[1598276243552, 900.0], TOST=[1598276243552, 900.0]}
英文:

If you clone the lists you add to the map you get your expected behviour I think:

package stackoverflow;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

public class TestLinkedHash {

	private static LinkedHashMap&lt;String, List&lt;Object&gt;&gt; linkObj = new LinkedHashMap&lt;String, List&lt;Object&gt;&gt;();
	private static List&lt;Object&gt; test = new ArrayList&lt;Object&gt;();

	public static void main(String[] args) {
		long timestamp = System.currentTimeMillis();
		double value = 900.0;

		String key = &quot;TPS&quot;;
		String key1 = &quot;TEST&quot;;
		String key2 = &quot;TOST&quot;;

		test.add(timestamp);
		test.add(value);

		linkObj.put(key, test);
		linkObj.put(key1, test.stream().collect(Collectors.toList()));
		linkObj.put(key2, test.stream().collect(Collectors.toList()));
		System.out.println(linkObj);

		test.clear();
		System.out.println(linkObj);

		test.add(System.currentTimeMillis());
		test.add(200.0);
		linkObj.put(key, test);
		System.out.println(linkObj);
	}
}

Output:

{TPS=[1598276243552, 900.0], TEST=[1598276243552, 900.0], TOST=[1598276243552, 900.0]}
{TPS=[], TEST=[1598276243552, 900.0], TOST=[1598276243552, 900.0]}
{TPS=[1598276243626, 200.0], TEST=[1598276243552, 900.0], TOST=[1598276243552, 900.0]}

huangapple
  • 本文由 发表于 2020年8月24日 20:50:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63561410.html
匿名

发表评论

匿名网友

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

确定