2

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"));

    }

}
lucidbrot
  • 5,378
  • 3
  • 39
  • 68

8 Answers8

13
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.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
5

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:

  1. Java - HashMap vs Map objects
  2. What does it mean to “program to an interface”?
Community
  • 1
  • 1
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
3

Use putAll() method istead of type casting like this:

HashMap hm=new HashMap<String, String>();
hm.putAll(conpage);    
Ankur Lathi
  • 7,636
  • 5
  • 37
  • 49
2

ConcurrentHashMap and HashMap are siblings and not Parent-Child related. Hence the cast fails.

MohamedSanaulla
  • 6,112
  • 5
  • 28
  • 45
2

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.

Zied Hamdi
  • 2,400
  • 1
  • 25
  • 40
1

A ConcurrentHashMap is still a Map. So you can create a new TreeMap like this:

ConcurrentHashMap myMap;
...
TreeMap myTreeMap = new TreeMap( myMap );
1

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;
MD Sayem Ahmed
  • 28,628
  • 27
  • 111
  • 178
0

Actually There is no inheritance relation between HashMap and ConcurrentHashMap that's why while casting the concurrenthashmap object to hashmap its giving the ClassCastException.

Jagadeesh G
  • 272
  • 1
  • 3
  • 10