commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Phil Surette <psure...@espial.com>
Subject RE: Commons Collections:Suggested MapUtils enhancements
Date Thu, 10 Oct 2002 16:10:47 GMT
That sounds plenty fast to me! Thanks for taking this on.

As to the method signatures:

I originally suggested that toMap should have a heavily overloaded
method signature. The reason was to ease the pain of having to 
create all those java.lang.xxx objects instead of using basic types.
It's a lot easier to write 
toMap(new int[][]{{1,1},{2,4}})
than
toMap(new Object[][]{{new Integer(1), new Integer(1), {new Integer{2}, new
Integer{4}})
So I'd like to see at least
Map toMap(int[][])
Map toMap(Object[][])
The int[][] version would convert ints to Integers which would be a nice
convenience. 

This may be blue sky, but something like
Map toMap(String[][], Class keyClass, Class valueClass)
would be great. The keyClass and valueClass would be java.lang type wrapper
classes
(Integer.class, Long.class, Boolean.class etc.). The toMap would then call
the valueOf method of the keyClass to construct an appropriate object from
that key
string.

For example:
I want to create a Map that maps userNames to Integer ids (say I'm creating
a mock object to simulate a database call in a unit test).
I can say:
Map userNameToIdMap = MapUtils.toMap(
	new String[][]{{"spiderman", "1"}, {"superman", "22"}, {"batman",
"500"}}, 	String.class, Integer.class);
This will create the same thing as if I had written:
Map userNameToIdMap = new HashMap();
userNameToIdMap.put("spiderman", new Integer(1));
userNameToIdMap.put("superman", new Integer(22));
userNameToIdMap.put("batman", new Integer(500));

Also, I think the method sig should take two-dimensional Object[][]. Yes, an
Object array
is technically an Object, so you can pass an Object[][] into an Object[]
parameter, but if what you're expecting is an Object[][] created
like this: {{"hello", "goodbye"}{"fast", "slow"}} that should be
what the sig says  - and gives a nice hint to the end user as to what to
pass in.



-----Original Message-----
From: Moritz Petersen [mailto:moritzpetersen@mac.com]
Sent: Thursday, October 10, 2002 11:08 AM
To: Jakarta Commons Users List
Subject: RE: Commons Collections:Suggested MapUtils enhancements


I will develop toMap and asMap, but I will not finish it before Saturday.
So, if it is urgent, someone else should do it. By the weekend I should get
it done.
Comments? Suggestions?

Regards,

Moritz.

> -----Original Message-----
> From: Phil Surette [mailto:psurette@espial.com]
> Sent: Thursday, October 10, 2002 4:49 PM
> To: 'Jakarta Commons Users List'
> Subject: RE: Commons Collections:Suggested MapUtils enhancements
>
>
> Moritz, are you saying that you will develop the toMap and
> asMap methods? If so, great! If not, I'll add it to my
> TODO list and try get to it sometime after I finish another
> project I'm working on...
>
>
> -----Original Message-----
> From: Moritz Petersen [mailto:moritzpetersen@mac.com]
> Sent: Thursday, October 10, 2002 4:30 AM
> To: Jakarta Commons Users List
> Subject: RE: Commons Collections:Suggested MapUtils enhancements
>
>
> I can do that.
>
> -Moritz.
>
> > -----Original Message-----
> > From: Stephen Colebourne [mailto:scolebourne@btopenworld.com]
> > Sent: Thursday, October 10, 2002 10:15 AM
> > To: Jakarta Commons Users List
> > Subject: Re: Commons Collections:Suggested MapUtils enhancements
> >
> >
> > Note that the semantics of  asMap()  and toMap() are different.
> >
> > asMap() will wrap the object array in a map interface, however,
> you cannot
> > then add items to the map, as its data structure is the array.
> >
> > toMap() will copy the object array into a map, thus it acts as an
> > initializer only.
> >
> > IMO, both methods would be useful additions to MapUtils.
> > Also, a toList() would be a useful addition to ListUtils.
> >
> > I would suggest that you should stick to Object[] for the method
> > signatures.
> > You might also want to note that an ArrayUtils will be added to [lang]
> > project fairly soon that could contain the int[] to Object[] containing
> > Integer objects method.
> >
> > If you would like to write the code please write to the
> > commons-dev list (to
> > ensure no duplicated work), and see the directions for writing patches
> > posted on the website.
> >
> > Stephen (committer)
> >
> > From: "Moritz Petersen" <moritzpetersen@mac.com>
> > > > Also, what about a nice way of initializing Maps? Languages such as
> > > > Perl and
> > > > Python have very nice ways of initializing hashes. Compare
> > > > [python] squares = {1:1, 2:4, 3:9}
> > > > [java] Map squares = new HashMap();
> > > > [java] squares.put(new Integer(1), new Integer(1));
> > > > [java] squares.put(new Integer(2), new Integer(4));
> > > > [java] squares.put(new Integer(3), new Integer(9));
> > > >
> > > > In java, array initializers come closest to python's
> initializer. Why
> > > > not
> > > > have a MapFactory class that has methods like
> > > > public Map createMap(int[][] pairs)
> > > > public Map createMap(String[][] pairs)
> > > >
> > > > Then we could have
> > > > [java] Map squares = MapFactory.createMap(new
> > > > int[][]{{1,1},{2,4},{3,9}});
> > > > [java] Map genusSpecies = MapFactory.createMap(new String[][]
> > > > {{"homo","sapiens"},{"homo","habilis"},{"ursus","negra"}});
> > >
> > >
> > > I like this idea!
> > > It should be more general. Look at java.util.Arrays#asList(Object[]).
> > > This method can be used to initialize Collections:
> > >
> > >
> > >      Collection c = Arrays.asList(new Object[] { "foo", "bar" });
> > >
> > >
> > > There should be a similar way to create Maps:
> > >
> > >
> > >      Map m = MapUtils.asMap(new Object[] { {"foo", "bar"}, {"key", new
> > > Integer(1)} });
> > >
> > >
> > > (what i mean is: you are right, but the method should just take Object
> > > arrays...)
> > >
> > >
> > > -Moritz.
> > >
> > >
> > > --
> > > To unsubscribe, e-mail:
> > <mailto:commons-user-unsubscribe@jakarta.apache.org>
> > > For additional commands, e-mail:
> > <mailto:commons-user-help@jakarta.apache.org>
> > >
> >
> >
> > --
> > To unsubscribe, e-mail:
> <mailto:commons-user-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail:
> <mailto:commons-user-help@jakarta.apache.org>
>
>
>
> --
> To unsubscribe, e-mail:
> <mailto:commons-user-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail:
> <mailto:commons-user-help@jakarta.apache.org>
>


--
To unsubscribe, e-mail:
<mailto:commons-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail:
<mailto:commons-user-help@jakarta.apache.org>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message