1. Collection Framework - I
consists of THREE parts
• interfaces, the abstract data types that the
framework supports.
• implementations, the concrete versions of
these interfaces.
• algorithms, the predefined actions that can be
defined on either the interfaces or their
implementations.
1
2. Collection Framework - I
• All implementations are unsynchronized.
• All implementations are serializable and
cloneable.
• All implementations support having null
elements.
2
3. Collection Framework - I
• The predefined algorithms for supporting the
framework are found in the Collections and
Arrays classes.
3
4. Collection Framework - I
• Framework - Four core interfaces
• Two specializations for sorting
1. Collection
2. List
3. Set
4. Map
5. SortedSet
6. SortedMap
4
5. Collection Framework - I
Interface type and its implementation
5
Interface Implemented
Set HashSet, LinkedHashSet, EnumSet
SortedSet TreeSet
List Vector, Stack, ArrayList, LinkedList
Queue PriorityQueue, LinkedList
Map
Hashtable, HashMap, LinkedHashMap,
WeakHashMap, IdentityHashMap
SortedMap TreeMap
6. Collection Framework - I
Conversion of different Collection data types
• Implementations of the Collection interface
normally have a constructor that accepts a
Collection object.
• This enables you to convert a Collection to a
different type of Collection, such as a Queue
to a List, or a List to a Set, etc.
6
7. Collection Framework - I
Conversion of different Collection data types
constructors of some implementations
• public ArrayList (Collection c)
• public HashSet (Collection c)
• public LinkedList (Collection c)
7
8. Collection Framework - I
An example that converts a Queue to a List
import java.util.*;
public class MainClass {
public static void main(String[] args) {
Queue queue = new LinkedList();
queue.add("Hello");
queue.add("World");
List list = new ArrayList(queue);
System.out.println(list);
} // output :: [Hello, World]
} 8
9. Collection Framework - I
This example converts a List to a Set.
import java.util.*;
public class MainClass {
public static void main(String[] args) {
List myList = new ArrayList();
myList.add("Hello");
myList.add("World");
myList.add("World");
Set set = new HashSet(myList);
System.out.println(set);
}
}
9