title: Are there any Java standard classes that implement Iterable without implementing Collection?
body: <p>I have a conundrum that's caused me to ponder whether there are any standard java classes that implement <code>Iterable&lt;T&gt;</code> without also implementing <code>Collection&lt;T&gt;</code>. I'm implementing one interface that requires me to define a method that accepts an <code>Iterable&lt;T&gt;</code>, but the object I'm using to back this method requires a <code>Collection&lt;T&gt;</code>.</p>
  
  <p>This has me doing some really kludgy feeling code that give some unchecked warnings when compiled.</p>
  
  <pre><code>public ImmutableMap&lt;Integer, Optional&lt;Site&gt;&gt; loadAll(
          Iterable&lt;? extends Integer&gt; keys
  ) throws Exception {
      Collection&lt;Integer&gt; _keys;
      if (keys instanceof Collection) {
          _keys = (Collection&lt;Integer&gt;) keys;
      } else {
          _keys = Lists.newArrayList(keys);
      }
  
      final List&lt;Site&gt; sitesById = siteDBDao.getSitesById(_keys);
      // snip: convert the list to a map
  </code></pre>
  
  <p>Changing my resulting collection to use the more generified <code>Collection&lt;? extends Integer&gt;</code> type doesn't eliminate the unchecked warning for that line. Also, I can't change the method signature to accept a <code>Collection</code> instead of an <code>Iterable</code> because then it's no longer overriding the super method and won't get called when needed.</p>
  
  <p>There <a href="https://stackoverflow.com/questions/6416706/easy-way-to-change-iterable-into-collection">doesn't seem to be a way around</a> this cast-or-copy problem: other questions have been asked here an elsewhere and it seems deeply rooted in Java's generic and type erasure systems. But I'm asking instead if there ever are any classes that can implement <code>Iterable&lt;T&gt;</code> that don't also implement <code>Collection&lt;T&gt;</code>? I've taken a look through the <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Iterable.html" rel="nofollow noreferrer"><code>Iterable</code> JavaDoc</a> and certainly everything I expect to be passed to my interface will actually be a collection. I'd like to use an in-the-wild, pre-written class instead as that seems much more likely to actually be passed as a parameter and would make the unit test that much more valuable.</p>
  
  <p>I'm certain the cast-or-copy bit I've written works with the types I'm using it for in my project due to some unit tests I'm writing. But I'd like to write a unit test for some input that <em>is</em> an iterable yet <em>isn't</em> a collection and so far all I've been able to come up with is implementing a dummy-test class implementation myself.</p>
  
  <hr>
  
  <p>For the curious, the method I'm implementing is Guava's <a href="http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/cache/CacheLoader.html#loadAll(java.lang.Iterable)" rel="nofollow noreferrer"><code>CacheLoader&lt;K, V&gt;.loadAll(Iterable&lt;? extends K&gt; keys)</code></a> and the backing method is a JDBI instantiated data-access object, which requires a collection to be used as the parameter type for the <a href="http://grepcode.com/file/repo1.maven.org/maven2/com.ning/metrics.collector/2.0.9/org/skife/jdbi/v2/unstable/BindIn.java" rel="nofollow noreferrer"><code>@BindIn</code></a> interface. I think I'm correct in thinking this is tangental to the question, but just in case anyone wants to try lateral thinking on my problem. I'm aware I could just fork the JDBI project and rewrite the <code>@BindIn</code> annotation to accept an iterable...</p>