I'm working on a Java project, and I want to pass a Java object to a method in order to avoid code duplication. This is my code :
private List<Dog> getListDogs(SearchResponse response) {
SearchHit[] searchHit = response.getHits().getHits();
List<Dog> dogList = new ArrayList<>();
if (searchHit.length > 0) {
Arrays.stream(searchHit).forEach(hit -> dogList.add(objectMapper.convertValue(hit.getSourceAsMap(),Dog.class))
);
}
return dogList;
}
I have other objects that need to use the same method like Cat Object and Horse object. Do you have any idea on how I can make my method generic in order to pass just the object type as a parameter?
Something like this:
private List<Generic> getListObject(SearchResponse response , GenericObject object){...