Professional Documents
Culture Documents
KEC
COLLECTIONS
TOPICS
1. Collection Framework: Overview
2. Collection Interfaces
3. Collection Classes.
WHY COLLECTIONS?
In Java arrays have limitations.
1.They cannot dynamically shrink and grow.
2.They have limited type safety.
3.Implementing efficient, complex data structures from scratch would
be difficult.
Definition:
The Java Collections Framework is a set of classes and interfaces
implementing complex collection data structures.
A collection is an object that represents a group of objects.
COLLECTIONS
FRAMEWORK BENEFITS:
1. high-performance - implementations for the fundamental
collections (dynamic arrays, linked lists, trees, and hash tables)
are highly efficient.
2. high degree of interoperability - allow different types of
collections to work in a similar manner
3. extending and/or adapting a collection - easy.
4. Reduces programming effort (already there)
5. Increases performance (tested and optimized)
6. Part of the core API (available, easy to learn)
7. Promotes software reuse (standard interface)
8. Easy to design APIs based on generic collections
Java Collection Interface
The Collection interface is the root interface of the Java collections framework. There is no
direct implementation of this interface. However, it is implemented through its
subinterfaces like List, Set, and Queue.
The List interface is an ordered collection that allows us to add and remove elements
1. List Interface like an array.
The Set interface allows us to store elements in different sets similar to the set in
2. Set Interface mathematics. It cannot have duplicate elements.
The Queue interface is used when we want to store and access elements in First
3. Queue Interface In, First Out(FIFO) manner.
Methods of Collection
List<Data-Type> l= new LinkedList<Data-Type>();
List<Data-Type> a = new ArrayList<Data-Type>();
List<Data-Type> st = new Stack<Data-Type>();
List<Data-Type> v = new Vector<Data-Type>();
import java.util.*;
public class ScalerTopics {
public static void main(String args[])
{
ArrayList<String> str = new ArrayList<String>();
str.add("Scaler");
str.add("Topics");
str.add("Rocks");
System.out.println("ArrayList is" + str);
}
}
10
System.out.println(myNumbers);
15
20
Output: [10,15,20,25]
25
cars.get(0); Volvo
[]
[4]
Volvo
BMW
Ford
Mazda
1. Linked List is a linear data structure where the elements are called as nodes.
2. Here, each node has two fields- data and next. Data stores the actual piece of information
and next points to the next node. 'Next' field is actually the address of the next node.
3. Elements are not stored in a contiguous memory, so direct access to that element is not
possible.
4. LinkedList uses Doubly Linked List to store its elements while ArrayList internally uses a
dynamic array to store its elements.
5. LinkedList is faster in the manipulation of data as it is node-based which makes it unique.