Can you write a foreach loop like this?

Discussion in 'Java' started by Fencer, Dec 1, 2009.

  1. Fencer

    Fencer Guest

    Say I have a List<SomeClass> and now I want to loop through the list but
    I am only interested in the String representation of the SomeClass objects.

    Do I have to do it like this:

    for (SomeClass inst : aListOfSomeClass) {
    String str = inst.toString();
    // Do something with str
    }

    ?

    I don't really care about each instance objecct here, just the string
    represenation of each instance, so is it possible to do something like
    the following non-valid code (and here I imagine toString() being called
    on each element):

    for (String str : aListOfSomeClass.toString()) {
    // Do something with str
    }

    Hope I made sense, thanks!

    - Fencer
    Fencer, Dec 1, 2009
    #1
    1. Advertising

  2. Fencer

    Eric Sosman Guest

    Fencer wrote:
    > Say I have a List<SomeClass> and now I want to loop through the list but
    > I am only interested in the String representation of the SomeClass objects.
    >
    > Do I have to do it like this:
    >
    > for (SomeClass inst : aListOfSomeClass) {
    > String str = inst.toString();
    > // Do something with str
    > }
    >
    > ?


    Yes. (Well, "No" if SomeClass *is* String, but ...)

    The same would hold if you wanted something else derived
    from the objects in the List, as opposed to the objects
    themselves. The `for' will deliver the objects; you've got
    to perform the desired derivation -- toString, hashCode,
    getName, whatever -- on each object as you get it.

    > I don't really care about each instance objecct here, just the string
    > represenation of each instance, so is it possible to do something like
    > the following non-valid code (and here I imagine toString() being called
    > on each element):
    >
    > for (String str : aListOfSomeClass.toString()) {
    > // Do something with str
    > }


    No. There's no "mapcar" operation in Java.

    --
    Eric Sosman
    lid
    Eric Sosman, Dec 1, 2009
    #2
    1. Advertising

  3. In article <>,
    Fencer <> wrote:

    > Say I have a List<SomeClass> and now I want to loop through the list but
    > I am only interested in the String representation of the SomeClass objects.
    >
    > Do I have to do it like this:
    >
    > for (SomeClass inst : aListOfSomeClass) {
    > String str = inst.toString();
    > // Do something with str
    > }
    >
    > ?
    >
    > I don't really care about each instance objecct here, just the string
    > represenation of each instance, so is it possible to do something like
    > the following non-valid code (and here I imagine toString() being called
    > on each element):
    >
    > for (String str : aListOfSomeClass.toString()) {
    > // Do something with str
    > }
    >
    > Hope I made sense, thanks!
    >
    > - Fencer


    Java always has a hard way :)


    public class StringItr implements Iterable<String>
    {
    final Iterable m_target;
    public StringItr (Iterable i)
    {
    m_target= i;
    }
    public Iterator<String> iterator()
    {
    return new Iterator<String> ()
    {
    final Iterator i= m_target.iterator();
    public boolean hasNext()
    {
    return i.hasNext();
    }

    public String next()
    {
    return String.valueOf(i.next());
    }

    public void remove()
    {
    i.remove();
    }
    };
    }
    }


    ....

    for (String s : new StringItr(aListOfSomeClass))
    ....
    --
    I won't see Goolge Groups replies because I must filter them as spam
    Kevin McMurtrie, Dec 1, 2009
    #3
  4. Fencer

    Daniel Pitts Guest

    Kevin McMurtrie wrote:
    > In article <>,
    > Fencer <> wrote:
    >
    >> Say I have a List<SomeClass> and now I want to loop through the list but
    >> I am only interested in the String representation of the SomeClass objects.
    >>
    >> Do I have to do it like this:
    >>
    >> for (SomeClass inst : aListOfSomeClass) {
    >> String str = inst.toString();
    >> // Do something with str
    >> }
    >>
    >> ?
    >>
    >> I don't really care about each instance objecct here, just the string
    >> represenation of each instance, so is it possible to do something like
    >> the following non-valid code (and here I imagine toString() being called
    >> on each element):
    >>
    >> for (String str : aListOfSomeClass.toString()) {
    >> // Do something with str
    >> }
    >>
    >> Hope I made sense, thanks!
    >>
    >> - Fencer

    >
    > Java always has a hard way :)
    >
    >
    > public class StringItr implements Iterable<String>
    > {
    > final Iterable m_target;

    This should be an Iterable<?>
    > public StringItr (Iterable i)

    Same for this Iterable
    > {
    > m_target= i;
    > }
    > public Iterator<String> iterator()
    > {
    > return new Iterator<String> ()
    > {
    > final Iterator i= m_target.iterator();

    make this an Iterator<?>
    > public boolean hasNext()
    > {
    > return i.hasNext();
    > }
    >
    > public String next()
    > {
    > return String.valueOf(i.next());
    > }
    >
    > public void remove()
    > {
    > i.remove();
    > }
    > };
    > }
    > }



    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Dec 1, 2009
    #4
  5. In article <vdfRm.35142$>,
    Daniel Pitts <> wrote:

    > Kevin McMurtrie wrote:
    > > In article <>,
    > > Fencer <> wrote:
    > >
    > >> Say I have a List<SomeClass> and now I want to loop through the list but
    > >> I am only interested in the String representation of the SomeClass objects.
    > >>
    > >> Do I have to do it like this:
    > >>
    > >> for (SomeClass inst : aListOfSomeClass) {
    > >> String str = inst.toString();
    > >> // Do something with str
    > >> }
    > >>
    > >> ?
    > >>
    > >> I don't really care about each instance objecct here, just the string
    > >> represenation of each instance, so is it possible to do something like
    > >> the following non-valid code (and here I imagine toString() being called
    > >> on each element):
    > >>
    > >> for (String str : aListOfSomeClass.toString()) {
    > >> // Do something with str
    > >> }
    > >>
    > >> Hope I made sense, thanks!
    > >>
    > >> - Fencer

    > >
    > > Java always has a hard way :)
    > >
    > >
    > > public class StringItr implements Iterable<String>
    > > {
    > > final Iterable m_target;

    > This should be an Iterable<?>


    It should have array support, JavaDocs, and proper spacing too but it's
    just free info on Usenet. I leave it to the viewer to add the finishing
    touches.

    >

    [snip]
    --
    I won't see Goolge Groups replies because I must filter them as spam
    Kevin McMurtrie, Dec 2, 2009
    #5
  6. Fencer

    Daniel Pitts Guest

    Kevin McMurtrie wrote:
    > In article <vdfRm.35142$>,
    > Daniel Pitts <> wrote:
    >
    >> Kevin McMurtrie wrote:
    >>> In article <>,
    >>> Fencer <> wrote:
    >>>
    >>>> Say I have a List<SomeClass> and now I want to loop through the list but
    >>>> I am only interested in the String representation of the SomeClass objects.
    >>>>
    >>>> Do I have to do it like this:
    >>>>
    >>>> for (SomeClass inst : aListOfSomeClass) {
    >>>> String str = inst.toString();
    >>>> // Do something with str
    >>>> }
    >>>>
    >>>> ?
    >>>>
    >>>> I don't really care about each instance objecct here, just the string
    >>>> represenation of each instance, so is it possible to do something like
    >>>> the following non-valid code (and here I imagine toString() being called
    >>>> on each element):
    >>>>
    >>>> for (String str : aListOfSomeClass.toString()) {
    >>>> // Do something with str
    >>>> }
    >>>>
    >>>> Hope I made sense, thanks!
    >>>>
    >>>> - Fencer
    >>> Java always has a hard way :)
    >>>
    >>>
    >>> public class StringItr implements Iterable<String>
    >>> {
    >>> final Iterable m_target;

    >> This should be an Iterable<?>

    >
    > It should have array support, JavaDocs, and proper spacing too but it's
    > just free info on Usenet. I leave it to the viewer to add the finishing
    > touches.
    >
    > [snip]

    Finishing touches are one thing, broken code is another ;-).
    I would advocate a different approach, which is basically a library for
    providing an equivalent functionality to mapcar.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Dec 2, 2009
    #6
  7. Daniel Pitts wrote:
    [ SNIP ]

    > Finishing touches are one thing, broken code is another ;-).
    > I would advocate a different approach, which is basically a library for
    > providing an equivalent functionality to mapcar.
    >

    If and when Java gets lambdas I'd just make "map" a method in Collection.

    AHS
    Arved Sandstrom, Dec 2, 2009
    #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. Kevin Spencer

    Re: foreach loop error

    Kevin Spencer, Aug 22, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    360
    Kevin Spencer
    Aug 22, 2003
  2. Luc Kumps

    Re: foreach loop error

    Luc Kumps, Aug 22, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    378
    Luc Kumps
    Aug 22, 2003
  3. Richard Dixson
    Replies:
    1
    Views:
    509
    Joe Fallon
    May 18, 2004
  4. ad
    Replies:
    2
    Views:
    14,620
    Peter Rilling
    Jun 6, 2005
  5. Isaac Won
    Replies:
    9
    Views:
    372
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page