2

Suppose I have a class Employee :

class Employee {
         Employee(String name, int age)
         {
             this.name = name ;
             this.age = age;
         }
         String name ;
         int age;
     }

Now Create a List of this like :

ArrayList<Employee> aa = new ArrayList<Employee>();
 aa.add(new Employee("Nitish", 26));
 aa.add(new Employee("Sh", 2));
 aa.add(new Employee("S", 1));

Can i get Employee Object Where name value is "Nitish"? Without For loop

6
  • No you can't without looping. In case you want instant access, you may use Map and store name as key and Employee as value. Commented Nov 24, 2018 at 8:58
  • This was interview question Commented Nov 24, 2018 at 9:03
  • 2
    Generally an interviewer would want to see how quick and confident you are while answering and he should like if you try and suggest an alternate solution. So, the plan answer would be NO, And you could have suggested the usage of Map in case one needs to find without loop. Commented Nov 24, 2018 at 9:06
  • Use a Map for o(1) lookups Commented Nov 24, 2018 at 9:23
  • Or you can use a stream or method which performs the loop for you but that isn't changing how it behaves. Commented Nov 24, 2018 at 9:46

4 Answers 4

1

Try this

Employee emp = aa.stream().filter(e->e.name.equals("Nitish")).findAny().orElse(null);

this version returns null if not found

Sign up to request clarification or add additional context in comments.

Comments

1

I guess your interviewer just doesn't want you to use for or while loops to find objects in an ArrayList, but you can actually find them "without loops".

First, you need to override equals and hashCode of Employee class:

@Override public boolean equals(Object obj) {
    // ...
}

@Override public int hashCode() {
    // ...
}

Now you can find your object with ArrayList.indexOf (uses equals to find match) by creating a dummy reference:

Employee target = new Employee("Nitish", 26);
int index = employees.indexOf(target);

It's kinda silly but I guess some interviewers want us to think outside-of-the-box. Even though under the hood it's using loops, but if my interviewer asks me the same question, instead of saying no you can't, I'd use this example because I want to try my best just not to use "loops" as asked, and explains how it works behind the scene. Then afterwards I'd briefly come up with other better solutions, and hope that works!

2 Comments

This uses a loop under the covers.
@PeterLawrey Yes I know, but If my interviewer asks me the same question, instead of saying no you can't, I'd use this answer because I do my best not to use "loops" as asked, and explains how it works under the hood. Then afterwards I'd briefly come up with other better solutions.
0

You can use a filtered stream:

Object[] array = aa
        .stream()
        .parallel()
        .filter((emp) -> emp.name.equals("Nitish"))
        .toArray();

if (array.length > 0) {
    Employee employee = (Employee)array[0];
    String name = employee.name;
    int age = employee.age;
    System.out.println("Found Employee with Name = " + name + " and Age = " + age);
} else {
    System.out.println("Not Found");
}

2 Comments

This uses multiple loops under the covers
under the covers is the key word.
0

ArrayList work on index based structure.to ADD,READ,GET value from array need to index of the specific index. instead store data on ArrayList you can use Map. Using key can be access value from Map.

 Map<String, Employee> employeeMap = new HashMap<>();

    employeeMap.put("Nitish", new Employee("Nitish", 26));
    employeeMap.put("Sh", new Employee("Sh", 2));
    employeeMap.put("S", new Employee("S", 1));

    System.out.println(employeeMap.get("Nitish").getName());
    System.out.println(employeeMap.get("Nitish").getAge());

Comments

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.