Can I simplify this code?

J

James Yong

Can I simplify this code?

for (Iterator iter = this.children.iterator(); iter.hasNext(); )
{
String obj = (String)iter.next();
if (obj.equals(child)) return true;
}
return false;

to

return this.children.contains(child);
 
S

Steve Brecher

James Yong said:
Can I simplify this code?

for (Iterator iter = this.children.iterator(); iter.hasNext(); )
{
String obj = (String)iter.next();
if (obj.equals(child)) return true;
}
return false;

to

return this.children.contains(child);

Looks OK to me, as long as there is in fact a "contains" method provided by
whatever class children is an instance of and it does the conventional
thing.
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

James Yong schreef:
Can I simplify this code?

for (Iterator iter = this.children.iterator(); iter.hasNext(); )
{
String obj = (String)iter.next();
if (obj.equals(child)) return true;
}
return false;

to

return this.children.contains(child);

Depends on what children is. If it is a Collection, then yes.

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFFdpofe+7xMGD3itQRArfwAJ9KySRckkR/9rWpovUn7WaMH5rVhQCdGBIH
Ue5e9wKjFaSSAY3r28Oehuc=
=sW2W
-----END PGP SIGNATURE-----
 
A

Andreas Leitgeb

James Yong said:
Can I simplify this code?
for (Iterator iter = this.children.iterator(); iter.hasNext(); )
{
String obj = (String)iter.next();
if (obj.equals(child)) return true;
}
return false;

to

return this.children.contains(child);

I'd say: "yes, but ..."

The "but"-case is, when you have not only String objects
(or even null) in your collection, then the former code
might throw an exception, whereas the latter will rather
ignore irrelevant items. So it's not strictly equivalent.

Practically, the latter is not just shorter, it's also
most likely faster (even by orders of magnitude, depending
on the type of collection actually used) and more stable.
 
T

Thomas Schodt

James said:
Can I simplify this code?

for (Iterator iter = this.children.iterator(); iter.hasNext(); )
{
String obj = (String)iter.next();
if (obj.equals(child)) return true;
}
return false;

to

return this.children.contains(child);

What is wrong with just trying it?
 
S

Stefan Ram

Thomas Schodt said:
What is wrong with just trying it?

By trying one will only find errors, which can be detected by
the compiler and the runtime tests available. It might be a
mistake that only has effects under another Java implementation
or other runtime conditions.
 
T

Tris Orendorff

(e-mail address removed)-berlin.de (Stefan Ram) burped up warm pablum in
By trying one will only find errors, which can be detected by
the compiler and the runtime tests available. It might be a
mistake that only has effects under another Java implementation
or other runtime conditions.

WTF! That is a risk is a bit far-fetched. Do you even use the collection
classes?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top