Can you make this Key-interable view of a List of Maps better? I have a list of map entries, and I need an iterable that returns the keys.  

Of course, we could be naive and copy over into a new collection of the desired type, but that's inefficient.

So let's see if we can provide a view of the existing structure using generics.  Yes, I was able to do that, but it's not perfect: it gives unchecked conversions when it's passed to a function expecting Iterable&LT;K&GT;.  Could it be improved to avoid these warnings?

Perhaps it could implement Iterable&LT;K&GT; similar to the commented out line which is the path I attempted but couldn't complete.  Can you figure out the similar but correct (no warnings on usage) generic implementation?

This should help demonstrate how the ListKeyIterable is used:

List&amp;LT; Map.Entry &amp;LT; Long,String &amp;GT; &amp;GT; list;

void traverse( Iterable&amp;LT;Long&amp;GT; ) {}

traverse( new ListKeyIterable&amp;LT;List&amp;LT;Map.Entry&amp;LT;Long,String&amp;GT;&amp;GT;&amp;GT;( list );


Here is the working code, but it gives unchecked conversion warning on the call to traverse().

class ListKeyIterable&amp;LT;T extends List&amp;LT;? extends Map.Entry&amp;LT;?,?&amp;GT;&amp;GT;&amp;GT; implements Iterable
//class ListKeyIterable&amp;LT;T extends List&amp;LT;? extends Map.Entry&amp;LT;K,?&amp;GT;&amp;GT;&amp;GT; implements Iterable&amp;LT;K&amp;GT;
{
    T list;

    public ListKeyIterable( T list ) { this.list = list; }

    class ListKeyIterator&amp;LT;K&amp;GT; implements Iterator&amp;LT;K&amp;GT; {
        Iterator&amp;LT;Map.Entry&amp;LT;K,?&amp;GT;&amp;GT; iterator;

        public ListKeyIterator( Iterator&amp;LT;Map.Entry&amp;LT;K,?&amp;GT;&amp;GT; iterator ) { this.iterator = iterator; }
        @Override public boolean hasNext() { return iterator.hasNext(); }
        @Override public K next() { return iterator.next().getKey(); }
        @Override public void remove() { throw new RuntimeException( "ValueListIterator remove() not implemented." ); }
    }

    @Override public Iterator iterator() { return new ListKeyIterator( list.iterator() ); }
    //@Override public &amp;LT;K&amp;GT; Iterator&amp;LT;K&amp;GT; iterator() { return new ListKeyIterator( list.iterator() ); }
}
Just parameterize with both types:

class ListKeyIterable&lt;K,T extends List&lt;? extends Map.Entry&lt;K,?&gt;&gt;&gt; implements Iterable&lt;K&gt;;