Garbage Burrito

Topic

Author Topic
Rheaghen-za
rheaghen

Java Generics

06 14, 2007 @ 03:58PM

Ahhh... Generics, they make me smile!

heres a nice generic Collection Cleanup method that should totally be available in the java.util.Collections class

public static <U> void
   condense(java.util.Collection<U> collection) {
   Object[] arr_o = collection.toArray();
   collection.clear();
   for(Object o:arr_o)
      if(o!=null)
         collection.add((U)o);
   
}

Comments

Author Comments
Benderflydw8
Ben Kittrell
06 22, 2007 @ 06:55AM

Ooops, I guess I don't have my own forum on my RSS Feed Reader anymore. Missed this one completely.

I agree, generics are cool. I don't know how many times I've had to call an undocumented service that returns a collection, and had no idea what type was in the collection.

The foreach is a nice touch too. That's something I always missed from Perl.

Unfortunately we're still on Websphere 5.1 here, which means Java 1.4. They're making a push to get away from IBM, so it's easier to keep up with the times, but I'm sure they'll get tired of extending my contract by then.

Redwhitehouse
Jesse
09 23, 2007 @ 01:12AM

Oh you are a blessed one, Ben, a blessed one.


powered by : Doodlekit Online Free Website Builder : developed by : Doodlebit™ Website Company