0% found this document useful (0 votes)
7 views4 pages

Lab Collection Class ArrayList HashMap

The document demonstrates the use of ArrayList and HashMap in Java, covering their features, methods, and practical implementations. ArrayList allows dynamic storage of elements with indexed access, while HashMap stores data as key-value pairs for fast retrieval. Both collections provide flexibility and efficiency for data manipulation in Java programming.

Uploaded by

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

Lab Collection Class ArrayList HashMap

The document demonstrates the use of ArrayList and HashMap in Java, covering their features, methods, and practical implementations. ArrayList allows dynamic storage of elements with indexed access, while HashMap stores data as key-value pairs for fast retrieval. Both collections provide flexibility and efficiency for data manipulation in Java programming.

Uploaded by

Nisha Rathi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Collection Class: ArrayList in Java

AIM
To write a Java program to demonstrate the use of ArrayList, including adding, accessing, modifying,
removing elements, and traversing the list.

THEORY
ArrayList is a part of the Java Collection Framework and is present in the [Link] package.
It is a resizable (dynamic) array, which means its size increases or decreases automatically when elements
are added or removed.
Features of ArrayList
 Stores elements dynamically
 Maintains insertion order
 Allows duplicate values
 Provides indexed access to elements
 Offers methods such as add(), get(), set(), remove(), size()
Commonly used methods
Method Description
add() Add element to the list
get() Returns element at given index
set() Updates value at index
remove() Removes element
size() Returns number of elements
for-each loop Traverse all elements
ArrayList is widely used because it provides flexibility, easy insertion/removal, and powerful built-in
methods.

PROGRAM
import [Link];

public class ArrayListExample


{
public static void main(String[] args)
{

// Creating an ArrayList of Strings


ArrayList<String> fruits = new ArrayList<>();

// Adding elements
[Link]("Apple");
[Link]("Banana");
[Link]("Mango");
[Link]("Orange");

// Displaying the ArrayList


[Link]("Fruits List: " + fruits);

// Accessing elements
[Link]("First fruit: " + [Link](0));

// Modifying an element
[Link](1, "Blueberry");
[Link]("After modification: " + fruits);

// Removing an element
[Link]("Mango");
[Link]("After removal: " + fruits);

// Looping through the ArrayList


[Link]("\nUsing for-each loop:");
for (String fruit : fruits) {
[Link](fruit);
}

// Checking size
[Link]("\nTotal fruits: " + [Link]());
}
}

Output:

Fruits List: [Apple, Banana, Mango, Orange]


First fruit: Apple

After modification:
[Apple, Blueberry, Mango, Orange]

After removal:
[Apple, Blueberry, Orange]

Using for-each loop:


Apple
Blueberry
Orange

Total fruits: 3

CONCLUSION
In this practical, we successfully implemented and used an ArrayList in Java.
We learned how to add, access, modify, remove, and traverse elements in an ArrayList.
This demonstrates the flexibility of Java’s dynamic collections and how they make storing and manipulating
data easier compared to static arrays.
Collection Class: HashMap in Java
AIM
To write a Java program to demonstrate the use of HashMap, including adding key–value pairs, accessing
values, modifying entries, removing entries, and traversing the HashMap.

THEORY
A HashMap in Java is a part of the Java Collection Framework (package [Link]).
It stores data in the form of key–value pairs and uses hashing for fast access.
Features of HashMap
 Stores data as key → value
 Allows one null key and multiple null values
 Keys must be unique
 No guaranteed order (not sorted)
 Very fast operations: O(1) average time for put and get
Commonly used methods
Method Description
put(key, value) Add or update value
get(key) Return value for key
remove(key) Remove entry by key
containsKey(key) Check if key exists
keySet() Returns all keys
values() Returns all values
entrySet() Returns all key–value pairs
HashMap is commonly used for fast lookup tasks like dictionaries, indexing, caching, etc.
PROGRAM
import [Link];

public class HashMapExample


{
public static void main(String[] args)
{

// Creating a HashMap
HashMap<Integer, String> students = new HashMap<>();

// Adding key-value pairs


[Link](101, "Rahul");
[Link](102, "Neha");
[Link](103, "Amit");
[Link](104, "Pooja");

// Displaying the HashMap


[Link]("Students Map: " + students);

// Accessing a value using key


[Link]("Name of student with ID 102: " + [Link](102));
// Modifying a value
[Link](103, "Amit Sharma");
[Link]("After modification: " + students);

// Removing an entry
[Link](104);
[Link]("After removal: " + students);

// Looping through HashMap


[Link]("\nUsing for-each loop (key-value pairs):");
for ([Link]<Integer, String> entry : [Link]()) {
[Link]([Link]() + " -> " + [Link]());
}

// Checking size
[Link]("\nTotal students: " + [Link]());
}
}

OUTPUT

Students Map: {101=Rahul, 102=Neha, 103=Amit, 104=Pooja}


Name of student with ID 102: Neha

After modification:
{101=Rahul, 102=Neha, 103=Amit Sharma, 104=Pooja}

After removal:
{101=Rahul, 102=Neha, 103=Amit Sharma}

Using for-each loop (key-value pairs):


101 -> Rahul
102 -> Neha
103 -> Amit Sharma

Total students: 3

CONCLUSION
In this practical, we successfully implemented and used a HashMap in Java.
We learned how to insert, access, modify, remove, and iterate through key–value pairs.
HashMap provides fast lookup and flexible storage of data where each value is associated with a unique
key.
It is widely used for indexing, caching, and storing structured data.

You might also like