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"));
}
}
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.
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:
Use putAll() method istead of type casting like this:
HashMap hm=new HashMap<String, String>();
hm.putAll(conpage);
ConcurrentHashMap and HashMap are siblings and not Parent-Child related. Hence the cast fails.
A ConcurrentHashMap is still a Map. So you can create a new TreeMap like this:
ConcurrentHashMap myMap;
...
TreeMap myTreeMap = new TreeMap( myMap );
You can do so in the following way -
HashMap<String, String> map = new HashMap<String, String>(conpage);
JavaDoc .
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;
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.
实际上,HashMap和ConcurrentHashMap之间没有继承关系,这就是为什么在将钱币并发对象映射为hashClass时将其传递给ClassCastException的原因。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.