First using stream we will Iterate through hospitals list,

find out the hospital from the list whose name equals currentHospital name, take out the first result using findFirst.

Since the result may be or may not be available it returns Optional. So on Optional, we will say if it's (the hospital object which we were looking for) present we will get the hospital out of it using the get method

Optional<Hospital> result = hospitals.stream().filter(h -> h.nameHospital.equals(currentHospital)).findFirst();

if(result.isPresent()){
    Hospital hospital = result.get();
}
Answer from navnath on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_arraylist_indexof.asp
Java ArrayList indexOf() Method
public int indexOf(Object item) Java Arrays Tutorial · Java ArrayList Tutorial · ❮ ArrayList Methods · ★ +1 · Sign in to track progress · REMOVE ADS · PLUS · SPACES · GET CERTIFIED · FOR TEACHERS · BOOTCAMPS · CONTACT US · × · If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com ·
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › ArrayList.html
ArrayList (Java Platform SE 8 )
April 21, 2026 - Java™ Platform Standard Ed. 8 ... public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable
Discussions

java - Using indexOf() method for ArrayList of objects - Stack Overflow
In my program, I have a Vendor class and a Hospital class and store instances of these classes in arraylists called vendors and hospitals. My Vendor class has a string array with names of hospitals... More on stackoverflow.com
🌐 stackoverflow.com
A better way to find the index of an element in an array?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
23
3
August 27, 2022
Arraylist indexof
Develop REST APIs, PL/SQL, MLE JavaScript, JSON, run SQL and scripts, load data, and more! More on forums.oracle.com
🌐 forums.oracle.com
Java ArrayList IndexOf - Finding Object Index - Stack Overflow
ArrayList holder = new ArrayList; Data one = new Data(0,0); Data two = new Data(0,4); Data three = new Data(0,5); ... Is indexOf any better than going through the whole array list myself? Or am I missing something. ... The problem is you are not overriding equals method but overloading instead. ... The indexOf() method does go through the entire list. Here's an excerpt from Java ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-util-arraylist-indexof-java
Java Arraylist indexOf() Method - GeeksforGeeks
December 10, 2024 - Example 1: Here, we will use the indexOf() method to find the index of a specific element in an ArrayList. ... // Java program to demonstrate the // use of indexOf() method import java.util.ArrayList; public class GFG { public static void main(String[] args) { // Creating an ArrayList of Integers ArrayList<Integer> arr = new ArrayList<Integer>(5); // Adding elements to the ArrayList arr.add(1); arr.add(2); arr.add(3); // Printing the initial values // in the ArrayList System.out.print(""); for (Integer v : arr) { System.out.print(v + " "); } // Using indexOf() to // find the index of 3 int p = arr.indexOf(3); // Prints the index of the element System.out.println("\nThe element 3 is at index: " + p); } }
🌐
Codecademy
codecademy.com › docs › java › arraylist › .indexof()
Java | ArrayList | .indexOf() | Codecademy
February 12, 2023 - The .indexOf() method returns the index of the first occurrence of the specified element in an ArrayList. If the element is not found, -1 is returned. ... Looking for an introduction to the theory behind programming?
🌐
Medium
medium.com › @AlexanderObregon › javas-arraylist-indexof-method-explained-6bb47ce4c894
Java’s ArrayList.indexOf() Method Explained | Medium
August 19, 2024 - If the specified element is present in the list, indexOf() returns the zero-based index of its first occurrence. If the element is not found, the method returns -1. ... import java....
Find elsewhere
🌐
Coderanch
coderanch.com › t › 456342 › java › ArrayList-indexOf-method
ArrayList indexOf() method (Java in General forum at Coderanch)
How does ArrayList indexOf() method work in such cases? All the example I find on the internet are for finding the index of straight forward strings or Integer objects. Any response in this regard shall be highly appreciated. regards, Rahul · Antany Vasanth · Ranch Hand · Posts: 44 · posted 16 years ago · Number of slices to send: Optional 'thank-you' note: Send · Hi Rahul, Try overriding "equals" method from "Object" class. http://www.javaworld.com/javaworld/jw-01-1999/jw-01-object.html Regards, Antany.
🌐
Programiz
programiz.com › java-programming › library › arraylist › indexof
Java ArrayList indexOf()
returns the position of the specified element from the arraylist · Note: If the specified element doesn't exist in the list, the indexOf() method returns -1. import java.util.ArrayList; class Main { public static void main(String[] args) { // create an ArrayList ArrayList<Integer> numbers ...
🌐
PREP INSTA
prepinsta.com › home › java tutorial › java arraylist indexof() method
Java ArrayList IndexOf() Method | PrepInsta
March 30, 2026 - Java ArrayList IndexOf() Method is use to return the index of the element passed into the method of arraylist
🌐
W3Resource
w3resource.com › java-tutorial › arraylist › arraylist_indexof.php
Java indexOf Method - w3resource
August 19, 2022 - The indexOf() method is used to get the index of the first occurrence of an element in an ArrayList object. ... Return Value: The index of the first occurrence an element in ArrayList, or -1 if the element does not exist.
🌐
Codekru
codekru.com › home › arraylist indexof() method in java
ArrayList indexOf() method in Java - Codekru
August 12, 2022 - java · indexOf() method helps return the index of the first occurrence of the specified element. In this post, we are going to discuss the indexOf() method of the ArrayList class in detail. Method declaration – public int indexOf(Object o) What does it do?
🌐
Oracle
forums.oracle.com › ords › apexds › post › arraylist-indexof-3393
Arraylist indexof
Develop REST APIs, PL/SQL, MLE JavaScript, JSON, run SQL and scripts, load data, and more!
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-arraylist-indexof-method-example
Java ArrayList indexOf() Method example
Here we are using the indexOf() method to find the index of few specified elements in the ArrayList. import java.util.ArrayList; public class IndexOfExample { public static void main(String[] args) { ArrayList<String> al = new ArrayList<String>(); al.add("AB"); al.add("CD"); al.add("EF"); al.add("GH"); al.add("IJ"); al.add("KL"); al.add("MN"); System.out.println("Index of 'AB': "+al.indexOf("AB")); System.out.println("Index of 'KL': "+al.indexOf("KL")); System.out.println("Index of 'AA': "+al.indexOf("AA")); System.out.println("Index of 'EF': "+al.indexOf("EF")); } }
🌐
Vultr
docs.vultr.com › java › standard-library › java › util › ArrayList › indexOf
Java ArrayList indexOf() - Find Element Index | Vultr Docs
September 27, 2024 - Create an instance of ArrayList and add some elements to it. Use the indexOf() method to find the index of a specified element. ... import java.util.ArrayList; ArrayList<String> list = new ArrayList<>(); list.add("Apple"); list.add("Banana"); ...
🌐
W3Schools
w3schools.com › java › java_arraylist.asp
Java ArrayList
It is part of the java.util package and implements the List interface. The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (if you want to add or remove elements to/from an array, you have to create a new one).
🌐
GeeksforGeeks
geeksforgeeks.org › java › arraylist-get-method-java-examples
ArrayList get(index) Method in Java with Examples - GeeksforGeeks
December 10, 2024 - // Java program to demonstrate error generated // while using get() method in ArrayList import java.util.ArrayList; public class GFG { public static void main(String[] args) { // Creating an ArrayList of Integers ArrayList<Integer> arr = new ArrayList<Integer>(4); // Adding elements to the ArrayList arr.add(10); arr.add(20); arr.add(30); arr.add(40); // Trying to access an element // at an invalid index int e = arr.get(5); // Printing the element at // index 5 (will throw exception) System.out.println("The element at index 5 is " + e); } }
🌐
How to do in Java
howtodoinjava.com › home › collections framework › java arraylist › java arraylist.indexof()
Java Arraylist.indexOf() - Get Element Index - HowToDoInJava
January 13, 2023 - ArrayList<String> list = new ArrayList<>(Arrays.asList("alex", "brian", "charles","alex",","alex","harry")); int index = list.indexOf("alex"); System.out.println(index);
🌐
JavaGoal
javagoal.com › home › how to get index of object in arraylist java
java arraylist indexof and get index of object in arraylist java - JavaGoal
July 26, 2020 - import java.util.ArrayList; import java.util.Iterator; public class ArrayListExample { public static void main(String[] args) { ArrayList<Integer> listOfNumbers = new ArrayList<Integer>(); listOfNumbers.add(111); listOfNumbers.add(222); listOfNumbers.add(333); listOfNumbers.add(444); listOfNumbers.add(555); System.out.println("Index value of 111: "+ listOfNumbers.indexOf(111)); System.out.println("Index value of 555: "+ listOfNumbers.indexOf(555)); System.out.println("Index value of 123: "+ listOfNumbers.indexOf(123)); } }