Junior — Middle
Is the get() method implemented in the standard collection interface in Java?
sobes.tech AI
Answer from AI
In the standard Java Collections interface Collection<E>, the get() method is not defined. The Collection interface describes basic operations with collections, such as adding, removing, and checking for the presence of an element, but does not provide index-based access.
The get(int index) method is defined in the List<E> interface, which extends Collection<E>. Therefore, if you need index-based access, you should use List and its implementations, such as ArrayList.
Example:
List<String> list = new ArrayList<>();
list.add("Example");
String element = list.get(0); // Accessing element by index