List<Foo<Bar, Baz>> list = Lists.newArrayList();
List<Foo<Bar, Baz>> list = new ArrayList<Foo<Bar, Baz>>();
// Java 7부터 다이아몬드 오퍼레이터를 사용하여 후자도 간단하게 표현 가능
List<Foo<Bar, Baz>> list = new ArrayList<>();
내부적으로도 이렇게 구현되어있어서 argument를 넘겨주지 않을 시 new ArrayList<>()와 같음
public static <T> ArrayList<T> newArrayList(Iterator<? extends T> elements) {
if (elements == null) {
return null;
}
ArrayList<T> list = newArrayList();
while (elements.hasNext()) {
list.add(elements.next());
}
return list;
}
public static <T> ArrayList<T> newArrayList() {
return new ArrayList<>();
}
단, List.newArrayList를 사용하면 ArrayList를 import하지 않아도 돼서 좀 더 깔끔하다는 장점이 있다..?
참고자료 - https://stackoverflow.com/questions/9980915/lists-newarraylist-vs-new-arraylist
Lists.newArrayList vs new ArrayList
What is the best construction for creating a List of Strings? Is it Lists.newArrayList() (from guava) or new ArrayList()? is it just a personal preference? or is it just Type generic type inferen...
stackoverflow.com
'프로그래밍 > Java' 카테고리의 다른 글
자바 메서드 파라미터 개수를 가변으로 받고 싶을 때 사용 가능한 문법 (0) | 2022.02.25 |
---|---|
2.변수와 타입 (0) | 2021.12.17 |
1. 자바의 특징 (0) | 2021.12.16 |
0. 자바 학습 시작 (0) | 2021.12.16 |