Recent Tutorials and Articles
    Designing / Developing a Generic type Class
    Published on: 9th August 2013

    This tutorial demonstrates the designing and developing a class which takes/uses Generic type parameters just like Collection etc.

    Let's start with implementing a very basic in-memory cache providing the methods to put and get the Objects out of it.

    InMemoryObjectCache.java
    
    package com.sts.allprogrammingtutorials.generics;
    
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * @author Sain Technology Solutions
     *
     */
    public class InMemoryObjectCache {
    	
    	private final Map cachedObjects = new HashMap();
    	
    	public boolean put(final Object key, final Object value) {
    		if(key == null || value == null) {
    			return false;
    		}
    		
    		cachedObjects.put(key, value);
    		return true;
    	}
    	
    	public Object get(Object key) {
    		return cachedObjects.get(key);
    	}
    }
    
    

    Following is how it will look like using Generics (notice K and V in angle brackets and the places where these have been used) -

    InMemoryObjectCacheUsingGenerics.java
    
    package com.sts.allprogrammingtutorials.generics;
    
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * @author Sain Technology Solutions
     *
     */
    public class InMemoryObjectCacheUsingGenerics<K, V> {
    	
    	private final Map<K,V> cachedObjects = new HashMap<K, V>();
    	
    	public boolean put(final K key, final V value) {
    		if(key == null || value == null) {
    			return false;
    		}
    		
    		cachedObjects.put(key, value);
    		return true;
    	}
    	
    	public V get(K key) {
    		return cachedObjects.get(key);
    	}
    }
    
    

    Now let's take it even further by making sure that only objects implementing Serializable interface are cached by our InMemoryObjectCache cache. Here is how you would implement same using lower bounded wildcard (notice extends keyword usage) -

    InMemoryObjectCacheUsingGenerics.java
    
    package com.sts.allprogrammingtutorials.generics;
    
    import java.io.Serializable;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * @author Sain Technology Solutions
     *
     */
    public class InMemoryObjectCacheUsingGenerics<K extends Serializable, V extends Serializable> {
    	
    	private final Map<K,V> cachedObjects = new HashMap<K, V>();
    	
    	public boolean put(final K key, final V value) {
    		if(key == null || value == null) {
    			return false;
    		}
    		
    		cachedObjects.put(key, value);
    		return true;
    	}
    	
    	public V get(K key) {
    		return cachedObjects.get(key);
    	}
    }
    
    

    Thank you for reading through the tutorial. In case of any feedback/questions/concerns, you can communicate same to us through your comments and we shall get back to you as soon as possible.

    Published on: 9th August 2013

    Comment Form is loading comments...