Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Is there any way to cast java.util.map(HashMap) to HazelCast IMap ?

Map<String, User> map = ....;
IMap<String, User> imap;

Thanks

Hazelcast IMap implements Map and ConcurrentMap interfaces. In the following case, objects map2 and map will point to the same distributed object (even more, map2 and map would point to the same proxy object).

Map<Object, Object> map2 = hazelcastInstance.getMap("test"); IMap<Object, Object> map = hazelcastInstance.getMap("test");

With Map interface you're limited with "standard" methods. IMap provides extensions like submitting EntryProcessors, adding Event listeners.

I hope it does make sense. Let me know if you have any questions.

Thank you

Hi Vik, but my problem is in unit test. the hazelcast instance is mocked. I want to add a value into mocked IMap. I need that existing value in IMap for test purposes. In mocked IMap I couldn't add any value with 'set' method. Thanks – Mehrdad Abdolghafari Oct 26, 2016 at 13:18 java.lang.ClassCastException: java.util.HashMap cannot be cast to com.hazelcast.core.IMap – Mehrdad Abdolghafari Oct 26, 2016 at 13:22 IMap<String, User> iMapMocked = Mockito.mock(IMap.class); How can I add a value into this mocked IMap? – Mehrdad Abdolghafari Oct 26, 2016 at 13:22

Quote from the official Hazelcast documentation:

Let's say you want to test if two members have the same size of a map.

@Test
public void testTwoMemberMapSizes() {
  // start the first member
  HazelcastInstance h1 = Hazelcast.newHazelcastInstance();
  // get the map and put 1000 entries
  Map map1 = h1.getMap( "testmap" );
  for ( int i = 0; i < 1000; i++ ) {
    map1.put( i, "value" + i );
  // check the map size
  assertEquals( 1000, map1.size() );
  // start the second member
  HazelcastInstance h2 = Hazelcast.newHazelcastInstance();
  // get the same map from the second member
  Map map2 = h2.getMap( "testmap" );
  // check the size of map2
  assertEquals( 1000, map2.size() );
  // check the size of map1 again
  assertEquals( 1000, map1.size() );

p.s. please don't write like that, use given when then / arrange act assert

<groupId>com.hazelcast</groupId> <artifactId>hazelcast</artifactId> <classifier>tests</classifier> <version>${hazelcast.version}</version> <scope>test</scope> </dependency>

In your unit test you will have these fields:

@Mock
private HazelcastInstance hazelcastInstance;
private TestHazelcastInstanceFactory hazelcastFactory = new TestHazelcastInstanceFactory();

And finally, in your test (assuming you are using Mockito):

// mock the hazelcast map
IMap<Object, Object> mockedMap = hazelcastFactory.newHazelcastInstance().getMap("doesntmatter");
mockedMap.put("some-key", someObject);
when(hazelcastInstance.getMap("testMap")).thenReturn(mockedMap);
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.