I have usersArrayList, messagesArrayList and chatsArrayList, that are ArrayList<User>, ArrayList<Message> andArrayList<Chat> respectively. Each of these classes has an id attribute.
I tried to write a generic function to find instance of given object in given list by id like that:
public static <T> T findInListById(ArrayList<T> list, String objId) {
return list.stream().filter(listObj -> listObj.id.equals(objId)).findFirst();
}
But Java says that listObj doesn't have an id attribute. How do I write function so it works for User, Chat and Message classes?
P.S. I also tried writing a parent class IDClass for all of them, which solved the problem, but then when I tried to invoke it (ID.findInListById(usersArrayList,userId)), Java would complain:
Required Type: ArrayList<ID>
Provided: ArrayList<User>