Can I simplify this code?

Discussion in 'Java' started by James Yong, Dec 6, 2006.

  1. James Yong

    James Yong Guest

    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);
     
    James Yong, Dec 6, 2006
    #1
    1. Advertising

  2. James Yong <james$@i$.com> wrote:
    > 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.

    --
    For mail, please use my surname where indicated:
    (Steve Brecher)
     
    Steve Brecher, Dec 6, 2006
    #2
    1. Advertising

  3. -----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-----
     
    Hendrik Maryns, Dec 6, 2006
    #3
  4. James Yong <james$@i$.com> wrote:
    > 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.
     
    Andreas Leitgeb, Dec 6, 2006
    #4
  5. James Yong wrote:
    > 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?
     
    Thomas Schodt, Dec 6, 2006
    #5
  6. James Yong

    Stefan Ram Guest

    Thomas Schodt <> writes:
    >> return this.children.contains(child);

    >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.
     
    Stefan Ram, Dec 6, 2006
    #6
  7. -berlin.de (Stefan Ram) burped up warm pablum in
    news:-berlin.de:

    > Thomas Schodt <> writes:
    >>> return this.children.contains(child);

    >>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.


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

    --
    Tris Orendorff
    [Q: What kind of modem did Jimi Hendrix use?
    A: A purple Hayes.]
     
    Tris Orendorff, Dec 15, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ben
    Replies:
    8
    Views:
    405
  2. Replies:
    6
    Views:
    294
    Bruno Desthuilliers
    Aug 27, 2006
  3. Patrick

    can I simplify this?

    Patrick, Jun 8, 2007, in forum: XML
    Replies:
    6
    Views:
    424
    Patrick
    Jun 11, 2007
  4. John Bokma
    Replies:
    0
    Views:
    92
    John Bokma
    Mar 30, 2014
  5. Tim McDaniel
    Replies:
    0
    Views:
    179
    Tim McDaniel
    Mar 31, 2014
Loading...

Share This Page