OOP 6 Handout
OOP 6 Handout
1
SLIIT - Faculty of Computing
Collections in Java
The Collections Framework is a sophisticated hierarchy of
interfaces and classes that provide state-of-art technology for
managing groups of objects.
Java Reference Book 09: Page 497
1
3/11/18
Collection Interface
• Declaration:
• Interface Collection <E>
• E: specifies the type of objects that collection will hold
Method Description
public boolean add(Object element) is used to insert an element in this collection.
public boolean addAll(Collection c) is used to insert the specified collection elements in the invoking
collection.
public boolean remove(Object element) is used to delete an element from this collection.
public boolean removeAll(Collection c) is used to delete all the elements of specified collection from the
invoking collection.
public boolean retainAll(Collection c) is used to delete all the elements of invoking collection except
the specified collection.
public int size() return the total number of elements in the collection.
public void clear() removes the total no of element from the collection.
public boolean contains(Object element) is used to search an element.
public boolean containsAll(Collection c) is used to search the specified collection in this collection.
public Iterator iterator() returns an iterator.
public Object[] toArray() converts collection into array.
public boolean isEmpty() checks if collection is empty.
public boolean equals(Object element) matches two collection.
5
public int hashCode() returns the hashcode number for collection.
SLIIT - Faculty of Computing
List Interface
q Java.util.List is a child interface of Collection.
Queue Interface
q The java.util.Queue is a subtype of java.util.Collection interface.
2
3/11/18
Set Interface
ArrayList Class
q ArrayList class extends the AbstractList class and implements
the List interface.
q ArrayList support dynamic arrays (can increase and decrease
size dynamically).
q ArrayList constructors;
§ ArrayList() – Creates an empty array list.
§ ArrayList (Collection <? Extends > c) - Creates an array list that is
initializd with the elements of the collection of c.
§ ArrayList (int capacity) – Creates an array list with initial capacity. The
capacity is the underlying array that is used to store the elements. The
capacity grows as elements are added to the array list.
Example : ArrayList
Ref: Java Complete Reference Pg: 512
ArrayListDemo.java
3
3/11/18
10
class ArrayListToArray {
public static void main(String args[]) { // Create an array list.
ArrayList<Integer> al = new ArrayList<Integer>();
int sum = 0;
ArrayListToArray.java
// Sum the array.
for(int i =0 ; i< ia.length ; i++)
sum += i;
} 12
4
3/11/18
HashSet Class
q HashSet extends AbstractSet and implements the Set interface.
q It creates a collection that uses a hash table for storage.
q In hashing, the informational content of a key is used to determine a
unique value, called its hash code. The hash code is then used as the index
at which the data associated with the key is stored. The transformation of
the key into its hash code is performed automatically
q HashSet constructors;
• HashSet( ) - Default hash set.
• HashSet(Collection<? extends E> c) - Initializes the hash set by using the
elements of c
• HashSet(int capacity) - Initializes the capacity of the hash set to capacity
• HashSet(int capacity, float fillRatio) - Initializes both the capacity and the fill ratio
(also called load capacity ) of the hash set. The fill ratio must be between 0.0 and
1.0, and it determines how full the hash set can be before it is resized upward.13
Example : HashSet
Ref: Java Complete Reference Pg: 517
import java.util.HashSet;
System.out.println(hs);
}
}
14
q Both keys and values are objects. The keys must be unique, but
the values may be duplicated.
q Some maps can accept a null key and null values, others cannot.
15
5
3/11/18
HashMap Class
q The HashMap class extends AbstractMap and implements the Map
interface. It uses a hash table to store the map.
q HashMap is a generic class that has this declaration:
class HashMap<K, V>
K specifies the type of keys, and V specifies the type of values.
q HashMap constructors
§ HashMap( ) - Default hash map
§ HashMap(Map<? extends K, ? extends V> m) - Initializes the hash map by
using the elements of m
§ HashMap(int capacity) - Third form initializes the capacity of the hash map to
capacity. The default capacity is 16
§ HashMap(int capacity, float fillRatio) - Initializes both the capacity and fill
ratio of the hash map by using its arguments. The meaning of capacity and fill
17
ratio is the same as for HashSet. The default fill ratio is 0.75
SLIIT - Faculty of Computing
Example : HashMap
Ref: Java Complete Reference Pg: 537
public class HashMapDemo {
public static void main(String args[]) {
// Create a hash map.
HashMap<String, Double> hm = new HashMap<String, Double>();
6
3/11/18
Reference
q Java T- Point:
https://www.javatpoint.com/collections-in-java
19