• Post Reply Bookmark Topic Watch Topic
  • New Topic

please explain the statement f (!cond(it.next())) from the mentioned piece of code.  RSS feed

 
gauravkv gupta
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

On the tutorial provided by Sun, following example is used. I am not getting the meaning of "f (!cond(it.next()))" in the piece of code.

static void filter(Collection<?> c) {
for (Iterator<?> it = c.iterator(); it.hasNext(); )
if (!cond(it.next()))
it.remove();
}

Please explain the "cond". What is meant by this?

link: http://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html

 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gauravkv gupta wrote:
Please explain the "cond". What is meant by this?


Assume it as a function which return false whenever there is a need to remove a specific element of a Collection otherwise true.

The following method shows you how to use an Iterator to filter an arbitrary Collection — that is, traverse the collection removing specific elements.

static void filter(Collection<?> c) {
for (Iterator<?> it = c.iterator(); it.hasNext(); )
if (!cond(it.next()))
it.remove();
}
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . or true whenever you should leave that element in the list
The cond() method ought to be somewhere in your class, or a static import.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cond() would be a user-defined method which would return a boolean to indicate if the given element should remain in the collection.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!