Finding an Object in a Collection

// finding an object in an ArrayList
											int index = myArrayList.indexOf(myStringObj);
											// finding an object by value in a HashMap
											myHashMap.containsValue(myStringObj);
											// finding an object by key in a HashMap
											myHashMap.containsKey(myStringObj);

The examples shown in this phrase illustrate how you can find objects in the most commonly used collections—the ArrayList, and the HashMap. Using the indexOf() method of the ArrayList, you are able to find the position in the array where a given object is located. If the object passed into the indexOf() method is not found, a value of -1 is returned. A HashMap indexes items by objects instead of by integer values as an ArrayList does. You can use the containsValue() or

Get Java™ Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.