Newbie: What is "cond" in the following code?  
Author Message
Zalek Bloom





PostPosted: 2004-1-11 4:19:00 Top

java-programmer, Newbie: What is "cond" in the following code? I am reading Java trail from Sun on site:
http://java.sun.com/docs/books/tutorial/collections/interfaces/collection.html

There is an example of the following code:

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

My question: what is "cond" from the above code?

Thanks,

Zalek
 
Neil Campbell





PostPosted: 2004-1-11 4:23:00 Top

java-programmer >> Newbie: What is "cond" in the following code? Zalek Bloom wrote:

> I am reading Java trail from Sun on site:
>
http://java.sun.com/docs/books/tutorial/collections/interfaces/collection.html
>
> There is an example of the following code:
>
> static void filter(Collection c) {
> for (Iterator i = c.iterator(); i.hasNext(); )
> if (!cond(i.next()))
> i.remove();
> }
>
> My question: what is "cond" from the above code?
>
> Thanks,
>
> Zalek

It's not very well explained, but this is from the same page:

"The following snippet shows you how to use an Iterator to filter a
Collection, that is, to traverse the collection, removing every element
that does not satisfy some condition:"

I think that cond() is a method which determines whether the item satisfies
the 'some condition'.

--
Neil Campbell
batneil[AT]thebatcave[DOT]org[DOT]uk
http://www.thebatcave.org.uk
 
Tony Morris





PostPosted: 2004-1-11 8:31:00 Top

java-programmer >> Newbie: What is "cond" in the following code? "The following snippet shows you how to use an Iterator to filter a
Collection, that is, to traverse the collection, removing every element that
does not satisfy some condition: "

'cond' in this case, is some arbitrary condition that you create.
An example might be if you want to remove, from a Collection of Integer
instances, all values less than 100 (and thus, keep all instances with
values greater than or equal to 100).

/**
* A condition for removing certain elements from a <tt>Collection</tt>.
*
* @return <code>true</code> if the given Integer value is greater than or
equal to 100, <code>false</code> otherwise.
* @throws IllegalArgumentException if the <tt>Object</tt> argument is not
an instance of Integer.
*/
static boolean cond(Object o)
{
if(!(o instanceof Integer))
{
throw new IllegalArgumentException("!(o instanceof " + Integer.class
+ ')');
}

return (((Integer)o).intValue() >= 100);
}

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software

"Zalek Bloom" <email***@***.com> wrote in message
news:email***@***.com...
> I am reading Java trail from Sun on site:
>
http://java.sun.com/docs/books/tutorial/collections/interfaces/collection.html
>
> There is an example of the following code:
>
> static void filter(Collection c) {
> for (Iterator i = c.iterator(); i.hasNext(); )
> if (!cond(i.next()))
> i.remove();
> }
>
> My question: what is "cond" from the above code?
>
> Thanks,
>
> Zalek


 
 
Walter Mitty





PostPosted: 2004-1-12 22:02:00 Top

java-programmer >> Newbie: What is "cond" in the following code? "Tony Morris" <email***@***.com> brightened my day with his
incisive wit when in news:40009e49$0$26117$email***@***.com
he conjectured that:

> */
> static boolean cond(Object o)
> {
> if(!(o instanceof Integer))
> {
> throw new IllegalArgumentException("!(o instanceof " +
> Integer.class
> + ')');
> }
>
> return (((Integer)o).intValue() >= 100);
> }
>


Out of curiosity (java newbie) why don't you just make the parameter
"Integer" : why check yourself?
 
 
Debian User





PostPosted: 2004-1-17 5:06:00 Top

java-programmer >> Newbie: What is "cond" in the following code? Walter Mitty <email***@***.com> wrote:
> "Tony Morris" <email***@***.com> brightened my day with his
> incisive wit when in news:40009e49$0$26117$email***@***.com
> he conjectured that:
>
>> */
>> static boolean cond(Object o)
>> {
>> if(!(o instanceof Integer))
>> {
>> throw new IllegalArgumentException("!(o instanceof " +
>> Integer.class
>> + ')');
>> }
>>
>> return (((Integer)o).intValue() >= 100);
>> }
>>
>
>
> Out of curiosity (java newbie) why don't you just make the parameter
> "Integer" : why check yourself?

Because a collection contains Object's. So you either check it in the
condition method (as shown by Tony), or you cast it before calling the
method.

Which option you choose depends on whether you know the class (because you
only fill the collection with Integer's for example) and whether you need
an object of a specific class for the rest of the loop.


Oscar

--
No trees were harmed in creating this message.
However, a large number of electrons were terribly inconvenienced.