2

Is there any difference between?

MyObject myWantedObj = Iterables.tryFind(myListOfObjects, new Predicate<MyObject>() {
    public boolean apply(MyObject myObj) {
        return myObj.getSomeAttribute().equals(someFinalVariable);
    }
}).orNull();

and

MyObject myWantedObj = FluentIterable.from(myListOfObjects).firstMatch(new Predicate<MyObject>() {
    public boolean apply(MyObject myObj) {
        return myObj.getSomeAttribute().equals(someFinalVariable);
    }
}).orNull();

Iterables.tryFind and FluentIterable.firstMatch Javadoc are equals to:

Returns an Optional containing the first element in iterable that satisfies the given predicate, if such an element exists.

I missing something?

Omar Hrynkiewicz
  • 502
  • 1
  • 8
  • 21

1 Answers1

9

Iterables.tryFind() pre-dates FluentIterable.firstMatch() by quite a bit. If you're just doing a single operation (as in your example), it doesn't really matter which you use. We probably never would have created the Iterables class if we had created FluentIterable first (hindsight is 20/20).

The power of FluentIterable comes when you're chaining several functional-type steps together. For example:

   FluentIterable
       .from(database.getClientList())
       .filter(activeInLastMonth())
       .transform(Functions.toStringFunction())
       .limit(10)
       .toList();