I basically have my own custom list which has a bunch of functionality but since I am manipulating the same kinds of lists I wanted to extend this custom list with a defined type and want to be able to use the parent functions and return the child type.
Example:
//Object within the list
public class ChildObject {
private string name;
// getters, setters, methods, etc.
}
// Parent custom list
public class ParentList<T> extends ArrayList<T> {
public ParentList<T> filter(Predicate<T> predicate) {
Collector<T, ?, List<T>> collector = Collectors.toList();
List<T> temp = this.stream().filter(predicate).collect(collector);
this.clear();
this.addAll(temp);
return this;
}
}
// Extended custom list with predefined type
public class ChildList extends ParentList<ChildObject> {
// other methods
}
// implementation
public static void main(String[] args) {
ChildObject a = new ChildObject("Alice");
ChildObject b = new ChildObject("Bob");
ChildList filterList = new ChildList();
filterList.add(a);
filterList.add(b);
filterList.filter(c -> childObject.getName().equals("Alice"));
}
It would be really nice if this is possible but feel free to chime in whether this is doable, practical, or have any thoughts on performance issues or bad practices.