0

For example, this Q&A says List list = new ArrayList() is desirable because it deals with change easily, you can change to other List e.g. LinkedList.

However, is it really desirable on following case, for example

List<MyClass> data = new ArrayList<>();

//... long code

Random rand = new Random();
for (int i = 0; i < data.size(); i++) {
    MyClass record = data.get(rand.nextInt(data.size()));
    ...//processing
}

this code contains too many random accesses. If a successor reads List<MyClass> data = new ArrayList<>(); and thinks "OK, any List is no problem on this code. I add a bit of insertion processing, change ArrayList to LinkedList". This causes too slow processing when too many random accesses are executed.

Not only List, a declaration by interface is desirable unless implementation methods work in a similar way, isn't it?


Edit: change to true random access

5
  • 2
    You are thinking too specifically, the idea is to abstract operations. What if you wanted a method that could operate on Set and List of element(s)? Then using Collection or writing two methods are your choices. There are trade-offs to most decisions in programming. Nov 17, 2020 at 3:18
  • This is not vast problem. When I want to use ArrayList, is it appropriate to declare by List, that means "other type of List is also allowed".
    – Lamron
    Nov 17, 2020 at 3:57
  • Your example loop is poor organization for precisely this reason; you'd be better off using an Iterator and skipping every other element. That said, you can always use RandomAccessList, and some JDK algorithms do internally check whether a list is a RandomAccessList. Nov 17, 2020 at 3:58
  • I fixed example code that needs true random access, this case Iterator can't be used. And, if you are correct, List<MyClass> data = new ArrayList<>(); is incorrect and RandomAccessList<MyClass> data = new ArrayList<>(); is correct.
    – Lamron
    Nov 17, 2020 at 4:10
  • "this code contains too many random accesses" — what do you mean by this? Nov 17, 2020 at 5:58

3 Answers 3

0

In my opinion, it depends on whether compatibiliy/abstraction is more important than performance. If random access is a key feature for the funcionality, I think it is acceptable to constrict input as ArrayList and consumers will have to convert their input to ArrayList.

0

Your code example runs in exactly the same way whether you use:

List<MyClass> data = new ArrayList<>();

…or you use:

ArrayList<MyClass> data = new ArrayList<>();

Referring to an object by its more general interface (or superclass) as seen in the first version does not change any behavior. In both examples above, you are manipulating an array-backed list, an ArrayList object.

When choosing a particular implementation of List, such as LinkedList or ArrayList, you must consider how the list will likely be used in your code. We have multiple implementations because various programs have various needs. For example, if you program is doing many arbitrary accesses by specific position within the list, use ArrayList. If you are doing many edits to insert/delete various elements within the list, then use LinkedList. Each kind of List has its particular advantages.

The point of referring to the collection by its more general interface rather than the specific concrete class is that other code referring to that list will not break when you decide to change from one kind of list to another. If you come to realize that your code will be doing insertions/deletions in the middle of the list more often than arbitrary access by position, you can change this:

List<MyClass> data = new ArrayList<>();

…to this:

List<MyClass> data = new LinkedList<>();

…without breaking any other code referring to data.

Of course this assumes the other code is satisfied by the methods available on the interface. If your code requires calls to the methods specific to the ArrayList class, then you must refer to the list as a ArrayList rather than as a List.

The last paragraph of yours is unclear. Your mentions about "too many random accesses" and "too slow processing" are not explained. So edit your Question if your concerns have not been addressed.


By the way, be aware of the many useful utility methods in the Collections class. To get random elements from List, you could copy the list and then call Collections.shuffle to move the elements into randomly-chosen positions.

List< String > names = List.of( "Alice" , "Bob" , "Carol" , "Duke" ) ;
List< String > namesRandom = new LinkedList<>( names ) ;  // Copy the list into a second list to leave the original intact.
Collections.shuffle( namesRandom ) ;  // Move around the element positions within this second list.

See this code run live on IdeOne.com.

names = [Alice, Bob, Carol, Duke]
namesRandom = [Bob, Carol, Alice, Duke]
0

The general principle is that the type of the variable should reflect what it is used for, rather than what the object is "made of" (i.e. its internal implementation details).

In most cases, what matters is that the object can be used as a list, and it doesn't matter that a list is "made of" an array, so the standard advice is to declare a variable of type List<Foo> instead of ArrayList<Foo>.


In your specific case, you want to use the object for fast random access; it still doesn't matter that the object is "made of" an array, since any other list implementation with fast random access would be fine. (For example, the return value of Arrays.asList.) So applying the general principle in your case, the type should reflect the fact that you want to use it for fast random access.

Unfortunately, Java does not have a type like RandomAccessList<Foo>. There is a marker interface RandomAccess which can be used by classes implementing List to denote that they support fast random access, but a variable declared like RandomAccess fooList = new ArrayList(); is not useful since the marker interface does not have methods like get.

So the appropriate thing to do is to declare the variable as type List<Foo> and either write a comment saying it should support fast random access (if the list is created locally), or check that fooList instanceof RandomAccess and throw an IllegalArgumentException otherwise (if the list is supplied from elsewhere).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.