Questions tagged [iterable]

An iterable is an object, such as a string or collection, that can be iterated over, yielding up its members one at a time.

1303 questions
1404
votes
24 answers

In Python, how do I determine if an object is iterable?

Is there a method like isiterable? The only solution I have found so far is to call hasattr(myObj, '__iter__') But I am not sure how fool-proof this is.
willem
  • 25,977
  • 22
  • 75
  • 115
561
votes
15 answers

What are iterator, iterable, and iteration?

What are "iterable", "iterator", and "iteration" in Python? How are they defined? See also: How to build a basic iterator?
thechrishaddad
  • 6,523
  • 7
  • 27
  • 33
509
votes
8 answers

Convert Iterable to Stream using Java 8 JDK

I have an interface which returns java.lang.Iterable. I would like to manipulate that result using the Java 8 Stream API. However Iterable can't "stream". Any idea how to use the Iterable as a Stream without converting it to List?
rayman
  • 20,786
  • 45
  • 148
  • 246
489
votes
21 answers

Easy way to convert Iterable to Collection

In my application I use 3rd party library (Spring Data for MongoDB to be exact). Methods of this library return Iterable, while the rest of my code expects Collection. Is there any utility method somewhere that will let me quickly convert one…
Ula Krukar
  • 12,549
  • 20
  • 51
  • 65
351
votes
14 answers

Java: Get first item from a collection

If I have a collection, such as Collection strs, how can I get the first item out? I could just call an Iterator, take its first next(), then throw the Iterator away. Is there a less wasteful way to do it?
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
306
votes
9 answers

Why does Stream not implement Iterable?

In Java 8 we have the class Stream, which curiously have a method Iterator iterator() So you would expect it to implement interface Iterable, which requires exactly this method, but that's not the case. When I want to iterate over a Stream…
roim
  • 4,780
  • 2
  • 27
  • 35
271
votes
3 answers

Why does Iterable not provide stream() and parallelStream() methods?

I am wondering why the Iterable interface does not provide the stream() and parallelStream() methods. Consider the following class: public class Hand implements Iterable { private final List list = new ArrayList<>(); private…
skiwi
  • 66,971
  • 31
  • 131
  • 216
254
votes
15 answers

What is the difference between iterator and iterable and how to use them?

I am new in Java and I'm really confused with iterator and iterable. Can anyone explain to me and give some examples?
Charles Cai
  • 2,731
  • 4
  • 18
  • 10
193
votes
6 answers

How do I add the contents of an iterable to a set?

What is the "one [...] obvious way" to add all items of an iterable to an existing set?
Ian Mackinnon
  • 13,381
  • 13
  • 51
  • 67
191
votes
16 answers

Why is Java's Iterator not an Iterable?

Why does the Iterator interface not extend Iterable? The iterator() method could simply return this. Is it on purpose or just an oversight of Java's designers? It would be convenient to be able to use a for-each loop with iterators like…
Łukasz Bownik
  • 6,149
  • 12
  • 42
  • 60
180
votes
5 answers

Convert ES6 Iterable to Array

Say you have an array-like Javascript ES6 Iterable that you know in advance will be finite in length, what's the best way to convert that to a Javascript Array? The reason for doing so is that many js libraries such as underscore and lodash only…
Michael Bylstra
  • 5,042
  • 4
  • 28
  • 24
176
votes
10 answers

Convert Java Array to Iterable

I have an Array of primitives, for example for int, int[] foo. It might be a small sized one, or not. int foo[] = {1,2,3,4,5,6,7,8,9,0}; What is the best way to create an Iterable from it? Iterable fooBar =…
ntg
  • 12,950
  • 7
  • 74
  • 95
174
votes
9 answers

Length of generator output

Python provides a nice method for getting length of an eager iterable, len(x) that is. But I couldn't find anything similar for lazy iterables represented by generator comprehensions and functions. Of course, it is not hard to write something…
Maxim
105
votes
10 answers

Get size of an Iterable in Java

I need to figure out the number of elements in an Iterable in Java. I know I can do this: Iterable values = ... it = values.iterator(); while (it.hasNext()) { it.next(); sum++; } I could also do something like this, because I do not need the…
js84
  • 3,676
  • 2
  • 19
  • 23
104
votes
4 answers

Kotlin's Iterable and Sequence look exactly same. Why are two types required?

Both of these interfaces define only one method public operator fun iterator(): Iterator Documentation says Sequence is meant to be lazy. But isn't Iterable lazy too (unless backed by a Collection)?
Venkata Raju
  • 4,866
  • 3
  • 27
  • 36
1
2 3
86 87