You are on page 1of 42

SARANYA S S

AP/ CT –UG UNIT 5

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.

E specifies the type of objects


that the collection will hold

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);
}
}

ArrayList is [Scaler, Topics, Rocks]


[10, 15, 20, 25]

10
System.out.println(myNumbers);
15
20
Output: [10,15,20,25]
25
cars.get(0); Volvo

[Opel, BMW, Ford, Mazda]

[BMW, Ford, Mazda]

[]

[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.

LinkedList list=new LinkedList();


The LinkedList class is almost identical to the ArrayList
Sorting an ArrayList in ascending order
Sorting an ArrayList in descending order
import java.util.*;
public class ScalerTopics{
public static void main(String args[]){
HashSet<String> str= new HashSet<String>();
System.out.println("Size at the beginning "+str.size());
str.add("Hello");
str.add("Hi");
str.add("Namaste");
str.add("Bonjour");
System.out.println(str);
System.out.println("Size after addition "+str.size());
str.remove("Bonjour");
System.out.println(str);
System.out.println("Size after removal "+str.size());
}
Size at the beginning 0
} [Hi, Hello, Namaste, Bonjour]
Size after addition 4
[Hi, Hello, Namaste]
Size after removal 3
import java.util.*;
public class ScalerTopics{
public static void main(String args[]){
LinkedHashSet<String> str= new LinkedHashSet<String>();
System.out.println("Size at the beginning "+str.size());
str.add("Hello");
str.add("Hi");
str.add("Namaste");
str.add("Bonjour");
System.out.println(str);
System.out.println("Size after addition "+str.size());
str.remove("Bonjour");
System.out.println(str);
System.out.println("Size after removal "+str.size());
}
}

Size at the beginning 0


[Hello, Hi, Namaste, Bonjour]
Size after addition 4
[Hello, Hi, Namaste]
Size after removal 3
import java.util.*;
class ScalerTopics {
public static void main(String[] args)
{
Set<String> ts = new TreeSet<>();
System.out.println("Size at the beginning "+ts.size());
ts.add("India");
ts.add("USA");
ts.add("Britain");
System.out.println(ts);
System.out.println("Size after addition "+ts.size()); Size at the beginning 0
ts.add("Britain"); [Britain, India, USA]
System.out.println(ts); Size after addition 3
}} [Britain, India, USA]
import java.util.*;
public class ScalerTopics{
public static void main(String args[])
{
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
System.out.println("Size at the beginning "+pq.size());
pq.add(99);
pq.add(18);
pq.add(27);
pq.add(34);
System.out.println("New PriorityQueue" + pq);
System.out.println("Size after addition "+pq.size());
System.out.println("Top-most element " +pq.peek());
System.out.println("Removing " +pq.poll());
System.out.println("New PriorityQueue after removal" + pq);
System.out.println("Size after removal "+pq.size());
}
} Size at the beginning 0
New PriorityQueue[18, 34, 27, 99]
Size after addition 4
Top-most element 18
Removing 18
New PriorityQueue after removal[27, 34, 99]
Size after removal 3

You might also like