The document discusses the Java Collections Framework, which includes interfaces like Collection, List, Set, and Map. It describes common implementations like ArrayList, LinkedList, HashSet, TreeSet, HashMap, and LinkedHashMap. It covers the core functionality provided by the interfaces and benefits of using the framework.
2. Introduction to collections
a Collection is a container of Objects, it groups
many Objects into a single one
Arrays are too static (but can be also
considered to be collections)
Arrays have very few builtin features
Originally, Java contained a few collection
classes, like Vector, Hashtable etc.
3.
Java 1.2 introduced the Collections Framework
Another example of a collections framework is
the STL(Standard Template Library) in C++.
4. What is a Collections Framework
The Java Collections Framework consists of:
Interfaces – abstract data types representing various
collections. Allow collections to be manipulated
independently of their implementations.
Implementations – these are the concrete
implementations of the interfaces. They are reusable data
structures.
5.
Algorithms – these are able to perform useful
computations, like searching and sorting, on the
implementations of the interfaces. So, the algorithms are
polymorphic and therefore are reusable functionality.
6. Benefits of Collections
Reduces programming effort
Increases program speed and quality
Allows interoperability among unrelated APIs
Reduces effort to design new APIs
Helps to reuse the code
7. Interfaces
Here are the core Collections interfaces:
Note: Collection is at the root, Map is separate
All Collections interfaces and implementation
classes reside in the java.util package.
8. Collection interface
add(...) adds an element
contains(...) checks if the specified element
exists
remove(...) removes an element
clear() removes all elements
size() returns the number of elements
toArray() converts the Collection to an array
etc.
11. Confusing words
collection (lowercase ‘c’), which represents any of
the data structures in which objects are stored and iterated
over.
Collection (capital ‘C’), which is actually the
java.util.Collection interface from which Set and List
extend.
Collections (capital ‘C’ and ends with ‘s’),
which is actually the java.util.Collections class that holds a
pile of static utility methods for use with collections.
12. java.util.List
A List is a ordered collection of objects
Implemented by ArrayList, Vector & LinkedList
Users have control on where in List, new
element in the list should be added
List can contain duplicates element
13. ArrayList
It is an growable array. It gives you fast
iteration and fast random access.
Adding element require O(n) time.
ArrayList is not synchronized
Choose it when you need fast iteration but
aren’t as likely to be doing a lot of insertion and
deletion.
14.
15. Vector
Vector is a holdover from the earliest days of
Java
A Vector is basically the same as an ArrayList,
but Vector() methods are synchronized for
thread safety.
16. LinkedList
Doubly Linked List implementation of List
Interface
provide special method to get , remove and
insert element at the beginning and at the end
of List
Very efficient for sequential access and
insertion of element in the middle of the List
17.
18. java.util.Set
A Set is a collection that does not contain any
duplicate element.
Element that are put in a set must override
equals() method to establish uniqueness
19. HashSet
It is an unsorted, unordered Set
A Set BackedUp by HashMap
can contain null
basic operation take constant time
Iteration order may not always be same
Choose set where lookup time has to be
optimized
Operations are not synchronized
21. LinkedHashSet
It is an ordered version of HashSet
It maintains a doublylinked List across all
elements.
can contain null
Use it instead of HashSet when you care about
the iteration order
22.
23. TreeSet
It is guaranteed to be in ascending order, sorted
by the natural order of the element or by the
comparator provided.
It uses a RedBlack tree structure
can't contain null
User defined class must implement comparable
interface
It is not synchronized
24. Map interface
Map maps keys to values
Used to lookup object based on key
Doesn't extend Collection, but provides similar
methods
– put(), get(), remove() operate with single keyvalue pairs
– containsKey(), containsValue() check for existense
– Collection views: keySet(), values(), entrySet()
25. java.util.HashMap
Implementation is based on Hashtable
Ideal for fast lookup
Provide constant time performance for inserting
& locating pair
Allows one null key in a collection and multiple
null values in a collection
26. java.util.Hashtable
Hashtable is the synchronized counterpart to
HashMap.
HashMap lets you have null values as well as
one null key, a Hashtable doesn’t let you have
anything that’s null.
27. java.util.LinkedHashMap
It maintains insertion order
it will be somewhat slower than HashMap for
adding and removing elements
you can expect faster iteration with a
LinkedHashMap
28. java.util.TreeMap
Implemented based on RedBlack tree
The keys appear sorted
Basic operation take log(n) time
Methods are not synchronized