Contains a method for Iterable and Iterator?

Is there an easy way to check if an element is contained in an iterable or iterator similar to the Collection.contains (Object o) method?

those. instead of writing:

Iterable<String> data = getData(); for (final String name : data) { if (name.equals(myName)) { return true; } } 

I would like to write:

 Iterable<String> data = getData(); if (Collections.contains(data, myName)) { return true; } 

I am really surprised that there is no such thing.

+7
java iterator collections iterable
source share
5 answers

In Java 8, you can turn Iterable into Stream and use anyMatch on it:

 String myName = ... ; Iterable<String> data = getData(); return StreamSupport.stream(data.spliterator(), false) .anyMatch(name -> myName.equals(name)); 

or using the method link,

 return StreamSupport.stream(data.spliterator(), false) .anyMatch(myName::equals); 
+6
source share

Guava has Iterables.contains and Iterators.contains functions that do what you want. You can look at the source to find out how to implement them yourself.

+3
source share

An iterator is a kind of cursor that can be moved around the elements of any set of elements. Thus, its internal state is mainly a pointer to the current element. If you try to find out if it contains a β€œspecific item”, you will have to move the cursor and therefore change the internal state. Changing a state simply by asking a question is definitely bad.

This is even a problem with the mentioned Guava. It will modify the iterator object, just calling the contains method.

Iterable, on the other hand, is just an interface telling the compiler that there is something to iterate over. In most cases, the iterable object will be the collection itself. If you add methods such as "contains" to the Iterable interface, you get a (simplified) version of the Collection interface that already exists. Therefore, there is no need for this.

If you are stuck in your code in some place where you have a link to iterability, but you need collection functionality, you should consider reorganizing your code. Either you should use the collection of interfaces consistently, or ask yourself why you better not use the collection methods at the moment. Thus, your problem is most likely the result of suboptimal code.

On the other hand, I would find it strange to use Iterable as a type for parameters or variables anyway. Technically you can do this, but I think it is intended to be used only in loops.

+3
source share

An Iterator is like a pointer / link to an item in a collection. This is not the collection itself. Therefore, you cannot use iterator.contains() . Additionally, the Iterable interface is used to iterate through the collection using a for-each loop. Collection and Iterator / Iterable are different.

0
source share

From JavaDoc for Iterable :

Implementing this interface allows an object to be an object of a foreach statement.

I apologize that what you are trying to do is impossible.

.contains is a method of the Collection interface, not Iterable, so maybe you can use this interface.

0
source share

All Articles