commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Paul Feuer" <>
Subject [collections] proposed new features - MapIndexing, MapPartitioning
Date Tue, 11 Apr 2006 23:37:52 GMT
Hi all,

(I looked at various FAQ's and some previous mailing list answers to
"I want to contribute" and I hope I do it right ;)

I'm a fan of collections and use it all over the place, but have a two
of functions I'd like to propose. I use these functions in some
operations of Collections of Maps that are returned by our database
layer at work. (The proposed code, however, is not the property of the
company - it was specially developed for including in the commons

I'm not sure if org.apache.commons.collections.functors is the best
place for these, but the map package seemed to be only Map
implementations, so the functors package seemed like a better home.
Tho I was surprised to see no TestCases in the functors package.

The indexing operation has come in handy when sending DB results out
to a JSP - transforming it this way allows easy keyed access to each

The partitioning operation is useful when we need to bucket groups of records.

In each case, it could be argued that, given a large enough
collection, someone would not want to repeat the traversal of the
collection if they were going to do addition per-record processing,
but the same could be said for a lot of the Transformer
implementations. They're great commodity operations.

Links are provided to my proposed implementations and test cases below:

Let me know if this is not the right approach to contributing!! I'm
looking to get involved and want to do it right!



1) MapIndexingTransformer
proposed package: org.apache.commons.collections.functors (?)

Transforms a Collection of Maps into a Map using a value of each Map
to key that Map in the output.

In pseudocode:
   indexValueToMapMap = Map
   for each record in input Collection
       get value from record at indexKey
       indexValueToMapMap[ index ] = record

Thus the input:
  Collection [
       Map[ "ID"=1, "NAME"="Paul" ],
       Map[ "ID"=2, "NAME"="Kim" ]

is indexed on "ID" into
   Map [
       1 = Map[ "ID"=1, "NAME"="Paul" ],
       2 = Map[ "ID"=2, "NAME"="Kim" ]

2) MapPartitioningTransformer
proposed package: org.apache.commons.collections.functors (?)

Transform a Collection of Maps by grouping together those Maps that
share a common value for some key. In other words, the input to this
Transformer is a Collection of Maps as you might see coming from a
database, and for each of the records, they may share some common
value at a given key. "Partitioning on that value" will create
collections for each unique value of that key in the input collection.

In pseudocode:

partitionNameToCollection = Map

for each record of inputCollection
   get value as partitionName from record at partitionKey
   with partitionNameToCollection add record at partitionName

return partitionNameToCollection

Thus the input:

    Map[ "ID"=1, "GROUP"="New York City", "NAME"="Paul" ]
    Map[ "ID"=2, "GROUP"="New York City", "NAME"="Kim" ]
    Map[ "ID"=3, "GROUP"="Atlanta", "NAME"="Marcia" ]
    Map[ "ID"=4, "GROUP"="Atlanta", "NAME"="Will" ]

is partitioned on "GROUP" into:

    "group1" = Collection[
                 Map[ "ID"=1, "GROUP"="New York City", "NAME"="Paul" ]
                 Map[ "ID"=2, "GROUP"="New York City", "NAME"="Kim" ]
    "group2" = Collection[
                 Map[ "ID"=3, "GROUP"="Atlanta", "NAME"="Marcia" ]
                 Map[ "ID"=4, "GROUP"="Atlanta", "NAME"="Will" ]

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message