Diablo
Diablo

Reputation: 35

How to typecast a HashMap to a WeakHashMap?

I have a method like this :-

public Map<String,String> loadProperties() Exception{
    Map <String,String> params = new HashMap<String,String>();
.
.
.
  return params;
}

The above method returns me a Map of (key , value) from the DB.

I need to TypeCast loadProperties() to a WeakHashMap. Below I have another class called Service. I tried the typecasting in its constructor but it is giving me ClassCastException.

"java.lang.ClassCastException: java.util.HashMap cannot be cast to java.util.WeakHashMap"

Below is the Service class:-

private Service() throws Exception {
   configPropertiesCache = dao.loadProperties();
 configPropertiesCache = (WeakHashMap<String, String>) dao.loadProperties(); 

I am curious to know why its not working ?

Upvotes: 0

Views: 576

Answers (6)

Matt3o12
Matt3o12

Reputation: 4342

You cannot cast a Map to a WeakHashMap but you can create a new WeakWashMap which contains all values. You can do it with that code:

Map<String, String> otherMap = new HashMap()<String, String>;
map.put("Hello", "World");
WeakHashMap<String, String> weakMap = new WeakHashMap(otherMap);
System.out.println(weakMap.get("Hello"));

Upvotes: 0

The Cat
The Cat

Reputation: 2475

The type cast won't work but you could create an asWeakHashMap method that takes a map and adds all the entries into a new WeakHashMap and returns that map.

Upvotes: 0

BlackJoker
BlackJoker

Reputation: 3191

do it like this:

public WeakHashMap<String, String> getWeakHashMap(Map<String, String> map) {
    if (map instanceof WeakHashMap) {
        return (WeakHashMap<String,String>) map;
    }
    return new WeakHashMap<String,String>(map);
}

Upvotes: 2

Zim-Zam O&#39;Pootertoot
Zim-Zam O&#39;Pootertoot

Reputation: 18148

HashMap and WeakHashMap are different Map implementations, and as such you can't cast one to the other. In general, if you have ClassA with subclasses ClassB and ClassC, then you can cast ClassB or ClassC to ClassA, but you cannot cast ClassB to ClassC or vice versa.

If you want to turn HashMap into a WeakHashMap then you'll need to create a new WeakHashMap, then iterate through your HashMap and add all of its key-value pairs to the WeakHashMap.

Upvotes: 0

Jesper
Jesper

Reputation: 206846

It doesn't work because a HashMap simply is not a WeakHashMap. Type casting does not do any kind of magical conversion from one type to another type.

The only thing that a type cast means is that you tell the compiler that you want it to treat one type of object as if it is another type of object - but a check will still be done at runtime to check if the object you're casting really is what you cast it to, and if the check fails you get a ClassCastException.

Either create the map as a WeakHashMap in your loadProperties() method, or if you cannot modify that method, copy it into a WeakHashMap:

configPropertiesCache = new WeakHashMap<>(dao.loadProperties());

Upvotes: 7

BobTheBuilder
BobTheBuilder

Reputation: 19284

HashMap isn't a WeakHashMap so you cant cast one to the other. However, you can return WeakHashMap in your loadProperties method, or convert the HashMap to a WeakHashMap manually.

Upvotes: 0

Related Questions