Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Collection and Arraylist in Java? [duplicate]

Tags:

java

Possible Duplicate:
What is the benefit of polymorphism using Collection interface to create ArrayList object?

 ArrayList al=new ArrayList();
 Collection c=new ArrayList();

What is difference between object al and c? Are both of them are same or what?

like image 415
Gautam Savaliya Avatar asked Aug 23 '26 18:08

Gautam Savaliya


1 Answers

The Collections API is a set of classes and interfaces that support operations on collections of objects.

Example of classes: HashSet, HashMap, ArrayList, LinkedList, TreeSet and TreeMap. Example of interfaces: Collection, Set, List and Map.

Whereas, ArrayList: It is re-sizable array implementation. Belongs to 'List' group in collection. It permits all elements, including null. It is not thread -safe.

Collections: It implements Polymorphic algorithms which operate on collections.

Collection: It is the root interface in the collection hierarchy.

The following interfaces (collection types) extends the Collection interface:

  • List
  • Set
  • SortedSet
  • NavigableSet
  • Queue
  • Deque

Java does not come with a usable implementation of the Collection interface, so you will have to use one of the listed subtypes. The Collection interface just defines a set of methods (behaviour) that each of these Collection subtypes share. This makes it possible ignore what specific type of Collection you are using, and just treat it as a Collection. This is standard inheritance, so there is nothing magical about, but it can still be a nice feature from time to time.

like image 120
code_fish Avatar answered Aug 26 '26 13:08

code_fish