Listy.newArrayList vs new ArrayList
Jaka jest najlepsza konstrukcja do tworzenia List
ciągów? Czy to Lists.newArrayList()
(z guawy) czy new ArrayList()
?
czy jest to po prostu Typ generic Typ wnioskowania?
czy istnieje jakaś teoretyczna lub praktyczna wartość w korzystaniu z list.newArrayList()?
8 answers
Guava builder zapisuje wpisywanie argumentów typu wiele razy. Porównaj:
List<Foo<Bar, Baz>> list = Lists.newArrayList();
List<Foo<Bar, Baz>> list = new ArrayList<Foo<Bar, Baz>>();
W Javie 7 jest to trochę przestarzałe, ponieważ masz operator diamond:
List<Foo<Bar, Baz>> list = new ArrayList<>();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-02 17:30:56
Z Źródła guawy :
public static <E> ArrayList<E> newArrayList() {
return new ArrayList<E>();
}
Wszystko, co robi, to pozwala na wnioskowanie typu-bez różnicy w czasie wykonywania.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-08 08:06:53
Dodaj jeden punkt, przeciążenie wersji list.newArrayList () jest bardziej użyteczne:
Lists.newArrayList(E... elements)
-
Lists.newArrayList(Iterable<? extends E> elements)
-
Lists.newArrayList(Iterator<? extends E> elements)
Zapewniają więcej użytecznych funkcji niż new ArrayList()
.
Na przykład: new ArrayList()
nie można wykonać:
Lists.newArrayList("a","b");
Lists.newArrayList(anIterable);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-03 02:11:28
Oto lista.newArrayList napisał (a):
@GwtCompatible(serializable = true)
public static <E> ArrayList<E> newArrayList() {
return new ArrayList<E>();
}
Więc te dwa są zasadniczo takie same, z użyciem newArrayList ma tę zaletę, że nie trzeba powielać typu generycznego. Jest to bardzo pomocne w przypadku złożonych leków generycznych:
List<Map<X,List<Y>> list = new ArrayList<Map<X,List<Y>>();
List<Map<X,List<Y>> list = Lists.newArrayList();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-02 17:37:24
Jak wyjaśniono TUTAJ , główne motywacje do używania Lists
, Sets
etc mają poprawić czytelność / powielanie w kodzie, a także wnioskowanie typu.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-02 17:32:06
Java 8 approach for overloaded constructors.
Jeśli chcesz użyć przeciążonych konstruktorów z Guava, polecam inny sposób używając Java 8 Stream Builder.
List<String> listTest = Stream.<String>builder
.add("Hello").add("My Name").add("Blah")
.build().collect(Collectors.toList());
Jeśli korzystasz z Javy w wersji 9+, możesz użyć poniżej. Ale ta metoda zwraca ImmutableList, więc nie można dodać więcej elementów do niej.
List.of("My","Name", "IS", "this");
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-06-16 16:09:43
Jak wspomniano, Java 7 sprawia, że jest to przestarzałe, ale używam metody factory, ponieważ ułatwia później zmianę typu listy, zestawu, mapy lub cokolwiek innego.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-02 17:33:09
Możesz używać Arrays.asList()
z java.util
więc nie musisz używać Guava:
List<String> list = Arrays.asList("one","another", null, "one_more")
W przeciwieństwie do Javy 8, Stream Builder jest również opcją, ale jest zbyt gadatliwy i przesadny dla większości typowych przypadków użycia)Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-02-28 11:38:27