Reusing type parameter value in java

I just saw that someone posted the following post in ugly code:

public static Tuple<ArrayList<ArrayList<ArrayList<String>>>, ArrayList<ArrayList<ArrayList<String>>>> split( ArrayList<ArrayList<ArrayList<String>>> data, [..]); 

(layout with me, in a ridiculous attempt to get this semi-readable)

I was looking for a way to make this look a bit like this (non-functional) code:

 TypeParam T = ArrayList<ArrayList<ArrayList<String>>>; public static Tuple<T,T> split( T data, [..]); 

So far, the best solution I have found is to define a class (in this example, class Data ) that extends ArrayList<ArrayList<ArrayList<String>>> , which makes the code look like this:

 public static Tuple<Data, Data> split( Data data, [..]); 

Although this method is quite satisfactory, I do not want to give up the possibility that there is some way to use generics that I am missing, and I am wondering if Java has a way to do this even more aesthetically.

Another solution I'm playing with is to use the Annotation Handler to fix this for me, but I feel it misses a certain simplicity.

+4
source share
2 answers

What about

  public static <T extends ArrayList<ArrayList<String>>> Tuple<T,T> split(T data); 

But I agree with @esej that you probably want some class to encapsulate the list-list-list-string-data structure.

I'm also not sure that there should be an ArrayList hardcoded (as opposed to using the List interface).

+7
source

You can do something like:

 public static <T extends List<List<List<String>>>> Tuple<T, T> split(T data) { } 

It does not seem to look better than IMO.

0
source

All Articles