• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Iterable / Iterator

 
Ranch Hand
Posts: 32
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having trouble undestanding the point of Iterable interface.

whats the purpose of Iterable?


well since it haves a method that returns an iterator, if I implement Iterable it will make me create an Iterator right?

what is really the iterator real use?

well if someoene can help me, I'd really appreciate that,


thank you Jaime
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jaime Caetano wrote:I'm having trouble undestanding the point of Iterable interface.

whats the purpose of Iterable?


well since it haves a method that returns an iterator, if I implement Iterable it will make me create an Iterator right?

what is really the iterator real use?



Interfaces is how stuff is done in Java. For example, it uses Runnable objects, and not just objects with the run() method.

In this case, it uses Iterable objects, and not just objects with the iterator() method. And there are a few places where this is useful. The one specified in the JavaDoc is for the "foreach" loop.

Henry
 
Jaime Caetano
Ranch Hand
Posts: 32
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you tell me a little more of how the Iterable relates to the for each loop?

 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jaime Caetano wrote:can you tell me a little more of how the Iterable relates to the for each loop?



I am assuming that you used the enhanced for loop (with a collection) before ... if so, how do you think it works? The loop doesn't have a list of collections that are valid. It just checks if it is Iterable, and if so, get the iterator to loop through.

Henry
 
Jaime Caetano
Ranch Hand
Posts: 32
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes I did use it before, so in order to for each loop go on, it first checks if the variable is from an iterable collection, is that right?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jaime Caetano wrote:yes I did use it before, so in order to for each loop go on, it first checks if the variable is from an iterable collection, is that right?



Correct. One option is that it must be an Iterable object. The other option is that it is an array object.

Henry
 
Jaime Caetano
Ranch Hand
Posts: 32
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
amazing, im getting into it, but look

I'm trying all this and so I made a class Test and now:
I can't really understand what is the Iterator() for... cause I made it return null, and the main output its the same as having returning an iterator...
what could be a possibly explanation for this?




thank you,

 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jaime Caetano wrote:I'm trying all this and so I made a class Test and now:
I can't really understand what is the Iterator() for... cause I made it return null, and the main output its the same as having returning an iterator...


It's the second variable in the for statement that has to be Iterable. In your code that's the ArrayList that al2 points to.
To test your Test2 class you'd need something like
 
Jaime Caetano
Ranch Hand
Posts: 32
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Joanne,


I understood, t2 is iterable and it is a object,

but is there any point in your example?
what happens if you loop through a single object?

ermmmm....
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jaime Caetano wrote:
I understood, t2 is iterable and it is a object,

but is there any point in your example?
what happens if you loop through a single object?

ermmmm....



An instance of an ArrayList is a single object. An instance of a linked list is a single object. An array is a single object.

Iterators are not for those single objects. Iterators are for the items that can iterated through that is managed by those single objects.


So, yes. If your Test2 class doesn't manage anything that needs to be iterated through, there is really is no purpose of making it iterable.

Henry
 
Jaime Caetano
Ranch Hand
Posts: 32
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so, implementing Iterable<> is something very specific right?

cause right now I cant think of something that may need to be Iterable

and it's not already Iterable...
 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you apply the word Iterable to an object in Java you should get the sense that this Object contains a collection of things... Its like having a list of items and you go through each item on the list from the first to the last...

Here is an illustration using code snippets to actually see the abstract concept:




Carefully following through the logic in the code snippets you should now comprehend what the Iterable interface is all about...
 
Jaime Caetano
Ranch Hand
Posts: 32
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you a lot for the good example Rico,

so imagine you had a Collection there instead of an array,

to make your shoppingList Iterable, you could simply

make an Iterator<String> it = list.iterator();


well java could solve this by turning all data structures Iterable already right??
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jaime Caetano wrote:thank you a lot for the good example Rico,
so imagine you had a Collection there instead of an array,
to make your shoppingList Iterable, you could simply
make an Iterator<String> it = list.iterator();


Yes.

well java could solve this by turning all data structures Iterable already right??


And in most cases it has. Specifically: every subclass of Collection (and that includes every subclass of List and Set) IS an Iterable.

Also: arrays behave like Iterables for the purposes of a foreach loop.

Winston
 
reply
    Bookmark Topic Watch Topic
  • New Topic