Reputation:
Is it possible to convert ConcurrentHashMap
to HashMap
in java ?
This is my sample program where i was converting from ConcurrentHashMap
to HashMap
but i was getting the following exception:
Exception in thread "main" java.lang.ClassCastException: java.util.concurrent.ConcurrentHashMap cannot be cast to java.util.HashMap at com.Hi.main(Hi.java:18)
My code:
package com;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class Hi {
public static void main(String args[]) {
Map<String, String> conpage = new ConcurrentHashMap<String, String>();
conpage.put("1", "A");
conpage.put("2", "B");
conpage.put("3", "C");
HashMap hm = (HashMap) conpage;
System.out.println(hm.get("1"));
}
}
Upvotes: 3
Views: 14405
Reputation: 89169
Map<String, String> hashMap = new HashMap<String, String>(conpage);
A ConcurrentMap
(like ConcurrentHashMap
) has no relationship with any AbstractMap
, such has HashMap
, so the solution is to create a new HashMap
adding all values from ConcurrentHashMap
during object creation.
Hope this helps.
Upvotes: 14
Reputation: 2660
To be more explicit in the explanation of the previous comments: consider you have three classes :
class Position {
}
class One extends Position {
String gold = "the best";
}
class Two extends Position {
String silver = "just wait next year!";
}
You cannot do the following cast (note that a cast is not a conversion: it's only a redeclaration of the type)
void showPosition() {
Position one = new One(); // this is regular since One extends Position
Two two = (Two)one; // this is impossible because one is not a two
}
If ever this cast was possible, how would you like the compiler to handle the following promblem? : there is no field called silver in one: so calling
((Two)one).silver
is impossible: returning null would be unsafe, since you wouldn't understand what's happening : since you know that the field 'silver' is initialized to the value "just wait next year!"
Java being a safe language, it doesn't allow this type of errors (actually it's a help from Java that it throws the exception since you think the type is something else that is it is).
The behvior you expect is rather a behavior proper to scripts.
Upvotes: 2
Reputation: 270
Actually There is no inheritance relation between HashMap and ConcurrentHashMap that's why while casting the concurrenthashmap object to hashmap its giving the ClassCastException.
Upvotes: 0
Reputation: 49372
A ConcurrentHashMap is not a HashMap , so you cannot perform this cast. Treat them as Map regardless of implementation.
Nevertheless , you can use Map#putAll() .
Suggested Reading:
Upvotes: 5
Reputation: 7836
Use putAll() method istead of type casting like this:
HashMap hm=new HashMap<String, String>();
hm.putAll(conpage);
Upvotes: 3
Reputation: 29166
You can do so in the following way -
HashMap<String, String> map = new HashMap<String, String>(conpage);
Typically each concrete type in the Java Collection API (like HashMap, ArrayList etc.) has a constructor which takes a reference of its parent (like Map, List) and constructs a new object from it.
About the exception you are getting, it's because ConcurrentHashMap
is not a subtype/supertype of HashMap
, thus you are getting a ClassCastException
. However, this would have worked fine -
Map<String, String> hm = (Map<String, String> ) conpage;
Upvotes: 1
Reputation:
A ConcurrentHashMap is still a Map. So you can create a new TreeMap like this:
ConcurrentHashMap myMap;
...
TreeMap myTreeMap = new TreeMap( myMap );
Upvotes: 1
Reputation: 6242
ConcurrentHashMap and HashMap are siblings and not Parent-Child related. Hence the cast fails.
Upvotes: 2