| |

VerySource

 Forgot password?
 Register
Search
View: 1187|Reply: 4

Help: Confusion about WeakHashMap

[Copy link]

2

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-3-8 03:30:01
| Show all posts |Read mode
package statictest;
import java.util. *;
class A
{
int a = 3;
String str = "hello";
Ranch
public String toString ()
{
return str + String.valueOf (a);
}
}

public class StaticTest
{
public static void main (String [] args) throws Exception
{
Map map = new WeakHashMap ();
String a = "a";
String av = "av";
A ca = new A ();
map.put ("ca", ca);
map.put (a, av);
System.out.println (map);
System.gc ();
a = null;
ca = null;
Thread.sleep (1000);
System.gc ();
System.gc ();
System.out.println (map);
Ranch
            /*operation result:
{a = av, ca = hello3}
{a = av, ca = hello3}
Same twice * /
Ranch
Map map2 = new WeakHashMap ();
map2.put ("obj", new A ());
A xyz = (A) map2.get ("obj");
System.out.println (map2);
xyz.a = 10000;
System.out.println (map2);
            /*operation result:
{obj = hello3}
{obj = hello10000}
Object member variables can be changed * /
}

}

Doubt 1:
Why did I set the key of WeakHashMap to null, and also called the garbage collector, and let the main thread sleep for a while, but this map still does not let this pair of values?
Doubt 2:
The value stored in the map is always a reference, but why I set the value corresponding to the key ca of the first map to null, but it is printed out or the original value? And in the second map2, after I change the member value of the saved object in it, can it reflect the correct result?
Reply

Use magic Report

3

Threads

17

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-5-31 08:45:01
| Show all posts
Why is this so? Is there a problem with the program run by lz?

import java.util. *;
class Mouse
{
private int i;
Mouse (int i) {
this.i = i;
}
public String toString () {return "Mouse" + i;}
}
public class Test2
{
public static void main (String [] args)
{
Map hashMap = new WeakHashMap ();
Mouse s = new Mouse (1);
s = null;
hashMap.put (s, new Integer (1));
// s = null;
System.out.println (hashMap);
}
}
java Test2
Print: null = 1
Reply

Use magic Report

2

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-6-26 21:30:01
| Show all posts
As said upstairs, if the key is explicitly set to null, it can be added, if the key is an object, not a string, then when the object is null, weakhashmap will automatically reduce the capacity, put this key Value pairs are deleted from the collection.
The nature of the string is puzzling. Although it is the final class, the collection contains a reference, not its own. It is reasonable to say that this reference will point to the new object, but why still point to the old object? Shouldn't the old object be recycled by GC?
code show as below:
package statictest;
import java.util.*;
import java.lang.ref.*;
import java.lang.ref.*;

class A
{
int a=3;
String str="hello";
The
public String toString()
{
return str+a;
}
}

public class StaticTest
{
public static void main(String[] args) throws Exception
{
Map map=new WeakHashMap();
String b="this is b:";
A ab=new A();
map.put(ab, new A());
map.put(b, "b");
System.out.println(map);
String c=(String)map.get(b);
c=null; //Set the value of key b to null, but the printout has not changed.
ab=null; //Because ab is a reference to a class object, it will be moved out of the map
b=null; //But the key b is also an object, why does it not change when set to null? ?
System.gc(); //Must be called, otherwise GC will be too late to recycle, and the original key-value pair will still be printed out
System.out.println(map);
}

}




Print the result:
{hello3=hello3, this is b:=b}
{this is b:=b}
Reply

Use magic Report

1

Threads

20

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-7-12 10:45:01
| Show all posts
There is such a relevant description

This class will work perfectly well with key objects whose equals methods are not based upon object identity, such as String instances. With such recreatable key objects, however, the automatic removal of WeakHashMap entries whose keys have been discarded may prove to be confusing.

String instance is a recreatable key object
Furthermore, it may be because gc() only collects garbage memory in the heap area instead of the stack area.
Caused this to happen.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-2 21:45:01
| Show all posts
The third floor said that gc() only reclaims the heap area, and String b = "this is b: "; b is only stored in the string pool, if the new object comes out, it is right

import java.util.Map;
import java.util.WeakHashMap;


class StaticTest
{
int a=3;
String str = "hello ";

public String toString()
{
return str+a;
}
public static void main(String[] args) throws Exception
{
        Map map=new WeakHashMap();
        String b = new String("this is b: ");
        StaticTest ab=new StaticTest();
        map.put(ab, new StaticTest());
        map.put(b, "b ");
        System.out.println(map);
        String c=(String)map.get(b);
        c=null; //Set the value of key b to null, but it does not change when printed.
        ab=null; //Because ab is a reference to a class object, it will be moved out of the map
        b=null; //b is in the heap memory, nothing is printed here
        System.gc(); //It must be called, otherwise the GC will not have time to recycle, and the original key-value pair will still be printed
        System.out.println(map);
}
}

{this is b: =b, hello 3=hello 3}
{}
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list