Why Does Arraylist Have "Implements List"

Why does arraylist class implement List as well as extend AbstractList?

For your first question take a look at Why does ArrayList have "implements List"?


To answer your second question

java.util.ArrayList<String> a = Arrays.asList(stra);

as you mentioned Arrays.asList returns its own implementation of AbstractList and unfortunately creators of this code also named this class ArrayList. Now because we cant cast horizontally but only vertically returned array list can't be cast to java.utli.ArrayList but only to java.util.AbstractList or its super types like java.util.List that is why your first code example works.

Why does ArrayList have implements List?

Yes. It could've been omitted. But thus it is immediately visible that it is a List. Otherwise an extra click through the code / documentation would be required. I think that's the reason - clarity.

And to add what Joeri Hendrickx commented - it is for the purpose of showing that ArrayList implements List. AbstractList in the whole picture is just for convenience and to reduce code duplication between List implementations.

LinkedList in Java: Why it directly implements List when it indirectly from extended class

No, it has no impact in terms of language semantics in this case. I imagine they just put it to make it clearer that it implements List as well, without the developer having to traverse the full hierarchy.

Type List vs type ArrayList in Java

Almost always List is preferred over ArrayList because, for instance, List can be translated into a LinkedList without affecting the rest of the codebase.

If one used ArrayList instead of List, it's hard to change the ArrayList implementation into a LinkedList one because ArrayList specific methods have been used in the codebase that would also require restructuring.

You can read about the List implementations here.

You may start with an ArrayList, but soon after discover that another implementation is the more appropriate choice.

Reason for - List list = new ArrayList();

When someone writes code like this, he/she is trying to follow a basic OO design principle which says -

Program to an interface, not to a concrete implementation

I have explained this principle in one of my blog posts. Look in the Class Inheritance VS Interface Inheritance section.

To summarize the post, when you use a reference of a parent type to refer to an instance of a sub-type, you get a lot of flexibility. For example, if you ever need to change your sub-type implementation in the future, you will be able to do that easily, without changing much of your code.

Consider the following method -

public void DoSomeStuff(Super s) {
s.someMethod();
}

and a call to this method -

DoSomeStuff(new Sub());

now, if you ever need to change the logic inside someMethod, you can easily do it by declaring a new subtype of Super, say NewSubType, and changing the logic inside that implementation. In this way, you will never have to touch other existing code which utilizes that method. You will still be able to use your DoSomeStuff method in the following way -

DoSomeStuff(new NewSubType());

Had you declared the parameter of DoSomeStuff to be of Sub, you would then have to change its implementation too -

DoSomeStuff(NewSubType s) {
s.someMethod();
}

and it may also chain/bubble to several other places.

In terms of your collection example, this lets you change the list implementation that a variable is pointing to without much hassle. You can easily use a LinkedList in place of an ArrayList.

Polymorphism: Why use List list = new ArrayList instead of ArrayList list = new ArrayList?

The main reason you'd do this is to decouple your code from a specific implementation of the interface. When you write your code like this:

List list = new ArrayList();  

the rest of your code only knows that data is of type List, which is preferable because it allows you to switch between different implementations of the List interface with ease.

For instance, say you were writing a fairly large 3rd party library, and say that you decided to implement the core of your library with a LinkedList. If your library relies heavily on accessing elements in these lists, then eventually you'll find that you've made a poor design decision; you'll realize that you should have used an ArrayList (which gives O(1) access time) instead of a LinkedList (which gives O(n) access time). Assuming you have been programming to an interface, making such a change is easy. You would simply change the instance of List from,

List list = new LinkedList();

to

List list = new ArrayList();  

and you know that this will work because you have written your code to follow the contract provided by the List interface.

On the other hand, if you had implemented the core of your library using LinkedList list = new LinkedList(), making such a change wouldn't be as easy, as there is no guarantee that the rest of your code doesn't make use of methods specific to the LinkedList class.

All in all, the choice is simply a matter of design... but this kind of design is very important (especially when working on large projects), as it will allow you to make implementation-specific changes later without breaking existing code.

Why does ArrayList implement RandomAccess Interface?

Interfaces with no methods are called marker interfaces in Java.

As per the JavaDoc of RandomAccess:

Marker interface used by List implementations to indicate

that they support fast (generally constant time) random access.

For more information check the two JavaDoc pages.

http://docs.oracle.com/javase/6/docs/api/java/util/RandomAccess.html

http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html

ArrayList in Java doesn't implement methods from Collection interface?

containsAll() method is defined by AbstractCollection which is extended by AbstractList, which is in turn extended by ArrayList. So ArrayList inherits containsAll() implementation.

Consider the following code:

List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
boolean contains = list.containsAll(Arrays.asList("b", "c"));

Here, when list.containsAll() is called, actually the method declared in AbstractCollection is executed.

Do ArrayList or HashSet classes implements Collection interface implicitly?

The standard JDK list/set implementations extend AbstractList and AbstractSet, respectively, which provide a "skeletal implementation" of the List/Set interfaces. The concrete classes (such as ArrayList and HashSet) fill in the gaps by implementing any methods that are not covered by these abstract classes (or overriding them as needed). So, just by looking at the ArrayList source, for example, you might get the impression that not all methods are implemented, but they are if you also look in AbstractList (and AbstractCollection -- see below).

Both AbstractList and AbstractSet extend AbstractCollection which implements the Collection interface (again providing a "skeletal implementation").



Related Topics



Leave a reply



Submit