Can you write a foreach loop like this?

F

Fencer

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
 
E

Eric Sosman

Fencer said:
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.
 
K

Kevin McMurtrie

Fencer said:
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))
....
 
K

Kevin McMurtrie

Daniel Pitts said:
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]
 
D

Daniel Pitts

Kevin said:
Daniel Pitts said:
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.
 
A

Arved Sandstrom

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
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top