Guava ClassToInstanceMap Example
Guava ClassToInstanceMap
A special type of collection that uses the class as the key and the instance of that class as the value. In this example, my ClassToInstanceMap allows me to store all sorts of different types of wrapper classes in myNumberMap collection. These all happen to be numeric types (wrapper classes) for Java primitive types and store the values of the types in the collection. In addition, I have created another collection called myDataMap which is storing classes like Customer and Associate although doing so was not a necessity.
Methods available to ClassToInstanceMap
@GwtCompatible
public interface ClassToInstanceMap<B>
extends Map<Class<? extends B>,B>
# | Method and Description |
---|---|
1 | static <B> MutableClassToInstanceMap<B> create() Returns a new MutableClassToInstanceMap instance backed by a HashMap using the default initial capacity and load factor. |
2 | static <B> MutableClassToInstanceMap<B> create(Map<Class<? extends B>,B> backingMap) Returns a new MutableClassToInstanceMap instance backed by a given empty backingMap. |
3 | protected Map<K,V> delegate() Returns the backing delegate instance that methods are forwarded to. |
4 | Set<Map.Entry<K,V>> entrySet() Returns a Set view of the mappings contained in this map. |
5 | <T extends B> T getInstance(Class<T> type) Returns the value the specified class is mapped to, or null if no entry for this class is present. |
6 | V put(K key, V value) Associates the specified value with the specified key in this map (optional operation). |
7 | void putAll(Map<? extends K,? extends V> map) Copies all of the mappings from the specified map to this map (optional operation). |
8 | <T extends B> T putInstance(Class<T> type, T value) Maps the specified class to the specified value. |
ClassToInstanceMap Implementations
There are two implementation classes available for ClassToInstanceMap:
- ImmutableClassToInstanceMap
- MutableClassToInstanceMap
Java Example using Guava ClassToInstanceMap
package com.avaldes; import com.avaldes.model.Associate; import com.avaldes.model.Customer; import com.google.common.collect.ClassToInstanceMap; import com.google.common.collect.MutableClassToInstanceMap; public class GuavaClassToInstanceMapExample { private static ClassToInstanceMap<Number> myNumberMap = MutableClassToInstanceMap.create(); private static ClassToInstanceMap<Object> myDataMap = MutableClassToInstanceMap.create(); public static void main(String[] args) { myNumberMap.putInstance(Integer.class, 1276); myNumberMap.putInstance(Double.class, 11.751897298); myNumberMap.putInstance(Float.class, 5.125f); myNumberMap.putInstance(Long.class, 1982092L); System.out.println("\n---[MutableClassToInstanceMap with Number]-----"); System.out.println("Value for Long.class is: " + myNumberMap.get(Long.class)); System.out.println("Value for Float.class is: " + myNumberMap.get(Float.class)); System.out.println("Value for Integer.class is: " + myNumberMap.get(Integer.class)); System.out.println("Value for Double.class is: " + myNumberMap.get(Double.class)); Associate associate = new Associate("111", "Amaury", "Valdes", "IT", "200", "IBM Global Solutions", "E"); Customer customer = new Customer(); customer.setName("Will Smith"); customer.setAddress("100 Main Street"); customer.setState("California"); customer.setZip("10011"); myDataMap.put(Associate.class, associate); myDataMap.put(Customer.class, customer); System.out.println("\n---[MutableClassToInstanceMap with Objects]-----"); System.out.println("Value for Customer.class is: " + myDataMap.get(Customer.class)); System.out.println("Value for Associate.class is: " + myDataMap.get(Associate.class)); } }
That’s It!
I hope you enjoyed this tutorial. Please continue to share the love and like us so that we can continue bringing you quality tutorials. Happy Coding!!!
Related Posts
- Guava Multimap - ArrayListMultimap Implementation Example
In this tutorial we will cover implementing an ArrayListMultimap (which allows duplicate key/value pairs). - Guava Multimap - HashMultimap Implementation Example
In this tutorial we will cover implementing an HashMultimap which does not allow duplicate key/value pairs, using HashMultimap. - Guava Multimap - TreeMultimap Implementation Example
In this tutorial we will cover a implementing an TreeMultimap which performs Sorting according to natural order and does not allow duplicate key/value pairs. - Guava Multiset Example
In this tutorial we will cover a basic introduction the to Guava's Multiset collection using easy to follow example to show you how best to implement this collection in your own code. - Guava BiMap Example (Bi-Directional Map)
In this tutorial we will cover a basic introduction the to Guava's Bidirectional Map (BiMap) using easy to follow example to show you how best to implement this collection in your own code. - Guava Table Example
In this tutorial we will cover a basic introduction the to Guava's Table using easy to follow example to show you how best to implement this collection in your own code. - Guava ClassToInstanceMap Example
In this tutorial we show you how to use Guava's ClassToInstanceMap collection to store class type as the key and the instance of the collection as a value.
Leave a Reply