I have an Iterable and I need to check about a specific string inside the iterable. I tried iter.contains("my string")
, but it does not work. Any idea?
-
3We need additional informations (just like this comment needs more data about what kind of data we need). – Pshemo Jun 18 '13 at 17:59
-
1Can you show some more code? What exactly is that `iter` referring to? – Rohit Jain Jun 18 '13 at 17:59
-
http://stackoverflow.com/questions/2925765/why-does-iterator-have-a-contains-method-but-iterable-does-not-in-scala-2-8 – lamilambkin Jun 18 '13 at 18:03
-
2If the class is actually derived from `Collection` I suppose you can upcast and then call `contains` on the result... – Maarten Bodewes Jun 18 '13 at 18:04
5 Answers
Iterable is an interface, it doesn't contain a method like contains because that would assume the underlying data structure could be read sequentially without corruption.
Neither are assumptions the Iterable interface makes.

- 10,953
- 2
- 31
- 48
Your only real option with a bare Iterable
is to do a bare for
loop:
for (String string : iterable) {
if (string.equals(foo)) {
return true;
}
}
return false;
...or you could call another method which does essentially the same thing, e.g. Guava's Iterables.contains(Iterable, Object)
.

- 191,574
- 25
- 345
- 413
The Interface Iterable only returns an Iterator. So it is not possible to directly obtain if a certain value is inside. Instead you have to iterate using a for-each structure e.g.
boolean found = false;
for (String s: iter) {
if (s.equals("my string")) {
found = true;
break;
}
}
Depending on the Size this may not be very efficient. But if its your only choice...it will work at least.

- 41
- 2
- 7
-
2Multiple mistakes in there, Björn, such as `string` instead of `String` and using `==` instead of `equals()` on a `String`. – Maarten Bodewes Jun 18 '13 at 18:15
-
@owlstead: You are right. Thanks for the comment! I corrected the errors. – Björn Höper Jun 18 '13 at 18:27
try with
iter.toString().toLowerCase().equals("my string")

- 1,147
- 1
- 9
- 22
-
That's not the question, I think he wants to do this without iterating through the elements... Why is a mystery, but there it is. – Maarten Bodewes Jun 18 '13 at 18:05
-
Ok. I think the problem like he tried to iterate element and compare with `iter` object directly. – NFE Jun 18 '13 at 18:08
-
-
2That is quite an ugly hack which assumes that the specific iterable implements `toString` in a "user-friendly" way. Although it is generally the case you should not rely on that. – assylias Jun 18 '13 at 18:18
-
-
-
1This is a poor solution. The toString function on the iterator iterface doesn't necessarily return the string representation of all its contents. – William Morrison Jun 18 '13 at 18:43
try to create a iterator object and there is a contains method for iterators in most programming languages
This is a very similar question that has a been answered here Why does Iterator have a contains method but Iterable does not, in Scala 2.8?

- 1
- 1

- 117
- 1
- 1
- 9