JAVA: returns a AbstractList which is filled but can't be seen were it is looped. Never seen programming style like this

60 Views Asked by At

This is the fastest solution for FizzBuzz program in leetcode(Question 412). I can't able to understand it line of execution and whats actually happening in program. Please help me out with explaining the line of execution of the program. Just tell me where the looping happens.

import java.util.AbstractList;

class Solution {
    public List<String> fizzBuzz(int n) {
        return new AbstractList<>() {
            @Override
            public String get(int i) {
                i++;
                if(i%15==0)
                    return "FizzBuzz";
                if(i%3==0)
                    return "Fizz";
                if(i%5==0)
                    return "Buzz";
                return i+"";
            }
            @Override
            public int size() {
                return n;
            }
        };
    }
}
1

There are 1 best solutions below

0
Almaz Shakirov On

Actually there is no looping.

They create a list which overrides methods. Method size returns given n value. Method get returns a string dependent on a given i value. So, it means that they don't store values in a list - they just calculate value ("FizzBuzz"/"Fizz"/"Buzz") on a given index