News for hackers. Stuff that matters.
Re:Java 7 - What's new? Release date, code examples and perf... by vlisivka on Sunday April 11, 2010 @02:54PM (#1257383)
If you cannot wait for Java 1.7, use usual trick with anonymous classes and static initializer block:

  Java 1.7:
  List<String> list = ["item"];

  Java 1.2:
  List<String> list = new AList<String>() {{ items=["item"]; }};
where:

  public class AList<T> implements List<T> {
    protected T[] items;
    // Implementation
 }

  Set<String> set = {"item"};
  Set<String> set = new ASet<String>() {{ items={"item"}; }};

  Map<String, Integer> map = {"key" : 1};
  Map<String, Integer> map = new AMap<String, Integer>() {{ keys=["key"]; values=[1]; }};

I can post detailed explanation and examples of this technique, which I love to use, in my blog, if somebody will be interested. (English is foreign language to me).

It is much better than to use full blown implementation and bunch of setters just to pass around one or two elements.

I often use this technique to pass optional data to/from methods or to program in declarative style in java.

Re:Java 7 - What's new? Release date, code examples and perf... by ericlavigne on Sunday April 11, 2010 @05:08PM (#1257606)
Or use Google Collections in Java 1.5 or higher.

http://code.google.com/p/google-collections/

List<String> list1 = Lists.newArrayList("item"); List<String> list2 = Lists.newArrayList("item1","item2");

Of course, none of these workarounds are close to what Java 7 offers. Just a few more months :-)

Re:Java 7 - What's new? Release date, code examples and perf... by vlisivka on Sunday April 11, 2010 @07:09PM (#1257874)
> Or use Google Collections in Java 1.5 or higher.

See Sandman reply.

Re:Java 7 - What's new? Release date, code examples and perf... by Sandman on Sunday April 11, 2010 @06:49PM (#1257841)
For lists, you can also do this: List<String> strings = new ArrayList<String> (Arrays.asList("one", "two", "three"));
Re:Java 7 - What's new? Release date, code examples and perf... by vlisivka on Sunday April 11, 2010 @07:14PM (#1257893)
Yes, you right. This is very cheap way to initialize ArrayList. I use it too.

Did you know something similar for Map's? :-)

Re:Java 7 - What's new? Release date, code examples and perf... by Sandman on Sunday April 11, 2010 @08:22PM (#1258014)
Unfortunately, no.
Re:Java 7 - What's new? Release date, code examples and perf... by catch404 on Monday April 12, 2010 @04:29AM (#1258689)
I'm guessing the same applies for C#, does anyone know if there are language supported features for lists like the new java ones for C#?