new ArrayList() VS newArrayList()

3,786 views
Skip to first unread message

Dzmitry Lazerka

unread,
Aug 26, 2010, 11:45:33 AM8/26/10
to guava-discuss
Hi folks,

Is there any difference between guava Lists.new* methods, and standard
"new *List()" calls, except @GwtCompatible?

These methods were created for some purpose, but I can't fugure out
should I use them in my project, if I don't use GWT.

Colin Decker

unread,
Aug 26, 2010, 12:15:28 PM8/26/10
to guava-...@googlegroups.com
The difference is that you don't need to write the type parameter a second time:

List<Foo> = new ArrayList<Foo>();
vs.
List<Foo> = newArrayList(); // using static import

(By the way, the @GwtCompatible on new*List methods doesn't mean the constructors for those classes don't work on GWT, just that those methods do work.)

Colin


--
guava-...@googlegroups.com.
http://groups.google.com/group/guava-discuss?hl=en
unsubscribe: guava-discus...@googlegroups.com

This list is for discussion; for help, post to Stack Overflow instead:
http://stackoverflow.com/questions/ask
Use the tag "guava".

Craig Berry

unread,
Aug 26, 2010, 12:14:36 PM8/26/10
to guava-...@googlegroups.com
One of the motivations for the newXXX factories is to avoid repetition
of type params on the right-hand side. Compare

List<String> list = new ArrayList<String>();

vs.

List<String> list = Lists.newArrayList();

> --
> guava-...@googlegroups.com.
> http://groups.google.com/group/guava-discuss?hl=en
> unsubscribe: guava-discus...@googlegroups.com
>
> This list is for discussion; for help, post to Stack Overflow instead:
> http://stackoverflow.com/questions/ask
> Use the tag "guava".
>

--
Craig Berry
Software Engineer
Google (Santa Monica CA)

Baraa Basata

unread,
Aug 26, 2010, 1:21:23 PM8/26/10
to guava-...@googlegroups.com
I also like that I just import "Lists" and do not need to explicitly import the concrete implementations. It reinforces the discipline of interface vs implementation, and "import java.util.ArrayList" would need some other compelling reason to be used.
Reply all
Reply to author
Forward
0 new messages