Concept

How do Java collections perform a linear search?

ComputerScienceOne / Searching

"Java Set and List collections provide several linear search methods. Both have a public boolean contains(Object o) method that returns true or false depending on whether or not an object matching o is in the collection. The List collection has an additional public int indexOf(Object o) method that returns the index of the first matched element and −1 if no such element is found (Sets are unordered and have no indices, so this method would not apply). All of these functions are equality-based and they do not take a Comparator object. Instead, the elements are compared using the object’s equals() (and possibly hashCode()) method. The first element x such that x.equals(o) returns true is the element that is determined to match. For this reason, it is important to override both of these methods when designing objects (see Section 36.3.3)."

Related Ideas

How do Java collections perform a linear search? | ComputerScienceOne | Bifalgorithm | Bifalgorithm