Concise code with Generics and varargs

Discussions

Blogs: Concise code with Generics and varargs

  1. Concise code with Generics and varargs (10 messages)

    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<String> programmingLanguages = new ArrayList<String>(3);
    programmingLanguages.add("Java");
    programmingLanguages.add("C++");
    programmingLanguages.add("Ruby");

    Now looks like this:

    List<String> 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 Messages (10)

  2. I've been using this more and more to quickly initialize data structures in-place:
    Map<String, String> langs = new HashMap<String, String>(3) {{
        put("Java", "Wicked");
        put("C++", "Ok");
        put("Ruby", "Interesting");
    }};
  3. Nice little dirty trick!!!

    But, there are some drawbacks, that can hurt more or less:
    - this creates new anonymous subclass of HashMap, which has to be loaded, verified, compiled... and in turn slows down your app and makes it a bit 'fatter'.
  4. But I get a warning[ Go to top ]

    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");
    }};
    But I am getting a serialVersionUID warning until I do this: Map langs = new HashMap(3) {/** * */ private static final long serialVersionUID = -6312098211258969539L; { put("Java", "Wicked"); put("C++", "Ok"); put("Ruby", "Interesting"); }};
  5. What is the link to the article?
  6. http://robsmith1976.blogspot.com/
  7. And using static imports, it can become even more concise:

    List<String> languages = newList( "Java", "C++", "Ruby");
  8. Concise code with Generics and varargs[ Go to top ]

    Are you looking for something interesting to do with Generics and varargs?

    Isn't this back to front? It seems to me that new language features should solve existing problems or add missing functionality. If we have to look for things to do with new language features, isn't that a sign that something's wrong somewhere?
  9. A Better Way ...[ Go to top ]

    As pointed out by a comment to my blog, a better alternative is to use the Arrays.asList(Object... args) method (http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html#asList(T...)) provided in the standard library. I was not aware that this method had been changed to accept a vararg argument.
  10. NOT a better way...[ Go to top ]

    Arrays.asList(Object... args) returns a fixed size Arrays.ArrayList, not a java.util.ArrayList.
    So Arrays.asList("a").add("b) will throw a java.lang.UnsupportedOperationException
  11. Good one[ Go to top ]

    Good.. good to see that varargs is getting into use