Java 8 enhanced for loop with index/range
I would use subList
in this case:
for(String s : list.subList(1, list.size()))
and
for(String s : list.subList(0, 6))
Using of sublist
is better but stream version is using skip
and limit
:
list.stream().skip(1) .... limit(6).....
In the Java 8 we have Stream API, which we could use to iterate over List with custom indexes:
List<String> evenIndexes = IntStream
.range(0, names.length)
.filter(i -> i % 2 == 0)
.mapToObj(i -> names[i])
.collect(Collectors.toList());
in the range
method, you could start from 1, and/or iterate to 5.