Thank you! As I'm reading my textbook (Data Structure in Java) for class it made mention of the Iterable interface (I knew nothing about) and I needed some knowledge. Very helpful video!
Funny - only my hands can remember what the keys are... gotta open IDEA to check :-D . The shortcut is CTRL + ALT + v : Extract Variable (a refactoring) You can also extract constants, fields and parameters with CTRL + ALT + c : Extract constant CTRL + ALT + f : Extract field CTRL + ALT + p: Extract parameter
The performance trick you show not only works "only for Lists" but really "only for ArrayLists" I think -- if you were actually using a LinkedList the indexing code would work but be a disaster, right?
Question - What is the real life use-case to use Iterable? I feel it's a bit tough to understand Iterable as compared to Iterator. Is it that if you have to build your custom set of objects (i.e. for example an arraylist of class 'animals') and use iterator on that arraylist - then we need this concept of Iterable implementation?
Iterable is mostly used to implement classes who's nested elements can be iterated via the built-in Java for-each loop. For instance, if you implement a CustomerList or a ServerSet or Cars class, where the objects of that class can contain nested elements. If you want to be able to iterate those elements via the for-each loop, have that class implement the Iterable interface. In other words - the Iterable interface is a way for Java classes to "hook in" to the built-in for-each loop - to be usable with the for-each loop.
Hello Jakob. In the performance section, you explain that in each iteration of the loop an Interator object is created in order to "iterate" through the collection. Is that so, since, I think that only 1 Iterator object is created and IT loops through the collection. Creating a new object on each iteration sounds quite inefficient.
The Java compiler can see that the 'elements' variable references an object from a class that implements the Iterable interface. Then - behind the scenes - it calls elements.iterator() and uses that to iterate through the collection.
A Java Iteratable is an interface which objects containing elements (e.g. Collection, List, Set, Queue etc.) can implement, so its elements can be iterated via the Java for-each loop. The video also explains that. Most of my viewers prefer to see the code as soon as possible - that's why I jump into the code quite quickly - to show you *how* the Java Iterable works. Furthermore, the Java Iterable interface is not an abstract concept. It's a code concept. A simple interface. That's why I don't show any diagrams - but just code. I also explain how you can implement the Java Iterable interface in your own classes - so they can be used with the Java for-each loop too.
@@JakobJenkov It was "Ret-Conned in" as the parent Interface to Collection after the fact when they added enhanced for/foreach in Java 5. It is not limited to the Collection types tho, but anything that can be looped thru with enhanced for, right?
Thank you! As I'm reading my textbook (Data Structure in Java) for class it made mention of the Iterable interface (I knew nothing about) and I needed some knowledge. Very helpful video!
Great ! :-)
Amazing class, Jakob! Thank you!
You are welcome! :-)
Amazing.
Thank you, Jakob!
You are welcome! :-)
what key combination u press on 9:20???
Funny - only my hands can remember what the keys are... gotta open IDEA to check :-D . The shortcut is
CTRL + ALT + v : Extract Variable (a refactoring)
You can also extract constants, fields and parameters with
CTRL + ALT + c : Extract constant
CTRL + ALT + f : Extract field
CTRL + ALT + p: Extract parameter
@@JakobJenkov thanks for your time
u the best!
Fantastic video!
Thank you :-)
The performance trick you show not only works "only for Lists" but really "only for ArrayLists" I think -- if you were actually using a LinkedList the indexing code would work but be a disaster, right?
True - indexing into a LinkedList is slow! It's only fast in an ArrayList. I am sorry if they video does not explain that clearly enough!
Question - What is the real life use-case to use Iterable? I feel it's a bit tough to understand Iterable as compared to Iterator. Is it that if you have to build your custom set of objects (i.e. for example an arraylist of class 'animals') and use iterator on that arraylist - then we need this concept of Iterable implementation?
Iterable is mostly used to implement classes who's nested elements can be iterated via the built-in Java for-each loop. For instance, if you implement a CustomerList or a ServerSet or Cars class, where the objects of that class can contain nested elements. If you want to be able to iterate those elements via the for-each loop, have that class implement the Iterable interface. In other words - the Iterable interface is a way for Java classes to "hook in" to the built-in for-each loop - to be usable with the for-each loop.
@@JakobJenkov Ok thanks very much for your reply.
This is clear now. Karma++ to you. :)
Exactly what i was looking for :D Thanks alot!
Great! :-)
What is the difference between using iterator.stream().foreach and iterator.foreach? actually Im using java 11
I am not sure, actually. I guess once you call iterator.stream() you have all the Java Stream API methods available, and not just foreach() ...
Hello Jakob. In the performance section, you explain that in each iteration of the loop an Interator object is created in order to "iterate" through the collection. Is that so, since, I think that only 1 Iterator object is created and IT loops through the collection. Creating a new object on each iteration sounds quite inefficient.
In Java, an Iterator does not "loop through the collection". It can only return the next element in the collection for you.
Thanks for the video, is there a difference b/w Collection collection = new HashSet(); & Set list = new HashSet(); Collection collection = list; ?
No. The first version of the code is just shorter.
good video
Thank you 😊
How does your forEach know to iterate through your 'elements' member? does forEach call .iterable() under the hood?
The Java compiler can see that the 'elements' variable references an object from a class that implements the Iterable interface. Then - behind the scenes - it calls elements.iterator() and uses that to iterate through the collection.
Great :)
Thanks :-)
u just go to the code right ahead, i still dontk now what Iterable or iterator is in java
A Java Iteratable is an interface which objects containing elements (e.g. Collection, List, Set, Queue etc.) can implement, so its elements can be iterated via the Java for-each loop. The video also explains that. Most of my viewers prefer to see the code as soon as possible - that's why I jump into the code quite quickly - to show you *how* the Java Iterable works. Furthermore, the Java Iterable interface is not an abstract concept. It's a code concept. A simple interface. That's why I don't show any diagrams - but just code. I also explain how you can implement the Java Iterable interface in your own classes - so they can be used with the Java for-each loop too.
@@JakobJenkov It was "Ret-Conned in" as the parent Interface to Collection after the fact when they added enhanced for/foreach in Java 5. It is not limited to the Collection types tho, but anything that can be looped thru with enhanced for, right?
Java sucks sometimes really hard!