2007年5月17日星期四

Concise code with Generics and varargs

Are you looking for something interesting to do with Generics and varargs? If so then you may want to check out Rob Smith's blog on Concise Java Collection Syntax with varargs.
So code that looked like this before:

List programmingLanguages = new ArrayList(3);
programmingLanguages.add("Java");
programmingLanguages.add("C++");
programmingLanguages.add("Ruby");

Now looks like this:

List programmingLanguages = CollectionUtils.newList(
"Java", "C++", "Ruby");

With all the focus on type safety, it refreshing to see an interesting use for Generics. Is this just and edge case or are there any other techniques that you’ve discovered and would like to share.


Threaded replies
· Concise code with Generics and varargs by Kirk Pepperdine on Wed Nov 30 05:42:24 EST 2005
· Concise code with Generics and varargs by Geert Bevin on Wed Nov 30 06:04:10 EST 2005
· Concise code with Generics and varargs by Mileta Cekovic on Thu Dec 01 10:58:04 EST 2005
· But I get a warning by Janos Mucsi on Fri Mar 02 17:39:32 EST 2007
· Concise code with Generics and varargs by Erik van Oosten on Wed Nov 30 11:17:20 EST 2005
· Concise code with Generics and varargs by Joshua White on Thu Dec 01 14:04:47 EST 2005
· Concise code with Generics and varargs by Werner Schulz on Thu Dec 01 20:12:21 EST 2005
· Concise code with Generics and varargs by D S on Tue Dec 06 04:20:37 EST 2005
· A Better Way ... by Rob Smith on Tue Dec 06 22:37:56 EST 2005
· NOT a better way... by Ivo Houbrechts on Wed Dec 07 10:19:15 EST 2005
· Good one by murukesh s on Wed Dec 14 00:33:16 EST 2005

Message #192428 Post reply Post reply Post reply Go to top Go to top Go to top

Concise code with Generics and varargs
Posted by: Geert Bevin on ??? 30, 2005 in response to Message #192425
I've been using this more and more to quickly initialize data structures in-place:
Map langs = new HashMap(3) {{
put("Java", "Wicked");
put("C++", "Ok");
put("Ruby", "Interesting");
}};

没有评论: