Java.util package - classes versus interfaces

Why is Queue an interface but others like Stack and ArrayList are classes?

I understand that interfaces are made so that clients can implement and add them according to their own methods, whereas with classes, if each client needs its own methods there, it will become huge and bloated.

... or am I missing something here?

+8
java stack queue interface
source share
5 answers

A Queue can be implemented in several models, as well as List or Set . All of them simply indicate a contract for various types of collections.

An ArrayList , however, is a concrete implementation of List made for the internal use of an array to store elements. LinkedList also an implementation of a List that uses a number of interconnected nodes, i.e. a doubly linked list . Similarly, TreeSet and HashMap are concrete implementations of sets and mappings, respectively.

Now Stack is an odd case, especially because it is an inherited class from older versions of Java. You should no longer use Stack ; instead, you should use its modern equivalent, ArrayDeque . ArrayDeque is an implementation of a Deque (double queue) that internally uses an array for storage (which is what Stack does). A Deque supports all a Stack operations, such as pop , push , etc. Other Deque implementations include LinkedList , as mentioned by someone else, although this deviates from Stack in that the underlying is not an array, but a doubly linked list: -p

Now there are many Queue implementations and many different types of Queue s. You have not only a BlockingQueue (often used for a consumer producer), common implementations of which include LinkedBlockingQueue and ArrayBlockingQueue , but also TransferQueue s, etc. I digress ... you can learn more about the collection APIs in the corresponding Java Tutorial .

+7
source share

You understood the interface correctly. In this case, the standard Java library already provides both implementations and interfaces. It is better to use an interface so that you can switch the implementation at any time.

Hope this makes sense.

+3
source share

I think Stack well known as a class that should be an interface. Java libraries are a bit shaky when you need to choose the right interface.

ArrayList is just an implementation of the List interface, so Sun got it right! Another classic miss (in my opinion) is the Observable class, which is very necessary for implementing the default interface, and not just for the class.

0
source share

Interest Ask. My thought about this is that Queue is the basis for many data structures like BlockingQueue, PriorityQueue, Deque, etc. This group of classes needs a specific implementation for various operations, so it is much easier to make Queue as an interface.

0
source share

Reason interfaces are used for list and queue NOT to reduce excessive code.

The main advantage of interfaces is that they allow you to write flexible, loosely coupled code.

(Here's an amazing answer that perfectly describes this concept)

The interface simply defines a list of methods that will be implemented by the class.

This allows us to do a wonderfully powerful thing:

  • We can handle all classes that implement the interface in the same way.

This is a HUGE advantage.

Here is a very simple example:

We want to write a debugging method that prints each item in a Collection .

Collection is an interface. It defines a list of operations and does not implement them.

You cannot create an instance of the collection. You can instantiate a class that implements Collection.

There are many classes that implement Collection : ArrayList, Vector, TreeSet, LinkedList, etc. They all have different tricks, but they also have some things in common: since each class implements Collection, they all implement every method found here .

This allows us to do a very powerful thing:

  • We can write a method that works with ANY class that implements Collection.

It will look something like this:

 public void printCollection(Collection semeCollection) { for (Object o : someCollection) { String s = (o == null) ? "null" : o.toString(); System.out.println(s); } } 

Due to the magic of interfaces, we can now do the following:

 public void testStuff() { Collection s = new TreeSet(); Collection a = new ArrayList(); Collection v = new Vector(); s.add("I am a set"); a.add("I am an array list"); v.add("I am a vector"); printCollection(s); printCollection(a); printCollection(v); } 
0
source share

All Articles