Avatar ·

Initializing ArrayList in one line in Java: best ways

Hello everyone! I'm a beginner Java developer, and I have a problem with initializing an ArrayList. I know that you can add elements one by one using the `add()` method: ```java ArrayList list = new ArrayList<>(); list.add("Apple"); list.add("Banana"); list.add("Orange"); ``` But this takes 4 lines of code! It seems not very convenient, especially when you need to create a small list with fixed values, for example, for test data or configuration. I've seen that in other languages (like Python or JavaScript) you can do this: ```python # Python my_list = ["Apple", "Banana", "Orange"] ``` And in Java I tried this syntax, but it doesn't work: ```java ArrayList list = new ArrayList<>(){"Apple", "Banana", "Orange"}; ```