How to get Object from ArrayList Without loop?

how to iterate class object in java
how to iterate list of objects in java
how to retrieve values from arraylist in java using iterator
java foreach arraylist of objects
how to retrieve object values from arraylist in java
arraylist of objects in java
adding elements to arraylist in java using for loop
how to iterate pojo class in java

Suppose I have a class Employee :

class Employee {
         Employee(String name, int age)
    = 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

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!

print Arraylist without loop, You can print one by one without using loop but the problem is because you are using object type so you cannot print objects unless you use a  Getting the Object value and add to Arraylist and then I have to check arraylist contains string or not. Below is the POJO Class, finally this will get as Object. public class OrderTypeFeature { public OrderTypeFeature (ODataEntity entity) { create (entity); } private String OrderType; private String Feature; public String getOrderType () { return OrderType; } public void setOrderType (String orderType) { OrderType = orderType; } public String getFeature () { return Feature; }

You can use a filtered stream:

Object[] array = aa
        .filter((emp) ->"Nitish"))

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

How to iterate through ArrayList of objects in Java, The Java iterate through ArrayList programs. Learn how to retrieve values from ArrayList in Java using for loop, while loop, iterator and stream api. Make sure to specify type when you declare an ArrayList. If ArrayList is declared without type, the get method returns an object of the Object class and you will have to cast it down to the appropriate type before you can use any methods of that class.

Try this

Employee emp =>"Nitish")).findAny().orElse(null);

this version returns null if not found

How to loop ArrayList in Java, In this post we are sharing how to iterate (loop) ArrayList in Java. There. arrayList.add("DotNet"); arrayList.add("Perl"); // Get the Enumeration object  For Loop 14 7 39 40 Advanced For Loop 14 7 39 40 While Loop 14 7 39 40 Iterator 14 7 39 40. In the comment section below, Govardhan asked a question: He asked, how to iterate an ArrayList using Enumeration.

ArrayList work on index based 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));


Iterating over ArrayLists in Java, Java program to iterate over an ArrayList For Loop for iterating ArrayList System.out.print(numbers.get(i) + " " ); over an arraylist using for Each loop. The ArrayList class is part of the System.Collections namespace within .NET. By creating a new object of this type you can then store objects within an ArrayList. Below you can see that you need to explicitly create an ArrayList object using the New-Object cmdlet or by casting a standard array to an ArrayList object.

Adding Elements to an ArrayList, ArrayList<String> data = new ArrayList<String>(10); E get( int index ). This method returns a reference to an object in the list, which is of type E . Here is our example program, The for statement uses size() for the upper bound of the loop​. 1. ArrayList toArray() syntax. toArray() is overloaded method and comes in two forms: public Object[] toArray(); public <T> T[] toArray(T[] a); The first method does not accept any argument and returns the array of object type. We must iterate the objects array to find the desired element and typecast to desired class type.

How to display all the elements in ArrayList in Java using loop, How do I get an ArrayList to print line by line into a file in Java? protestations you hear from Big Tech, there is a simple privacy law that makes sense without destroying the tech industry. Using a loop, it is very easy to display the elements present in the loop. How do I count the duplicate objects in ArrayList in Java? The example also shows various ways to print the ArrayList using a loop, Arrays class, and Java 8 Stream. How to print ArrayList in Java? There are several ways using which you can print ArrayList in Java as given below. 1) Using for loop. You can print ArrayList using for loop in Java just like an array.

Here Is 4 Ways To Print ArrayList Elements In Java, For-each loop; Using iterator; Using List-iterator. Here is a string ArrayList. We have created an object of the list and added name of week days  #1 normal for loop Text 1 Text 2 Text 3 #2 advance for loop Text 1 Text 2 Text 3 #3 while loop Text 1 Text 2 Text 3 #4 iterator Text 1 Text 2 Text 3 Tags : arraylist java loop mkyong

  • 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.
  • This was interview question
  • 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.
  • Use a Map for o(1) lookups
  • Or you can use a stream or method which performs the loop for you but that isn't changing how it behaves.
  • 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.
  • This uses multiple loops under the covers
  • under the covers is the key word.