Results 1 to 10 of 10
- 01-11-2012, 04:53 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 13
- Rep Power
- 0
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:
Any help would be greatly appreciated!Java Code:public class MyClass { private Map<T, T> myMap = new HashMap<T, T>>(); public T get(T key) { return myMap.get(key); } }
- 01-11-2012, 05:52 PM #2
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!
- 01-11-2012, 06:28 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 13
- Rep Power
- 0
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.
- 01-12-2012, 04:27 AM #4
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.
A concrete implementation could be as simple asJava Code:interface MapT<T> extends Map<T, T> { }or could wrap a HashMap<T, T> and forward all method calls to the wrapped mapJava Code:class HashMapT<T> extends HashMap<T, T> implements MapT<T> { }dbJava 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(); } }Last edited by DarrylBurke; 01-12-2012 at 04:30 AM.
Why do they call it rush hour when nothing moves? - Robin Williams
- 01-12-2012, 10:36 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 01-12-2012, 12:45 PM #6
Re: Map<T,T> possible in non-generic class?
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-12-2012, 02:54 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
- 01-12-2012, 03:49 PM #8
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:
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.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! } }How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-12-2012, 04:06 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Map<T,T> possible in non-generic class?
I would say "nice job", but I suspect you feel unclean coming up with that...:)
- 01-12-2012, 04:49 PM #10
Re: Map<T,T> possible in non-generic class?
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
-
help with implementing Generic Array class
By aqeel2010 in forum New To JavaReplies: 2Last Post: 11-29-2011, 08:40 AM -
Writing generic array class
By aqeel2010 in forum New To JavaReplies: 6Last Post: 11-29-2011, 06:01 AM -
How to check instance of a generic class?
By chan_nguyen in forum New To JavaReplies: 7Last Post: 09-08-2010, 04:20 AM -
The type Class is not generic; it cannot be parameterized with arguments <T>
By new_2_java in forum Advanced JavaReplies: 1Last Post: 05-16-2008, 11:06 PM -
A simple generic class
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:41 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks