Why assign an ArrayList to a List

Conclusion or personal opinion

Maybe it's because there's no concern there.

It ’s an extreme phrase, As long as the user guarantees the interface I'm not interested in the processing cost of List <T> :: size ().

It's not a reason why you have to list it There is no clear reason to be so conscious.

As a result, it's enough, so List is fine. Therefore, it is assigned to List.

Supplement or excuse

Of course, if it is a commercial service, I think it should be verified by PoC or performance test. It is necessary to verify whether the usage was correct in another process. I have no intention of recommending coding that it should work.

Also, since the Stream API was added, the chances of writing new ArrayList <> () have decreased. It may be all right now.

Expectations for those who read

~~ It's like burning ~~ I'd be happy if you could give us various opinions such as objections and counterarguments.

background

When I was watching the buzzword on twitter earlier this week, a coffee shop in the city center was on fire.

From the flow of digging deeply into what everyone has and what kind of impression they have When I noticed, I was investigating the characteristics of Kramer. (If you follow the Wikipedia link, it feels like you'll reach the universe at the end for some reason.)

At that time, in a Java article with a slightly burning comment section, why

sample1.java


ArrayList<T> list = new ArrayList<>();

not,

sample2.java


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

I noticed the question, "Is it?"

The questioned side

--There is no such code on git or OSS, right?

The person who asked the question

――I do it for no reason because it is religious

I was writing that.

From the rough way of the comment section At first, I said, "There are some types like this ..." Gradually, I thought, "But surely ... why?" I wanted to output "How would I answer the same question?"

Now that person is not a Kramer I am grateful for the opportunity to question the code I was casually writing.

I will not give you a name, but I would like to take this opportunity to thank you.

Recommended Posts

Why assign an ArrayList to a List
[Java] How to use List [ArrayList]
Cast an array of Strings to a List of Integers in Java
How to sort a List using Comparator
Ability to display a list of products
How to use an array for a TreeMap key
java: How to write a generic type list [Note]
Send a command to Spigot from an external process
[Ruby] Does self refer to a class or an instance?
Assign a Java8 lambda expression to a variable and reuse it
How to connect a container created later to an existing network
[iOS] [Objective-C] How to update a widget from an Objective-C app
How to change a string in an array to a number in Ruby
I want to make a list with kotlin and java!
How to store a string from ArrayList to String in Java (Personal)
Convert an array that may be null to a stream
[Java] How to search for a value in an array (or list) with the contains method