[go: up one dir, main page]

0% found this document useful (0 votes)
16 views28 pages

Unit 2 Collection

The document discusses key Java data structures including Iterators, Vectors, and Hashing. It explains how Iterators allow for sequential traversal of collections, Vectors function as dynamic arrays with synchronization, and hashing techniques manage key-value pairs while addressing collisions. Additionally, it outlines methods for implementing hashing in Java, including Hashtable, HashMap, and LinkedHashMap.

Uploaded by

Tanmay Vaij
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views28 pages

Unit 2 Collection

The document discusses key Java data structures including Iterators, Vectors, and Hashing. It explains how Iterators allow for sequential traversal of collections, Vectors function as dynamic arrays with synchronization, and hashing techniques manage key-value pairs while addressing collisions. Additionally, it outlines methods for implementing hashing in Java, including Hashtable, HashMap, and LinkedHashMap.

Uploaded by

Tanmay Vaij
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Java Iterator

• An Iterator in Java is an interface used to traverse elements in a Collection


sequentially.
• It provides methods like hasNext(), next(), and remove() to loop through
collections and perform manipulation.
• An Iterator is a part of the Java Collection Framework, and we can use it with
collections like ArrayList, LinkedList, and other classes that implement the
Collection interface.
public class Demo
{
public static void main(String[] args)
{
List<String> names = new LinkedList<>();
names.add("aaa");
names.add("bbb");
names.add("ccc");

ListIterator<String> li = names.listIterator();

while (li.hasNext())
System.out.println(li.next());

// for-each loop creates Internal Iterator here.


for (String s : names)
System.out.println(s);
}
}
Vector
● Vector is like the dynamic array which can grow or shrink its size.
● We can store n-number of elements in it as there is no size limit.
● Found in the java.util package and implements the List interface
Vector is similar to array, but with 2 differences:
○ Vector is synchronized.
○ Java Vector contains many legacy methods that are not the part of a collections framework.
add(int index, Object o) It is used to append the specified element in the given vector.

addAll(Collection C) It is used to append all of the elements in the specified collection to the end of this
Vector.
addElement(Object O) It is used to append the specified component to the end of this vector. It increases
the vector size by one.
capacity() It is used to get the current capacity of this vector.

clear() It is used to delete all of the elements from this vector.

clone() It returns a clone of this vector.


contains(Object ele) It returns true if the vector contains the specified element.

containsAll(Collection C) It returns true if the vector contains all of the elements in the specified collection.

copyInto(Object[] arr) It is used to copy the components of the vector into the specified array.

elementAt(inr index) It is used to get the component at the specified index.

elements() It returns an enumeration of the components of a vector.


Hashing in Java
● Contains hash function that maps keys to some values.
● If two or more keys are mapped to same value, lead to collision.
● Chain hashing avoids collision.
● The idea is to make each cell of hash table point to a linked list of
records that have same hash function value.
● To add a node to the hash table, we need to find the hash index for
the specified key.
hashIndex = key % noOfBuckets
● Insert: Move to the bucket corresponds to the calculated hash index
and insert the new node at the end of the list.
● Delete: Calculate the hash index for the key, move to the bucket
corresponds to the calculated hash index, search the list in the
current bucket to find and remove the node with the given key (if
found).
Methods for Implementing hashing in Java
1. HashTable-based Method(A synchronised implementation of hashing)

2. HashMap-based Method (A non-synchronized faster implementation of hashing)

3. LinkedHashMap-based method(Similar to HashMap, but keeps order of elements)

You might also like