views:

476

answers:

8

I've created a linked list in java using generics, and now I want to be able to iterate over all the elements in the list. In C# I would use yield return inside the linked list while going over the list of elements contained in the list.

How would I go about creating a java version of the above where I can iterate over all the items contained in the linked list?

I'm looking to be able to write code ala

LinkedList<something> authors = new LinkedList<something>();
for (Iterator<something> i = authors.Values ; i.HasNext())
      doSomethingWith(i.Value);

And was thinking that the Value 'property'/method would consist of code resembling

LinkedListObject<something> current = first;
While (current != null){
 yield return current.getValue();
 current = current.getNext()
}

Edit: Notice that I'm not interested in using any 3rd party APIs. Built-in java functionality only.

+3  A: 

try this

check this article for a sample implementation as well:

Asad Butt
+1  A: 

I guess you should consider using threads to do a similar implementation in Java. I read what yield return does and to my understanding it seems to be a very dynamic. You should be able to achieve the similar functionality, but you will have to come up with your own set of class. Asad here has given few links.

But one thing I can tell is you cannot make java understand these things by itself within a for loop. You need to explicitly instruct them. Thats there for sure.

Bragboy
+1  A: 

I have tried to understand what yield does but without the C# experience i am not sure if i have it but i will try anyway...

I would suggest the following...

Something answer = null;
for (Something author: authors){

  if (author.equals("Tom Jones"){
    answer = author;
    break;
  }
}

When it comes to returning the values from a method i would do the following...

    public LinkedList<something> getAuthors(LinkedList<something> list){
      LinkedList<something> ret = new LinkedList<something>();
      for (something s:list){
        if (s.equals("abc"))
          ret.add(s);
      }
      return ret;
    }

Have i lost the plot?

Paul
where are you calling getAuthors(LinkedList)?
Bragboy
ummm... you would call getAuthors from anywhere in your code. if you create a class called Utils (as an example) and made the method static you could then say Utils.getAuthors(pass your list here); and that will return your new list.
Paul
+1  A: 

If you want the full functionality of yield return, you probably need to set this up in two threads-- one for the first method, and one for the second. Then the first thread should wait until the second thread puts its value somewhere accessible and notifys it that it's ready. Then the first thread would process that value, wait for the next value, etc.

Denise
+1  A: 

I don't understand why people are talking about threads... is there something I don't know about yield return?

To my understanding yield return just saves the method stack and restores it at a later time. To implement yield return you just have to save the state manually. See the Java iterator classes for details, though for a linked list you can just get away with saving the current item. For an array you'd just need the index.

CurtainDog
+1  A: 

Am I missing something here? There is already java.util.LinkedList, it is fully generics-enabled, and it has a method which returns an Iterator.

If you really want to re-invent the wheel, I'd suggest you look into to creating a LinkedListIterator class, probably implementing ListIterator. It would remember its current position within the linked list and advance it on each successive call.

crazyscot
The reasons for creating my own data structures are due to extensive need for performance enchancement later on in the process (might not apply as much to the linked list as to the hash table). Your solution with a sub-classed iterator was that one I eventually used to solve the problem at hand.
Qua
A: 

Threads are irrelevant here.

I also miss yield functionality a lot in java (C#, Python has it, not sure about Ruby)

Darius