This document provides an overview of Java collection classes and frameworks. It discusses the key collection interfaces like Set, List, Queue and Map. It describes commonly used collection classes like ArrayList, LinkedList, HashSet, HashMap etc. and their characteristics. It also compares similar classes like ArrayList and Vector. The document explains when to use different collection classes based on requirements like storing unique vs duplicate elements, ordered vs unordered storage etc. It lists common methods supported by all collection classes.
Goa Call Girls Just Call 👉📞9731111159 Top Class Call Girl Service Available
22.collections(1)
1.
2. Collection is a java object that is used to store
homogeneous & hetrogeneous , Duplicate & unique
objects with out size limitation for carrying multiple
objects At a time from one application to another
application among multiple layers of MVC
architecture as method arguments and return type
Problem with Object[] to collect objects:
1.It allows us to store only same type of elements
2.It is fixed in size
3.Doesnot inbuilt methods for performing different
operations
3. All collection classes are defined in java.util package
Sun developed many collection classes among them 13 are important , they
are
ArrayList HashSet HashMap
Vector LinkedHashSet LinkedHashMap
Stack TreeSet TreeMap
LinkedList Hashtable
PriorityQueue Properties
Some helper/utility classes
Collections Scanner Locale Date
Arrays StringTockenizer ResourceBundle Calender
Random Currently GregoroanCale
der
4. We can collect objects in 2 ways
1.In array format- in this format object does not have identity
2.In (key , value) pair format- in this format object has identit
In java 1.0 version SUN only introduced 2 classes to collect
objects in above formats
a. Vector :It stores objects in array format
b. Hashtable: It stores objects in (key, value ) pair format
In java 1.2 version all collections are collectively called as
Collection framework
1.Collection hierarchy
2.Map hierarchy
5.
6. *
Collection hierarchy classes are divided into 3 categories-
Set,List, and Queue
Collection hierarchy classes we can collect unique and duplicate
objects in array format
Set is a unique collection , it does not allow duplicate elements
Set is the super interface for all unique collection class
List is duplicate collection, it allows duplicate elements
List is the super interface for all duplicate collection class
7. *
Set is an unsorted and unordered, means it stores elements
without index
SortedSet: is a sub interface of set that stores elements in
sorted order either in Ascending or descending order based on
the object’s natural sorting order
NavigableSet: is a sub interface of sortedSet .It is added in java
6 to add more navigation method to sorted set
8. *
In a List implimented classes Vector , ArrayList and LinkedList
are the class
ArrayList,Vector:It stores objects in array format
We choose these two classes to store elements in indexed order
and to retrieveThem radomly
Specific Functionalities of these two class:
1.Duplicate objects are not allowed in indexed order
2.Heterogeneous objects are allowed
3.Insertion order is preserved
4.Implemented data strcture is growable array
5.Null insertion is possible, more than one null is allowed
6.Initial capacity is 10 , incrementel capacity is double
9. Difference between Array list and Vector
Vector object is thread-safe: It means synchronized object-so
multiple threads Cannot modify this object concurrently.
ArrayList object is non threadsafe: it means it is not
synchronized object.
It is best suitable in multithread model application
Limitation of both classes:
Inserting and removing elements in middle is costlier operation
10. LinkkedList:
We must use linked list only when we have more operations
or inserting and
removing at beining or in the middle of collection
Linked list is not suitable for adding operations because it
consumes more memory gives less performance
It is not synchrinized object
It is a ordered collection , indexed order
It uses equals method in searching and removing elements
from collections
11. *
Queue is a root icomparator, or interface of all types
of queues
1.Priority Queues: which order elements according to
a supplied comparator, or the elements natural
ordering
2.LIFO queues : which ordered the elements LIFO
3.FIFOqueue: all new elements are inserted at the tail
of the queue
12. *
a stack is a group of objects that allows to restore
elements in last in first out manner
Stack is a legacy collection it is a subclass of vector
When we add objects to stack those objects are internally
added to vector in insertion order
Public E push(E item)
Public E pop()
Public E peck()
Public boolean empty()
Public int search (Object o)
13. *
In a map , key should be unique and values can be duplicated
Each (key , value) is called an entry . In map by default entries
are not sorted
SortedMap: is the sub interface of map
It sorts entries based on key natural sorting order
14. *
HashMap holds the data in the form of key-value pairs where
each key is associated with one value.
2) HashMap doesn’t allow duplicate keys. But it can have
duplicate values.
3) HashMap can have multiple null values and only one null key.
4) HashMap is not synchronized. To get the synchronized
HashMap, use Collections.synchronizedMap() method.
5) HashMap maintains no order.
6) HashMap gives constant time performance for the operations
like get() and put() methods.
7) Default initial capacity of HashMap is 16.
15. *
1) public V put(K key, V value)
This method inserts specified key-value mapping in the map. If map already has a
mapping for the specified key, then it rewrites that value with new value.
2) public void putAll(Map m)
This method copies all of the mappings of the map m to this map.
3) public V get(Object key)
This method returns the value associated with a specified key.
4) public int size()
This method returns the number of key-value pairs in this map.
5) public boolean isEmpty()
This method checks whether this map is empty or not.
16. 6) public boolean containsKey(Object key)
This method checks whether this map contains the mapping for the specified
key.
7) public boolean containsValue(Object value)
This method checks whether this map has one or more keys mapping to the
specified value.
8) public V remove(Object key)
This method removes the mapping for the specified key.
9) public void clear()
This method removes all the mappings from this map.
10) public Set<K> keySet()
This method returns the Set view of the keys in the map.
11) public Collection<V> values()
This method returns Collection view of the values in the map.
17. 12) public Set<Map.Entry<K, V>> entrySet()
This method returns the Set view of all the mappings in this map.
13) public V putIfAbsent(K key, V value)
This method maps the given value with specified key if this key is currently not
associated with a value or mapped to a null.
13) public boolean remove(Object key, Object value)
This method removes the entry for the specified key if this key is currently
mapped to a specified value.
14) public boolean replace(K key, V oldValue, V newValue)
This method replaces the oldValue of the specified key with newValue if the key is
currently mapped to oldValue.
15) public V replace(K key, V value)
This method replaces the current value of the specified key with new value.
18.
19. *
1.For storing objects in arrray format,we must choose
collection hierarchy classes
2.For storing objects in (key,value) pair format, we choose
map hierarchy classes
3.For storing only unique elements, we must choose set
implemented classes
4.For storing hetrogeneous unique elements without worring
storing order, we must Choose HashSet
5.For storing heterogeneous unique elements in insertion order
We must choose LinkedHashSet
6.For storing only unique elements in sorting order we must
Choose TreeSet
20. 7.For storing unique and/or duplicate elements in indexed
order, we must choose List implemented classes
8.For storing and retrieve duplicate elements in random access
we must choose List Implemented classes
9.For storing and retrieving elements in LIFO order we should
choose Stack
10.For storing unique entries , we must choose HashMap
11.For storing unique entries in sorting order , we must choose
Treemap
12.Hatable and HashMap both works similar, the only
difference is Hashtable is Threadsisafe
13.Properties class is used to store properties permanently
in file
21. public boolean isEmpty()
Public boolean add(Object o)
Public boolean addAll(Collection c)
Public boolean remove(Object obj)
Public boolean removeAll(Collection )
Public vlid clear()
Public boolean contains(Object o)
Public boolean containsAll(Object o)
Public boolean retains(Object o)
Public int size()
Public int hashCode()
Public Object[] toArray()
Public Object[] toArray(Object[] obj)
Public boolean equals(Object obj)