Is there a prettier way of doing the following?
public static <T, E> Map<T, E> loadMap(File file, Map<T, E> map) {
try {
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
Object o = ois.readObject();
if(o instanceof Map) {
map = (Map<T, E>) o;
System.out.println(map);
}
ois.close();
fis.close();
return map;
} catch(IOException | ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
At first I didn’t return anything, because I thought that Objects(Map in this case) were passed by reference, so I just assigned the value of whatever was read, to passed Map. However, this didn’t alter the value outside of the function call.
So now I need a Map to get the types it uses and then return a map.
Map<Foo, Bar> foobar;
foobar = (Map<Foo, Bar>) loadMap(file, foobar);
just isn’t as good looking as plain old loadMap(file, foobar); or foobar = loadMap(file) (These two variants is the ones I like the most
(Looks the most pretty xD))
Cheers!