O'Reilly logo

Java™ Phrasebook by Timothy Fisher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required