I am trying to write a generic method to print map of different object types with same name belongs to different classes :
private<T> void print(T obj) {
//Some proecssing on obj.keySet()
obj.getMapOfObjects().values().stream().forEach(System.out::println);
}
for 2 classes B and C, as follows.
public class B implements A {
private Map<Object, List<String>> mapOfObjects = new HashMap<>();
public void setMapOfObjects(Map<Object, List<String>> mapOfObjects) {
this.mapOfObjects = mapOfObjects;
}
public Map<Object, List<String>> getMapOfObjects() {
return mapOfObjects;
}
}
and
public class C implements A {
private Map<Integer, List<String>> mapOfObjects = new HashMap<>();
public void setMapOfObjects(Map<Integer, List<String>> mapOfObjects) {
this.mapOfObjects = mapOfObjects;
}
public Map<Integer, List<String>> getMapOfObjects() {
return mapOfObjects;
}
}
A does not have getMapOfObjects() method as return types are different.
I am getting exception on line obj.getMapOfObjects().values() as "method getMapOfObjects() undefined on type T". I want to understand whether I can write above method by the use of generics or functions in java 8 and if yes then how to write?