Maksim,
Immediately struck that your type is a variable class, not an interface. Best practice advises to use just the same interface, for example:
private List<Clothes> clothes = new ArrayList<>(); private List<Computer> computers = new ArrayList<>(); private List<Smartphone> smartphones = new ArrayList<>();
Regarding your question, you can use the generic methodology:
public <T> void searchName(List<T> list)
The question is not known whether the classes Clothes, Computer and Smartphone are the heirs of some other class or interface. If they were, for example:
interface Goods { int price(); int name(); int amount(); } class Clothes implements Goods { ... } class Computer implements Goods { ... }
In this form, the code would become more concise: Initialization:
private static List<Goods> clothes = new ArrayList<>(); private static List<Goods> computers = new ArrayList<>();
Filling:
clothes.add(new Clothes()); computers.add(new Computers());
The method would remain unified:
public void searchName(List<Goods> list) { for (Goods entity : list) { System.out.println(entity.price()); } }
And the method call is the same for each of the collections:
searchName(clothes); searchName(computers);
In the worst case, in the version with generics, it will be possible to determine which particular object came using the instanceof operator.
ArrayList<Product>whereProductis the parent class for all listed - Alexey Shimansky