0

How would I search for the employee "Tim"?

ArrayList <ClassName> employee = new ArrayList <ClassName>();
   employee.add(new ClassName("Name","Department",phoneNumber,"address")); 
   employee.add(new ClassName("Tim","something",9803845994,"something St"));
0

1 Answer 1

3

Option 1 : Loop through the ArrayList to find the employee

  for(ClassName emp :  enployee)
  {
      if("Tim".equals(emp.getName()))
      {
           // do something
           break;
      }
  }

Option 2 : Store the employees in some fast searchable data structure like a HashMap

  Map<String, ClassName> employeeMap = new HashMap<>();
  employeeMap.add("Tim",  new ClassName("Tim","something",9803845994,"something St"));
  employeeMap.add("Jack",  new ClassName("Jack","something",22222222,"something St"));

No searching is very simple

  Employee emp = employeeMap.get("Tim");
Sign up to request clarification or add additional context in comments.

1 Comment

Just wanted to point out Option 2 only works if the employee name is distinct, as in adding another Tim will overwrite the other one

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.