Create a list of all indices - given count / size

Is there a more elegant way to create a list of all indices?

    private List<Integer> getIndexList(final int count) {
            final List<Integer> list = new ArrayList<>(count);

            for (int i = 0; i < count; i++) {
                list.add(i);
            }
            return list;
    }

      

List output:

<[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>

+3


source to share


1 answer


Yes, with streams:



List<Integer> indexList = IntStream.range(0,list.size()).boxed().collect(Collectors.toList());

      

+5


source







All Articles