Overview of Java Arraylist, Hashtable, Hashmap, Hashet, Linkedlist
Overview of Java Arraylist, Hashtable, Hashmap, Hashet, Linkedlist
HashTable, HashMap,
Hashet,LinkedList
This article discusses the main classes of Java Collection API. The following figure
demonstrates the Java Collection framework.
ArrayList
HashTable
HashMap
HashSet
LinkedList
First we will understand each of them, their respective classes and methods. Then we shall try
out examples for each type.
ArrayList in Java
ArrayList is a collection API used for storing elements using dynamic array. The dynamic
array is an array in which the array size is not fixed in advance. Therefore, we can change the
size of an array at run time using the ArrayList class. When we store elements into ArrayList,
depending on the number of elements, the memory is allotted and re-allotted to accommodate
all the elements. Every instance of the ArrayList class is allowed to store a set of elements in
the list.
Some of the important points about ArrayList class shown below :
ArrayList class extends AbstractList. It also implements the List interface. It is generic class
which has declaration as follows :
class ArrayList<E>
Method Description
void add(int position, It inserts specified element at the specified position in the
element obj) ArrayList.
boolean add(element obj) It appends specified element to the end of the ArrayList.
boolean addAll(Collection It appends all the elements of the collection to the end of the
c) ArrayList.
element remove(int It removes specified element at the specified position in the
position) ArrayList.
It removes first occurrence of specified element obj from the
boolean remove(object obj)
ArrayList.
void clear() It removes all the elements from the ArrayList.
boolean contains(Object o) It returns true if ArrayList contains the specified element .
object get(int position) It returns the element at the specified position in the ArrayList.
It returns first occurrence of the specified element in the list or -
int indexOf(Object o)
1 if element not found in the list.
It returns the last occurrence of the specified element in the list
int lastIndexOf(Object o)
or -1 if the element is not found in the list.
int size() It returns the number of elements in the list.
package mrbool.com;
import java.util.ArrayList;
import java.util.Iterator;
al.add("apple");
al.add("mango");
al.add("grapes");
al.add("orange");
System.out.println("Contents are:" + al);
al.remove(2);
System.out.println("After removing contents are:" + al);
System.out.println("size of array list:" + al.size());
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
}
Output:
HashTable
A HashTable is an array of the list. It was part of the java.util and is extended in the
Dictionary class. HashTable was re-engineered to implement the Map interface. HashTable is
similar to HashMap which can store elements in the form of key-value pairs and it is
synchronized. It contains unique elements and neither the keys nor the values can be null.
We can write HashTable as:
Method Description
It resets and removes all the key value pairs from the
void clear()
HashTable.
Object clone() It returns duplicate of invoking object.
boolean contains (object It returns true if the value that is equal to the value parameter
value) exists and returns false if value is not found
boolean containsKey It returns true if the key that is equal to the key parameter exists
(object key) and returns false if key is not found
boolean containsValue It returns true if the value that is equal to value parameter exists
(object value) and returns false if value is not found
It returns enumeration of the values in hash table./td>
Enumeration elements
It returns the object associated with key and it returns null if the
V get(Object key)
key is not in hash table.
boolean isEmpty() It returns true if there are no key value pairs in the hash table.
Enumeration keys() It returns enumeration of the keys in hash table.
V put(K key, V value) It inserts key and value in the hash table.
It removes key and corresponding value from hash table. It
V remove(Object key)
returns null if key is not in the hash table.
int size() It returns number of key value pairs in the hash table.
String to String It returns string equivalent of hash table.
Example using Hashtable:
package mrbool.com;
import java.util.Enumeration;
import java.util.Hashtable;
}
}
Output:
player 3:dhoni
player 2:sehwag
player 1:sachin
HashMap
HashMap is a collection which stores key-value pairs, where key is a reference and value is
the actual data. HashMap extends AbstractMap and implements the Map interface. It uses
hash table to store the map. It contains unique elements. We cannot use duplicate data for
keys in the HashMap.
Here, K specifies the type of keys and V specifies the type of values.
Method Description
It resets and removes all the key value pairs from the
void clear()
HashMap.
Object clone() It returns duplicate copy of HashMap instance.
boolean containsKey (object
It returns true if map contains mapping for the specified key.
key)
boolean containsValue It returns true if map maps one or more keys to the specified
(object value) value.
It returns true if collection of mappings found this map.< /td>
Set enrtySet()
It returns the value if specified key is mapped or null if it
Object get(Object key)
contains no mapping for the value.
boolean isEmpty() It returns true if there are no entries in the hash map.
Set keySet It returns set of keys in map object.
Object put(Object key,
it stores key-value pairs into the hash map.
Object value)
putAll(map m) Copy all the mappings to another map.
it removes the key and corresponding value from the
Object remove(object key)
Hashmap.
int size() It returns number of key value pairs in the hash map.
Collection values() It returns collection view of map values.
Example using HashMap:
package mrbool.com;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
while (it.hasNext()) {
Map.Entry m = (Map.Entry)
it.next();
}
}
Output:
102 : gambir
100 : sachin
101 : sehwag
HashSet
A HashSet represents a set of elements. It is available with collection package and extends
AbstractSet and implements the Set interface. The HashSet class is used to create a
collection and store it in a hash table. It does not guarantee the order of elements. It does not
allow the duplicate elements to be stored.
class HashSet<T>
Here, T represents the generic type constructor. It represents which types of elements are
being stored into the HashSet.
We can create object as follows:
HashSet methods:
Method Description
void clear() It removes all the elements from the HashSet.
Object clone() It returns duplicate copy of HashSet instance.
boolean contains
It returns true if the set contains the specified element.
(object obj)
boolean isEmpty() It returns true if there are no elements in the hash set.
Object remove(object It removes the element from the HashSet, if it is present. It returns
key) true if element is removed successfully, otherwise false.
int size() It returns the number of elements present in the HashSet.
Example:
package mrbool.com;
import java.util.HashSet;
import java.util.Iterator;
System.out.println("Elements using
iterator:");
Iterator it = hs.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
Output:
LinkedList
class LinkedList<E>
Method Description
void add(int position, It inserts specified element at the specified position in the
element obj) LinkedList.
boolean add(element obj) It adds specified element to the end of the LinkedList.
boolean addAll(Collection It appends all the elements of the collection to the end of the
c) LinkedList.
boolean addAll(int index, It inserts all the elements of the collection into the LinkedList,
Collection c) starting at the specified position.
element remove(int It removes specified element at the specified position in the
position) LinkedList.
It removes first occurrence of specified element obj from the
boolean remove(object obj)
LinkedList.
void clear() It removes all the elements from the LinkedList.
boolean contains(Object o) It returns true if LinkedList contains specified element .
void addFirst(Object o) It inserts the element at the first position of the linked list.
void addLast(Object o) It appends the specified element to the end of the linked list.
Object get(int index) It returns the element at specified position in the LinkedList.
Object getFirst() It returns the first element in the LinkedList.
Object getLast() It returns the last element in the LinkedList.
It returns the first occurrence of the specified element in the
int indexOf(Object o)
list or -1 if the element is not found in the list.
It returns the last occurrence of the specified element in the list
int lastIndexOf(Object o)
or -1 if the element is not found in the list.
int size() It returns the number of elements in the linked list.
ListIterator listiterator(int It returns a list iterator of the elements in the list starting at
index) specified position in the list.
It removes the element in the list at the specified position.
Object remove(int index)
Object removeFirst() It removes the first element from the linked list.
Object removeLast() It removes the last element from the linked list.
Object set(int index, Object It replaces the element at the specified position in the list with
element) the specified element .
It coverts linked list into an array of Object class type. All the
Object[] toArray()
elements in the list are stored in correct order.
Object[] toArray(Object [] It returns all the elements in the list are stored in correct order;
a) The run type of returned array is that of the specified array.
Listing 9: LinkedListDemo.java
package mrbool.com;
import java.util.Iterator;
import java.util.LinkedList;
Iterator it = ll.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
Output:
India
America
China
India
Conclusion