I recently encountered a piece of Java code on LeetCode that uses a reversed() method directly on a List<Integer> object, like so:
List<Integer> l = Arrays.asList(1, 2, 3, 4, 5);
System.out.println(l.reversed()); // [5, 4, 3, 2, 1]
To my knowledge, and according to the Java 17 API documentation, the List interface does not have a reversed() method.
However, this code compiles and runs successfully within the LeetCode environment. This behavior has left me puzzled because, in a standard Java environment (like IntelliJ IDEA with JDK 17), this code results in a compilation error, as expected.
I am curious if anyone can explain why this code works on LeetCode. Are there any common extensions or custom libraries that LeetCode might use to enable such syntax in their Java environment?
My Question:
Why does the
reversed()method work on aListobject in LeetCode's Java environment?How can one implement to enable this
.reversed()functionality in a standard Java environment?