public static < E > Optional < E > get ( final Iterable < E > iterable , final int position ) { checkNotNull ( iterable , "Get requires an iterable" ) ; if ( position < 0 ) { return Optional . empty ( ) ; } int iterablePosition = 0 ; for ( E anIterable : iterable ) { if ( iterablePosition == position ) { return of ( anIterable ) ; } iterablePosition ++ ; } return Optional . empty ( ) ; }
Return an optional of an element from a specified position in an iterable . If the position is out of bounds an empty optional is returned . Positions start at 0 .