How convert below Java for loop to stream? [closed]

I’ve been trying to use map() to do the first two steps but I’m not quite sure how to get the index i that represents the count of the current iteration:

for (int i = 0; i < carList.size(); i++) {
    String country = carList.get(i).getCountry();
    List<Integer> indexes = carsToIndexMap.getOrDefault(country, new ArrayList<>());
    carsToIndexMap.put(country, indexes);

I’ve been trying something like this:
    .map(p -> p.getCountry())
    .map(country -> carsToIndexMap.getOrDefault(country, new ArrayList<>()))


If I am not wrong, you seem to be looking for a grouping by country with indexes of the cars processed corresponding to it. This can be achieved by making use of IntStream with the range of carList size and groupingBy collector.

Map<String, List<Integer>> carsToIndexMap = IntStream.range(0, carList.size())
        .collect(Collectors.groupingBy(i -> carList.get(i).getCountry()));

Suggestion: Your variable name should indicate that the key is country and not cars, something like countryIndices.