commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "matthew.hawthorne" <>
Subject Re: collections size
Date Fri, 19 Mar 2004 19:26:44 GMT
Melzer, Steven wrote:
> so i wrote the following:
> 	public static int size(Object list) throws IllegalArgumentException {
> 		int i = 0;
> 		if (list instanceof Collection) {
> 			i = ((Collection)list).size();
> 		} else if (list instanceof Map) {
> 			i = ((Map)list).size();
> 		} else {
> 			i = Array.getLength(list);
> 		}
> 		return i;
> thought it would be nice to include this functionality in an upcoming commons collection

I understand the underlying idea behind your suggestion, but I don't 
quite get the use case.  Why create conditional llogic to get the size 
of an arbitrary list, when you'd then have to implement the same 
conditional logic to get the items out of the list?

If your goal is to treat all lists in a similar way, I think you'd be 
better of doing something like:

public static Collection toCollection(Object list) {
   if(list instanceof Collection)
     return list
   else if (list instanceof Map)
     return ((Map)list).entrySet()
     return Arrays.asList(list)

And then doing:

list = getListFromSomewhere()

I think this is a better solution, although I have the feeling that I'm 
missing something even more obvious here.

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

View raw message