Maybe we should stop declaring List list = new ArrayList()

Asked 1 years ago, Updated 1 years ago, 339 views

It is often stated that List=newArrayList() should be written instead of ArrayList=newArrayList(), but is this really an excellent way to write?
For example, English stackoverflow states that List=new ArrayList() is a good way to write List lnew ArrayList()If it's better to use LinkedList instead of ArrayList, you just have to rewrite the new ArrayList().

But is that true?Talk about using LinkedList lightly and having to take a break - If you were writing a code with a lot of random access, such as Qiita,

for(inti=0;i*2<data.size();i++){
    MyClass record=data.get(i*2);
}

List list=new ArrayList() and my successor said, "Well, with this code, anything is fine as long as it's a list.Then, there's something I'd like to insert, so I'll change it to LinkedList. As the article says, it's a very late code.

Shouldn't the declaration by an interface be used unless it is guaranteed that "all the methods implemented on that interface work the same way"?

Note: I also asked questions in English https://stackoverflow.com/questions/64868813/is-list-list-new-arraylist-really-desirable-from-arraylist-list-new-arr

java

2022-09-30 21:53

4 Answers

Although it may be more appropriate to express it in concrete form than List (interface), I think it is appropriate to use the Qiita case and the case in the questionnaire.

There are two positions on the interface: user and provider (implementer), and Qiita's article is an example of the user's position and the questionnaire is an example of the provider's position (*You may misunderstand.

And the advantage of providing the List type instead of the concrete type is that the user is not aware of the implementation and is not an advantage to the provider.In other words, in the questionnaire case,

"I see, with this code, anything on the list is fine.Then, I'd like to insert something, so I'll change it to LinkedList.

The idea is wrong, and providers must provide the right implementation for the expected usage regardless of whether it is a List.

On the other hand, I think the Qiita case is a mistake in using the List.

https://docs.oracle.com/javase/jp/11/docs/api/java.base/java/util/List.html

In some implementations (such as LinkedList classes), the execution of these operations may take time proportional to the index value. Therefore, if the caller does not know about these implementations, repeat the elements in the list rather than index the list.

Declaration by an interface should not be used unless it is guaranteed that "all methods implemented on that interface behave the same way."

(I think the person who should be aware of this point is not the user but the provider, but I will leave this point aside for now)
In this case, List does not specify/guarantee anything about the amount of access calculated by the index.
Rather, it suggests that different implementations have different computations, and it does.


2022-09-30 21:53

The way java handles this issue is that RandomAccess interface is provided to determine if objects implementing the list given by at least instanceof are random accessible.

The point is that specific implementation classes are not changeable, but List is too loose. If the random access feature is critical, I thought there could be an implementation that would runtime check and make an exception if it wasn't RandomAccess instance.

Since it's Java, I think some kind of test is implemented, so if you give LinkedList, for example, an exception will occur and the test fails, other implementers will notice it there.


2022-09-30 21:53

If the design requires the function of ArrayList, I think it should be ArrayList, and if it is a generic method, shouldn't it be List?
I don't think it's a rule to do this.
I personally think that the ideal is to abstract it as much as possible if the design requires assumptions and no specific assumptions.


2022-09-30 21:53

I have a similar idea, and I think it would be better to not accept instances of classes that Java has as a standard by interface name.
As for the collection, I think the interface name only accepts arguments for the method.


2022-09-30 21:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.