Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > nested generic HashMap problem

Reply
Thread Tools

nested generic HashMap problem

 
 
Lew
Guest
Posts: n/a
 
      04-27-2010
Chris Riesbeck wrote:
> I don't follow the "don't have a type" part here. The correlation I was
> trying to capture was
>
> * * T get(Demo<T>, long)
>
> using an underlying Map(Demo<T>, Map<long, T>). That seems to me to be
> well-defined, just not definable in Java.
>


What do you mean, not definable? That's exactly how you define it,
what you wrote just there, modulo the typos.

class Registry <T>
{
private final Map <Demo <T>, Map <Long, T>> demoMaps =
new HashMap <Demo <T>, Map <Long, T>> ();

...
}

--
Lew
 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      04-27-2010
Chris Riesbeck wrote:

> I don't follow the "don't have a type" part here. The correlation I was
> trying to capture was
>
> T get(Demo<T>, long)
>
> using an underlying Map(Demo<T>, Map<long, T>). That seems to me to be
> well-defined, just not definable in Java.



What I was referring to was the actual type used in the program. Cache
doesn't have a type (parameter) and so the hash map doesn't either.
Thus, Java says "I don't have a type to check here" when you try to
return a value.

private HashMap<Class<?>, HashMap<Long, ?>> maps
= new HashMap<Class<?>, HashMap<Long, ?>>();

The ? says to me "I have no type." YMMV.

In your design, you have a type, yes, but you need to tell the compiler.
 
Reply With Quote
 
 
 
 
Chris Riesbeck
Guest
Posts: n/a
 
      04-27-2010
Lew wrote:
> Chris Riesbeck wrote:
>> I don't follow the "don't have a type" part here. The correlation I was
>> trying to capture was
>>
>> T get(Demo<T>, long)
>>
>> using an underlying Map(Demo<T>, Map<long, T>). That seems to me to be
>> well-defined, just not definable in Java.
>>

>
> What do you mean, not definable? That's exactly how you define it,
> what you wrote just there, modulo the typos.
>
> class Registry <T>
> {
> private final Map <Demo <T>, Map <Long, T>> demoMaps =
> new HashMap <Demo <T>, Map <Long, T>> ();
>
> ...
> }
>
> --
> Lew


That defines a Map of Maps of one type T. I.e., you can make one
Registry where an instance of Demo<Book> retrieves a Map of type <Long,
Book>, and another Registry where a key of type Demo<Author> retrieves a
Map of type <Long, Author>.

But you can't define a single Registry where a Demo<Book> retrieves a
Map<Long, Book> and Demo<Author> retrieves a Map<Long, Author>.
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      04-27-2010
Chris Riesbeck wrote:
>>> I don't follow the "don't have a type" part here. The correlation I was
>>> trying to capture was
>>>
>>> * * T get(Demo<T>, long)
>>>
>>> using an underlying Map(Demo<T>, Map<long, T>). That seems to me to be
>>> well-defined, just not definable in Java.

>


Lew wrote:
>> What do you mean, not definable? *That's exactly how you define it,
>> what you wrote just there, modulo the typos.
>>
>> class Registry <T>
>> {
>> *private final Map <Demo <T>, Map <Long, T>> demoMaps =
>> * *new HashMap <Demo <T>, Map <Long, T>> ();
>>
>> *...
>> }

>


>> --
>> Lew

>


Don't quote sigs.

Chris Riesbeck wrote:
> That defines a Map of Maps of one type T. I.e., you can make one
> Registry where an instance of Demo<Book> retrieves a Map of type <Long,
> Book>, and another Registry where a key of type Demo<Author> retrieves a
> Map of type <Long, Author>.
>
> But you can't define a single Registry where a Demo<Book> retrieves a
> Map<Long, Book> and Demo<Author> retrieves a Map<Long, Author>.
>


That isn't what you asked for in the post to which I replied.

--
Lew
don't quote sigs
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      04-27-2010
On 4/27/2010 3:33 AM, Lew wrote:
> Daniel Pitts wrote:
>>> As a side note, I would make base and cache final.

>
> Chris Riesbeck wrote:
>> I agree. It's a habit I still haven't gotten into, even though I const
>> like crazy in C++.

>
> The semantics of Java's 'final' differ from C++'s 'const' somewhat, as
> with so many things that are similar between the languages but not quite
> the same.
>

Yes, I miss C++ "const", actually. I hated it when I was first learning
C++, because it stopped me from doing things I thought I should be able
to do (but were actually bad to do).

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      04-27-2010
On 4/27/2010 7:39 AM, Lew wrote:
> Chris Riesbeck wrote:
>> I don't follow the "don't have a type" part here. The correlation I was
>> trying to capture was
>>
>> T get(Demo<T>, long)
>>
>> using an underlying Map(Demo<T>, Map<long, T>). That seems to me to be
>> well-defined, just not definable in Java.
>>

>
> What do you mean, not definable? That's exactly how you define it,
> what you wrote just there, modulo the typos.
>
> class Registry<T>
> {
> private final Map<Demo<T>, Map<Long, T>> demoMaps =
> new HashMap<Demo<T>, Map<Long, T>> ();
>
> ...
> }

Lew, the problem is "T" is different for every key of the map.

map.put(String.class, new Map<Long, String>());
map.put(Foo.class, new Map<Long, Foo>());

There is no definition for Map<Class<...>, ...> which will fit the above
usecases.

One will need to carefully ensure the key matches the value, and cast
appropriately, or not use a java.util.Map.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      04-27-2010
On 4/27/2010 12:02 PM, Lew wrote:
> Chris Riesbeck wrote:
>>>> I don't follow the "don't have a type" part here. The correlation I was
>>>> trying to capture was
>>>>
>>>> T get(Demo<T>, long)
>>>>
>>>> using an underlying Map(Demo<T>, Map<long, T>). That seems to me to be
>>>> well-defined, just not definable in Java.

>>

>
> Lew wrote:
>>> What do you mean, not definable? That's exactly how you define it,
>>> what you wrote just there, modulo the typos.
>>>
>>> class Registry<T>
>>> {
>>> private final Map<Demo<T>, Map<Long, T>> demoMaps =
>>> new HashMap<Demo<T>, Map<Long, T>> ();
>>>
>>> ...
>>> }

>>

>
>>> --
>>> Lew

>>

>
> Don't quote sigs.
>
> Chris Riesbeck wrote:
>> That defines a Map of Maps of one type T. I.e., you can make one
>> Registry where an instance of Demo<Book> retrieves a Map of type<Long,
>> Book>, and another Registry where a key of type Demo<Author> retrieves a
>> Map of type<Long, Author>.
>>
>> But you can't define a single Registry where a Demo<Book> retrieves a
>> Map<Long, Book> and Demo<Author> retrieves a Map<Long, Author>.
>>

>
> That isn't what you asked for in the post to which I replied.

It has been the problem that the OP has been trying to solve this entire
thread.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      04-27-2010
Chris Riesbeck wrote:
>>> That defines a Map of Maps of one type T. I.e., you can make one
>>> Registry where an instance of Demo<Book> retrieves a Map of type<Long,
>>> Book>, and another Registry where a key of type Demo<Author>
>>> retrieves a
>>> Map of type<Long, Author>.
>>>
>>> But you can't define a single Registry where a Demo<Book> retrieves a
>>> Map<Long, Book> and Demo<Author> retrieves a Map<Long, Author>.


Lew wrote:
>> That isn't what you asked for in the post to which I replied.


Daniel Pitts wrote:
> It has been the problem that the OP has been trying to solve this entire
> thread.


In which he referred to wildcarded generics. In the post to which I
responded, he seemed to have changed tack and remarked on what on the surface
was a very specific point. While true, Daniel, your point is irrelevant.

I believe that what the OP is doing is best served by finding some upper-bound
type for the keys, at least, of the primary and secondary map layers, and
using it for T, perhaps making the secondary value simply 'Object'. What
they're trying to do is never going to be painless.

Their desire to have the structures be typeless dooms them to conflict with
the heart of generics. They will have to cast somewhere. Fortunately, they
have a class token buried in all that goop, so they can at least hide the
'catch (ClassCastException ex)' blocks down in the depths and throw only
runtime exceptions. They'll need to Javadoc the hell out of the structures so
that everyone knows that everything has to match the class token.

As I finished typing this I see your other post, Daniel, where you made many
of these points as well.

--
Lew
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      04-27-2010
On 27-04-2010 15:17, Daniel Pitts wrote:
> On 4/27/2010 3:33 AM, Lew wrote:
>> Daniel Pitts wrote:
>>>> As a side note, I would make base and cache final.

>>
>> Chris Riesbeck wrote:
>>> I agree. It's a habit I still haven't gotten into, even though I const
>>> like crazy in C++.

>>
>> The semantics of Java's 'final' differ from C++'s 'const' somewhat, as
>> with so many things that are similar between the languages but not quite
>> the same.

>
> Yes, I miss C++ "const", actually.


+1

Arne

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reuse HashMap$Entry (or HashMap in total) to avoid millions of allocations Vince Darley Java 4 03-02-2010 07:48 AM
generic interfaces with generic methods Murat Tasan Java 1 02-03-2009 12:17 PM
java.util.Properties extending from HashMap<Object, Object> insteadof HashMap<String, String> Rakesh Java 10 04-08-2008 04:22 AM
Generic class in a non generic class nramnath@gmail.com Java 2 07-04-2006 07:24 AM
Generic Newbie HashMap Query MrFredBloggs@hotmail.com Java 9 05-02-2005 04:01 PM



Advertisments