How to initialize arrayList in one line

0

 



First method : Using ArrayList



ArrayList<String> list = new ArrayList<String>() {{
add("A");
add("B");
add("C");
}};



Second Method : For Java 9 or Later 


List<String> strings = List.of("foo", "bar", "baz");



Third Method : For Java 8 or earlier


List<String> strings = Arrays.asList("foo", "bar", "baz");



Fourth Method :


ArrayList<String> list = new ArrayList<String>() {{
add("A");
add("B");
add("C");
}};


















Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !