0% found this document useful (0 votes)
3 views63 pages

Chapter One Java Collections 2(1)

Chapter One introduces Java Collections, focusing on iterators, vectors, hash sets, and maps. It explains the functionality and differences between ArrayList and Vector, highlighting that Vector is synchronized while ArrayList is not, making the latter more efficient. Additionally, it covers the concept of HashSet for unique elements and the Map interface for storing key-value pairs, emphasizing the dynamic nature of HashMap.

Uploaded by

merir143
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
3 views63 pages

Chapter One Java Collections 2(1)

Chapter One introduces Java Collections, focusing on iterators, vectors, hash sets, and maps. It explains the functionality and differences between ArrayList and Vector, highlighting that Vector is synchronized while ArrayList is not, making the latter more efficient. Additionally, it covers the concept of HashSet for unique elements and the Map interface for storing key-value pairs, emphasizing the dynamic nature of HashMap.

Uploaded by

merir143
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 63

Chapter One:

Introduction to Java Collections


What is an iterator?
• An Iterator is an object that can be used to loop
through collections, like ArrayList and HashSet.
• It is called an "iterator" because "iterating" is the
technical term for looping.

• In Java, iteration is a technique used to sequence


through a block of code repeatedly until a specific
condition either exists or no longer exists.
• Iterations are a very common approach used with
loops.
what is java iterator interface?
• The Iterator interface of the Java collections
framework allows us to access elements of a
collection.
• It has a subinterface ListIterator .
• All the Java collections include an iterator() method.
• This method returns an instance of iterator used to
iterate over elements of collections.
What is vector in java
• The Vector class is an implementation of the
List interface that allows us to create
resizable-arrays similar to the ArrayList class.
• A vector is similar to a dynamic array whose
size can be increased or decreased. Unlike
arrays, it has no size limit and can store any
number of elements. Since Java 1.2, it has
been a part of the Java Collection framework.
• A Vector is part of the Java Collections Framework
and is similar to an array, but with two key
differences: it can dynamically grow or shrink in size,
and it is synchronized.
• What is the difference between list and vector in
Java?
• only one threads are allowed . Significant Differences
between ArrayList and Vector: Synchronization:
Vector is synchronized, which means only one
thread at a time can access the code, while ArrayList
is not synchronized, which means multiple threads
can work on ArrayList at the same time.
• Java Vector vs. ArrayList (continued..)
• when one thread is accessing a vector, and at the
same time another thread tries to access it, an
exception
called ConcurrentModificationException is
generated. Hence, this continuous use of lock for
each operation makes vectors less efficient.
• However, in array lists, methods are not
synchronized. Instead, it uses
the Collections.synchronizedList() method that
synchronizes the list as a whole.
• Note: It is recommended to use ArrayList in place
of Vector because vectors less efficient.
Creating a Vector
• Here is how we can create vectors in Java.
• Vector<Type> vector = new Vector<>();
• Here, Type indicates the type of a linked list.
• For example,
• // create Integer type linked list
• Vector<Integer> vector= new Vector<>();
• // create String type linked list
• Vector<String> vector= new Vector<>();
• Add Elements to Vector
• add(element) - adds an element to vectors
• add(index, element) - adds an element to the
specified position
• addAll(vector) - adds all elements of a vector
to another vector
• For example,
• import java.util.Vector; class Main { public static
void main(String[] args) { Vector<String>
mammals= new Vector<>(); // Using the
add() method mammals.add("Dog");
mammals.add("Horse"); // Using index
number mammals.add(2, "Cat");
System.out.println("Vector: " + mammals); //
Using addAll() Vector<String> animals = new
Vector<>(); animals.add("Crocodile");
animals.addAll(mammals);
System.out.println("New Vector: " + animals); }}
Hash Set?
A "Hash Set" is a data structure that stores
unique elements using a hashing
technique, allowing for very fast lookups,
insertions, and deletions by converting
each element into a unique hash code,
which is used to efficiently store and
access the data within the set;
essentially, it's a collection where each
element can only appear once and can be
quickly identified based on its hash value.
A Hash Set is a form of Hash Table
data structure that usually holds a
large number of elements.
Using a Hash Set we can search,
add, and remove elements really
fast.
Hash Sets are used for lookup, to
check if an element is part of a set.
What is Map in java?
Map in Java is an interface available in java. util package and it stores the data
in key and value pairs. It does not allow duplicate keys.

A Map in Java is an object that maps keys to values and is designed for faster
lookups. Data is stored in key-value pairs and every key is unique. Each key
maps to a value hence the name map. These key-value pairs are called map
entries.

HashMap is a data structure that uses the Map interface and a hash table for
storing key-value pairs.
It's a widely used data structure in Java that provides efficient access and
manipulation of data based on unique keys.

Difference between Map and hashmap?

HashMap is a dynamic form of Map, whereas Map is a static type of Map. This
implies that the compiler will treat your Map object as if it were of type Map,
even though it may point to any of its subtypes at runtime.

You might also like