In this article, we are going to see an example of Guava ClassToInstanceMap
.
We will use ClassToInstanceMap
when we want to map keys which are types bull all are not of the same type. The values are instances of that type. ClassToInstanceMap
uses generics to make sure the type used as key belong to the same super type.
It implements Map
interface and the data structure internally relies on HashMap
. Since it is a map, each of its entry maps a Java raw type to an instance of that type.
Instead of simply accepting the types as it is it makes sure that the objects added are type safe. It applies checks the key value to make sure it is not null and can cast the object to the specified type parameter. It also make sure that the primitive classes are converted to the respective wrapper class, for example, long.class
and Long.class
are both of type Class<Long>
.
ClassToInstanceMap
provides the type safe methods T getInstance(Class)
and T putInstance(Class, T)
thus takes care of type casting issues.
In our example, we will create a mutable Map:
ClassToInstanceMap<A> classToInstanceMap = MutableClassToInstanceMap.create();
If you are interested in immutable map use ImmutableClassToInstanceMap
.
Let’s start with our example. We will define couple of types A_1
and A_2
, both extend super class A
.
A:
package com.javarticles.guava; public class A { public String toString() { return "A"; } }
A_1:
package com.javarticles.guava; public class A_1 extends A { public String toString() { return "A_1"; } }
A_2:
package com.javarticles.guava; public class A_2 extends A { public String toString() { return "A_2"; } }
We will create a ClassToInstanceMap
for type A
so that we can map instances of A_1
and A_2
against the proper type.
Note if we try to map instance of A_2
against key A_1
, we will get a ClassCastException
at runtime.
We will also create a ClassToInstanceMap
for Number
and store both int
and double
values.
GuavaClassToInstanceTypeExample:
package com.javarticles.guava; import com.google.common.collect.ClassToInstanceMap; import com.google.common.collect.MutableClassToInstanceMap; public class GuavaClassToInstanceTypeExample { public static void main(String[] args) { ClassToInstanceMap<A> classToInstanceMap = MutableClassToInstanceMap.create(); classToInstanceMap.put(A.class, new A()); classToInstanceMap.put(A_1.class, new A_1()); classToInstanceMap.put(A_2.class, new A_2()); System.out.println(classToInstanceMap); System.out.println("A_1 Object: " + classToInstanceMap.get(A_1.class)); try { classToInstanceMap.put(A_1.class, new A_2()); } catch (ClassCastException e) { System.out.println(e); } System.out.println("set A_1 Object to null"); classToInstanceMap.put(A_1.class, null); System.out.println("get A_1 Object: " + classToInstanceMap.get(A_1.class)); ClassToInstanceMap<Number> intMap = MutableClassToInstanceMap.create(); intMap.put(int.class, 2); intMap.put(double.class, 3.2); System.out.println("get int value: " + intMap.get(int.class)); System.out.println("get double value: " + intMap.get(double.class)); } }
Output:
{class com.javarticles.guava.A=A, class com.javarticles.guava.A_2=A_2, class com.javarticles.guava.A_1=A_1} A_1 Object: A_1 java.lang.ClassCastException: Cannot cast com.javarticles.guava.A_2 to com.javarticles.guava.A_1 set A_1 Object to null get A_1 Object: null
Download the source code
This was an example about Guava ClassToInstanceMap.