maps of maps of maps of...

6 views
Skip to first unread message

Tim Harsch

unread,
Dec 30, 2009, 7:08:54 PM12/30/09
to google-colle...@googlegroups.com
Is there something in the collecitons to represent map of maps?  Something like compound keys in database, or similar to Perl:
$mymap->{KEY1}{KEY2}{KEY3}=1. 

I realize I can create Maps of Maps, but then I have to handle the book keeping of instantiating maps at each level of access and that could be tedious for some keys >= 2.
Map<STRING,Map<STRING,Map<STRING,INTEGER>>>

Also, extending some of the API methods, like put( K, V) to now be put(K1, K2, K3, V) could take some doin'.

And then to make it generic for any number of keys, well hmmm...

Tim

Dimitris Andreou

unread,
Dec 30, 2009, 7:54:08 PM12/30/09
to Tim Harsch, google-colle...@googlegroups.com
How about something similar to Map<List<K>, V> ? If it fits your case,
it would be a much more compact representation than nested maps
(especially if you are thinking of HashMaps).

Regards,
Dimitris

2009/12/31 Tim Harsch <harsc...@gmail.com>:

> --
> Google Collections Library - users list
> http://groups.google.com/group/google-collections-dev?hl=en
>
> To unsubscribe, send email to:
> google-collections...@googlegroups.com

Jared Levy

unread,
Dec 30, 2009, 8:19:18 PM12/30/09
to Tim Harsch, google-colle...@googlegroups.com
http://code.google.com/p/google-collections/issues/detail?id=173 describe my solution when there are two key fields:
I created a Table interface that maps a two-part key (a row and a column) to a value. 
It includes methods like

V put(R rowKey, C columnKey, V value)
V get(Object rowKey, Object columnKey)

Hopefully, we'll be able to release that some time in the next few months.

Jared


--

Pat Farrell

unread,
Dec 30, 2009, 8:27:57 PM12/30/09
to Jared Levy, Tim Harsch, google-colle...@googlegroups.com
  V put(R rowKey, C columnKey, V value) 
V get(Object rowKey, Object columnKey)

Hopefully, we'll be able to release that some time in the next few months.


And we all are hopeful that its more like
 
  V get(R rowKey, C columnKey) 


Tim Harsch

unread,
Dec 30, 2009, 9:00:43 PM12/30/09
to Pat Farrell, Jared Levy, google-colle...@googlegroups.com
I blogged the code to the Triplet class I mentioned:
http://harschware.blogspot.com/2009/12/triplet-class.html

Dimitris Andreou

unread,
Dec 30, 2009, 9:17:14 PM12/30/09
to Jared Levy, Tim Harsch, google-colle...@googlegroups.com
Given a Pair type, is there a non-syntactic difference between
Table<R,C,V> and a Map<Pair<C, R>, V> ? Can you describe the main
motivation for it?

Regards,
Dimitris

2009/12/31 Jared Levy <jared....@gmail.com>:

Craig P. Motlin

unread,
Dec 31, 2009, 7:47:35 AM12/31/09
to Pat Farrell, Jared Levy, Tim Harsch, google-colle...@googlegroups.com
Get should take Object, not generic types.  This is one of areas where Sun actually got it right.

Jared Levy

unread,
Dec 31, 2009, 8:46:15 PM12/31/09
to Dimitris Andreou, Tim Harsch, google-colle...@googlegroups.com
The Table interface has several additional methods, such as

Set<R> rowSet();
Map<C, V> column(R row);

In addition, Table's get() and set() are more convenient than creating a pair every time you want to access the data.

The main motivation is that there are often situations where you want a two-part key. A Table simplifies the resulting code, just as a Multimap simplifies a one-to-many mapping.

Dimitris Andreou

unread,
Jan 1, 2010, 5:47:29 AM1/1/10
to Jared Levy, Tim Harsch, google-colle...@googlegroups.com
I see. I was curious about you doing all this work for just 2d
keys/tables (this doesn't scale to 3d for example, unless someone
repeats the whole exercise) but I guess it's a quite a sweet spot
anyway (tables in UIs, in databases, and in-between).

I'm eager to see in the future your answer for dense tables and
whether relational operators are introduced (and which).

Regards,
Dimitris

2010/1/1 Jared Levy <jared....@gmail.com>:

Reply all
Reply to author
Forward
0 new messages