I have this method to transform a List
to a Map
using one of the properties of the elements of the list:
For short it looks like this:
private Map<String, List<Diagnostic<? extends JavaFileObject>>> toMap( List<Diagnostic<? extends JavaFileObject>> diagnostics ) {
Map<String, List<Diagnostic<? extends JavaFileObject>>> result = new HashMap<String, List<Diagnostic<? extends JavaFileObject>>>();
for ( Diagnostic<? extends JavaFileObject> d : diagnostics ) {
List<Diagnostic<? extends JavaFileObject>> list = null;
if ( !result.containsKey( d.getCode() ) ) {
list = new ArrayList<Diagnostic<? extends JavaFileObject>>();
result.put( d.getCode(), list );
} else {
list = result.get( d.getCode() );
}
assert list != null;
list.add( d );
}
return result;
}
Yiack!..
I like genercis a lot, I use java prior to them and I don't want to go back to the cast everything era, but when a generic contains as element a generic element it self, things go messy.
I know in Java1.7 we will be able to use the "diamond" operator, but there should be another way.
This is what it would look like in a non-generic version:
private Map toMap( List diagnostics ) {
Map result = new HashMap();
for( Object o : diagnostics ) {
Diagnostic d = ( Diagnostic ) o;
List list = null;
if( !result.containsKey( d.getCode() ) ) {
list = new ArrayList();
result.put( d.getCode() , list );
} else {
list = result.get( d.getCode() );
}
assert list != null;
list.add( d );
}
return result;
}
Approximately, I didn't try to compile it.
How other languages handle this? C# for instance?, Scala? I liked a lot the way SML or Haskell do handle, but something I think too much magic may hurt ( but this is subjective of course )
Is there a workaround for this?
val
for inferred types? (projectlombok.org/features/val.html) It doesn't clear the whole mess, but it does shrink the code.var
in C# provides a similar solution. – Phosphene<? extends JavaFileObject>
I'll use it later – Deathwatch