Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By Tolls

Thread: Map<T,T> possible in non-generic class?

  1. #1
    Tim99 is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default Map<T,T> possible in non-generic class?

    Hi,

    in my (not generic) class, I want to map generic objects onto other objects of the same type: Map<T,T>.

    Examples:
    String --> String should be possible
    Double --> Double as well
    String --> Double should not be possible.

    I tried this:
    Java Code:
    public class MyClass {
    
      private Map<T, T> myMap = new HashMap<T, T>>();
    
      public T get(T key) {
        return myMap.get(key);
      }
    
    }
    Any help would be greatly appreciated!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default Re: Map<T,T> possible in non-generic class?

    What you're trying to do IS a class containing generics. Why don't you just declare T in the class header?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Tim99 is offline Member
    Join Date
    Dec 2011
    Posts
    13
    Rep Power
    0

    Default Re: Map<T,T> possible in non-generic class?

    Because one instance of this class should be able to store *both* Double-->Double or String-->String. All combinations of key/value should be possible as long as they are both of the same type.

    With e.g. MyClass<Double>, I wouldn't be able to store String-->String.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default Re: Map<T,T> possible in non-generic class?

    So you want a Map where the keys and values have to be of the same Type? That's a contract better represented in an interface than in the generic parameters of a class field.
    Java Code:
    interface MapT<T> extends Map<T, T> {
    }
    A concrete implementation could be as simple as
    Java Code:
    class HashMapT<T> extends HashMap<T, T> implements MapT<T> {
    }
    or could wrap a HashMap<T, T> and forward all method calls to the wrapped map
    Java Code:
    @SuppressWarnings("element-type-mismatch")
    class HashMapT<T> implements MapT<T> {
    
      private Map<T, T> map = new HashMap<T, T>();
    
      @Override
      public int size() {
        return map.size();
      }
    
      @Override
      public boolean isEmpty() {
        return map.isEmpty();
      }
    
      @Override
      public boolean containsKey(Object key) {
        return map.containsKey(key);
      }
    
      @Override
      public boolean containsValue(Object value) {
        return map.containsValue(value);
      }
    
      @Override
      public T get(Object key) {
        return map.get(key);
      }
    
      @Override
      public T put(T key, T value) {
        return map.put(key, value);
      }
    
      @Override
      public T remove(Object key) {
        return map.remove(key);
      }
    
      @Override
      public void putAll(Map<? extends T, ? extends T> m) {
        map.putAll(m);
      }
    
      @Override
      public void clear() {
        map.clear();
      }
    
      @Override
      public Set<T> keySet() {
        return map.keySet();
      }
    
      @Override
      public Collection<T> values() {
        return map.values();
      }
    
      @Override
      public Set<Map.Entry<T, T>> entrySet() {
        return map.entrySet();
      }
    
      @Override
      public String toString() {
        return map.toString();
      }
    
      @Override
      public boolean equals(Object obj) {
        if (obj == this) {
          return true;
        }
        if (!(obj instanceof HashMapT)) {
          return false;
        }
        return map.equals(((HashMapT) obj).map);
      }
    
      @Override
      public int hashCode() {
        return map.hashCode();
      }
    }
    db
    Last edited by DarrylBurke; 01-12-2012 at 04:30 AM.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default Re: Map<T,T> possible in non-generic class?

    That still doesn't fulfill the OPs requirements, which appear to be a desire to store, in the same Map, Doubles, Strings, Integers etc, but the key and value need to be the same.

    I doubt that's doable using generics, and will probably require type checking code.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default Re: Map<T,T> possible in non-generic class?

    Quote Originally Posted by Tolls View Post
    That still doesn't fulfill the OPs requirements, which appear to be a desire to store, in the same Map, Doubles, Strings, Integers etc, but the key and value need to be the same.

    I doubt that's doable using generics, and will probably require type checking code.
    Oh. I misunderstood the requirement to be to create separate Maps where in each Map the key and value would be of the same generic type.

    I agree that Generics can't specify the requirement as I now understand it. But this requirement also looks like being a badly flawed design.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default Re: Map<T,T> possible in non-generic class?

    Quote Originally Posted by DarrylBurke View Post
    Oh. I misunderstood the requirement to be to create separate Maps where in each Map the key and value would be of the same generic type.

    I agree that Generics can't specify the requirement as I now understand it. But this requirement also looks like being a badly flawed design.

    db
    Got to admit that I can't guarantee my interpretation is correct...:)

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default Re: Map<T,T> possible in non-generic class?

    This seems almost possible, but I agree with Darryl's opinion that this seems like a bad design.

    The closest I can come up with is something like this:

    Java Code:
    import java.util.HashMap;
    import java.util.Map;
    
    public class MapTest {
    
    	Map<Object, Object> map = new HashMap<Object, Object>();
    	
    	public <T> void put(Class<T> c, T key, T value){
    		map.put(key, value);
    	}
    	
    	public <T> T get(Class<T> c, T key){
    		return (T) map.get(key);
    	}
    	
    	
    	public static void main(String... args){
    		
    		MapTest mapTest = new MapTest();
    		
    		
    		mapTest.put(String.class, "cats", "test");
    		mapTest.put(Integer.class, 10, 1);
    		
    		mapTest.put(String.class, "cats", 10); //compiler error!
    		
    		System.out.println(mapTest.get(String.class, "cats"));
    		System.out.println(mapTest.get(Integer.class, 10));
    		
    		System.out.println(mapTest.get(Integer.class, "cats")); //compiler error!
    	}
    }
    This is quite a bit of a kludge, as the compiler warnings indicate, but at least it does accomplish the goal of converting possible runtime errors into compile-time errors instead.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default Re: Map<T,T> possible in non-generic class?

    I would say "nice job", but I suspect you feel unclean coming up with that...:)
    KevinWorkman likes this.

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default Re: Map<T,T> possible in non-generic class?

    Quote Originally Posted by Tolls View Post
    I would say "nice job", but I suspect you feel unclean coming up with that...:)
    Ha! Ask for a dirty design, get a dirty solution...
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. help with implementing Generic Array class
    By aqeel2010 in forum New To Java
    Replies: 2
    Last Post: 11-29-2011, 08:40 AM
  2. Writing generic array class
    By aqeel2010 in forum New To Java
    Replies: 6
    Last Post: 11-29-2011, 06:01 AM
  3. How to check instance of a generic class?
    By chan_nguyen in forum New To Java
    Replies: 7
    Last Post: 09-08-2010, 04:20 AM
  4. Replies: 1
    Last Post: 05-16-2008, 11:06 PM
  5. A simple generic class
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •