|
![](/i/fill.gif) |
Warp wrote:
> Is there a way in Java to shorten those long type names?
>
Not that I know of. In fact, it's even more annoying in that you have to
type the whole type twice when creating an object.
Map<String, ArrayList<ArrayList<String>>> theMap =
new HashMap<String, ArrayList<ArrayList<String>>>();
ArrayList<ArrayList<String>> someList = new ArrayList<ArrayList<String>>();
Of course, you could also use interfaces for the type and generic
parameters, only using a real class for the actual object you're creating,
making it a bit shorter:
Map<String, List<List<String>>> theMap =
new HashMap<String, List<List<String>>>();
List<List<String>> someList = new ArrayList<List<String>>();
//and let's continue the example
List<String> deeperList = new ArrayList<String>();
List<String> otherDeeperList = new ArrayList<String>();
deeperList.add("one");
deeperList.add("two");
otherDeeperList.add("three");
otherDeeperList.add("four");
someList.add(deeperList);
someList.add(otherDeeperList);
theMap.put("foo", someList);
It's interesting to note that in scripting languages, all that could be a
one-liner. In Javascript:
var theMap = {"foo": [["one", "two"], ["three", "four"]]};
And even more interesting to note that C++0x will allow an initializer
syntax almost as short as that :)
Post a reply to this message
|
![](/i/fill.gif) |