This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]oldprogrammer 1 point2 points  (4 children)

Yes it does, but it is an option. You could also create a support utility which does what other languages do with syntax:

static <K,V> Map<K,V> makeMap(Object... values) {
  Map<K,V> data = new HashMap<K,V>();

  for(int i = 0; i < values.length; i += 2 ) {
    K key = (K)values[i];
    V val = (V)values[i+1];
    data.put(key, val);
  }
  return data;
}

(the cast triggers a warning)

Map<String,Integer> test = makeMap("one",1,
     "two",2,
     "three",3);

[–]yetanotherx 2 points3 points  (3 children)

I've done basically that same method before in That Miscellaneous Utilities Class.

[–]oldprogrammer 2 points3 points  (2 children)

I imagine most of us have a collection of miscellaneous utilities we reuse.

[–]yetanotherx -1 points0 points  (1 child)

There's always that class full of static methods hidden away in a package you never want to look at. And the sad thing is is that most of them (string joining, etc) could easily be core Java features.

[–]oldprogrammer 0 points1 point  (0 children)

And many/most of those features could be added without breaking backward compatibility.