/**
* Return the index of the first item in someCollection for which * aPredicate.test(o) is true, or -1.
*/
public static <T> int find(Collection<T> someCollection, Predicate<T> aPredicate) {
List<T> list = new ArrayList<>();
for (Iterator<T> iterator = someCollection.iterator(); iterator.hasNext(); ) {
T value = iterator.next();
if (aPredicate.test(value)) {
list.add(value);
}
}
return list[0]; // or return list.get(0)
}
With the code above, I cannot use list[0] since it needs to be replaced with list.get(0), but this method is only applicable to the collection of Integers. How can I return the index of the first element in such case?