Since you are storing the Employee
objects and not String
in your list , i think it is impossible to search without looping through all list objects
for (Employee employee : empList) {
if (employee.getName().equals(searchString))
System.out.println("Found");
}
Note: Your Employee class should give access to name field either through getter method or change it to public
There are other alternatives, but it depends on your requirements and tradeoff's between speed, space, readability, resources etc
One thing i can think of is HashMap
, which has constant time lookup in average case
HashMap<Integer, String> hm = new HashMap<Integer, String>();
hm.put(1, "Tom");
System.out.println(hm.containsValue("Tom"));
Now,
Should I put it in Map?? Which one is more efficient??
Instead of coding and analyzing, Know Thy Complexities beforehand !