Bryant
Bryant

Reputation: 714

Eclipse Formatter: Wrap Nested ArrayList Declarations

I have a rather large and annoying nest of parameterized classes, such as the following for example:

HashMap<ExampleClass, ArrayList<ArrayList<ADifferentClass>>> PITA_var = new HashMap<ExampleClass, ArrayList<ArrayList<ADifferentClass>>>();

As you can see, declaring variables that utilize this nested ArrayList is a pain, especially when the declaration extends off-screen. I would like to have Eclipse (okay technically I'm using STS) at least solve that problem, but I have been unable to determine how to do so. Any help?

PS I know it would be a simple matter to declare additional classes that subclass these types; I am already doing that to an extent, and further subclassing would not be practical.

Upvotes: 0

Views: 52

Answers (1)

Paul Boddington
Paul Boddington

Reputation: 37645

I don't use Eclipse, so this answer may not be what you are looking for, but here are a few tips.

Firstly, use interfaces. That type should be

Map<ExampleClass, List<List<ADifferentClass>>>

Secondly, if you can, upgrade to Java 7 or 8. Then you can do:

 Map<ExampleClass, List<List<ADifferentClass>>> map = new HashMap<>();

PS I know it would be a simple matter to declare additional classes that subclass these types; I am already doing that to an extent, and further subclassing would not be practical.

Do not do that. Using subclasses just to get rid of type parameters is considered an antipattern.

Finally, consider whether your design could be improved. There may be a better approach than having a Map where the values are Lists of Lists.

Upvotes: 1

Related Questions