Q: Passing a condition as a parameter to an iterator I'm almost finished with my project, although there is one thing that I can't get past.
Let me quote it first: 

The class Board must also oer a method that returns an iterator that will
  return all the elements on the board (not including items carried by robots) that
  satisfy a given condition. Examples of conditions are all elements that have an
  energy of at least 1000 Ws (the iterator will then not return walls nor surprise
  boxes, because they have no known energy), all elements in some sub range of
  the board, all items on the board, etc.

So, I have a board with elements on. (Robots, certain items, walls, ...)
In class we have seen how to implement Iterable, and override the method iterator() if needed, but now I need to pass conditions as a parameters to this iterator?
My best shot was this method in class Board:
    public Iterator<Element> getAllElementsByCondition(boolean condition) {
    HashSet<Element> result = new HashSet<Element>();
    for (Element element : elements)
        if (Board.this.hasElement(element) && condition)
            result.add(element);
    return result.iterator();
}

However, as you see, I have no idea how to pass the condition as a parameter to the method.
I also don't really know if this is how I create the iterator.
EDIT: 
I'm not allowed to use any external libraries

A: Consider using Guava's collections augmentations.  In this case, specifically Collections2.filter() or Collections2.transform().  If you do this, you can supply a Predicate describing the condition you care about, and just use filter() to run through the collection and return a collection of matching elements.  Here's a very localized example:
public Iterator<Element> getAllElementsByCondition(Predicate<Element> condition) {
  return Collections2.filter(elements, condition).iterator(); 
}

But not that you may change the structure more drastically if you embrace the Guava way of doing things.

A: Assuming that you are not allowed to use an external library like Guava, you could:


*

*create a Filter<T> interface, that has one method, let's call it boolean match(T obj).

*pass a Filter<Element> to your iterator, that implements the match method to test for the condition, for example return "The name I'm looking for".equals(element.getName());.

*test in your loop if filter.match(element) then result.add(element).


EDIT
Your new method would then be:
public Iterator<Element> getAllElementsByCondition(Filter<Element> filter) {

And you would call it this way:
Iterator<Element> it = getAllElementsByCondition(new Filter<Element> () {
    public boolean match(element candidate) {
        return /* Enter your condition here */;
    }
});

the condition could be for example:
candidate.getEnergy() > 1000


A: I think that you should clarify the task. I imagine that your method should work, though it should be narrowed to something like getAllElementsWithEnergyGreaterThan(int minEnergy). And in your loop you compare your energy with minEnergy.
