0% found this document useful (0 votes)
2 views33 pages

Java Unit 4

The document provides examples of various Java collections including ArrayList, LinkedList, Stack, Queue, HashSet, LinkedHashSet, TreeSet, and HashMap. Each example demonstrates how to create the collection, add, remove, and access elements, as well as display the contents. The outputs of each example are also included, showcasing the results of the operations performed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views33 pages

Java Unit 4

The document provides examples of various Java collections including ArrayList, LinkedList, Stack, Queue, HashSet, LinkedHashSet, TreeSet, and HashMap. Each example demonstrates how to create the collection, add, remove, and access elements, as well as display the contents. The outputs of each example are also included, showcasing the results of the operations performed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

import java.util.

ArrayList;

public class ArrayListExample {


public static void main(String[] args) {
// Creating an ArrayList of Strings
ArrayList<String> fruits = new ArrayList<>();

// Adding elements to the ArrayList


fruits.add("Apple");
fruits.add("Banana");
fruits.add("Mango");
fruits.add("Orange");

// Displaying the ArrayList


System.out.println("Fruits List: " + fruits);

// Accessing an element
System.out.println("First fruit: " + fruits.get(0));

// Removing an element
fruits.remove("Banana");
System.out.println("After removing Banana: " + fruits);

// Updating an element
fruits.set(1, "Grapes");
System.out.println("After updating: " + fruits);

// Looping through elements


System.out.println("All fruits:");
for (String fruit : fruits) {
System.out.println(fruit);
}
}
}
OUTPUT
Fruits List: [Apple, Banana, Mango, Orange]
First fruit: Apple
After removing Banana: [Apple, Mango, Orange]
After updating: [Apple, Grapes, Orange]
All fruits:
Apple
Grapes
Orange
import java.util.LinkedList;
public class LinkedListExample {
public static void main(String[] args) {
// Creating a LinkedList of Strings
LinkedList<String> animals = new LinkedList<>();
// Adding elements to the LinkedList
animals.add("Dog");
animals.add("Cat");
animals.add("Cow");
animals.add("Horse");
// Displaying the LinkedList
System.out.println("Animals List: " + animals);
// Adding element at the beginning and end
animals.addFirst("Elephant");
animals.addLast("Goat");
System.out.println("After adding first and last: " + animals);
// Removing an element
animals.remove("Cat");
System.out.println("After removing Cat: " + animals);
// Accessing an element
System.out.println("First animal: " + animals.getFirst());
// Looping through elements
System.out.println("All animals:");
for (String animal : animals) {
System.out.println(animal);
}
}
}
OUTPUT
Animals List: [Dog, Cat, Cow, Horse]
After adding first and last: [Elephant, Dog, Cat, Cow, Horse, Goat]
After removing Cat: [Elephant, Dog, Cow, Horse, Goat]
First animal: Elephant
All animals:
Elephant
Dog
Cow
Horse
Goat
import java.util.Stack;
public class StackExample {
public static void main(String[] args) {
// Creating a Stack of Strings
Stack<String> books = new Stack<>();
// Pushing elements onto the Stack
books.push("Math");
books.push("Science");
books.push("History");
// Displaying the Stack
System.out.println("Stack: " + books);
// Peeking the top element
System.out.println("Top element: " + books.peek());
// Popping an element
books.pop();
System.out.println("After pop: " + books);
// Checking if stack is empty
System.out.println("Is stack empty? " + books.isEmpty());
// Iterating through the Stack
System.out.println("All elements:");
for (String book : books) {
System.out.println(book);
}
}
}
OUTPUT
Stack: [Math, Science, History]
Top element: History
After pop: [Math, Science]
Is stack empty? false
All elements:
Math
Science
import java.util.Queue;
import java.util.LinkedList;

public class QueueExample {


public static void main(String[] args) {
// Creating a Queue of Strings
Queue<String> tasks = new LinkedList<>();

// Adding elements to the Queue (Enqueue)


tasks.add("Task1");
tasks.add("Task2");
tasks.add("Task3");

// Displaying the Queue


System.out.println("Queue: " + tasks);

// Accessing the front element without removing (Peek)


System.out.println("Front of queue: " + tasks.peek());

// Removing elements from the Queue (Dequeue)


tasks.poll(); // removes "Task1"
System.out.println("After poll: " + tasks);

// Checking if queue is empty


System.out.println("Is queue empty? " + tasks.isEmpty());

// Iterating through the Queue


System.out.println("Remaining tasks:");
for (String task : tasks) {
System.out.println(task);
} OUTPUT
Queue: [Task1, Task2, Task3]
Front of queue: Task1
After poll: [Task2, Task3]
is queue empty? false
Remaining tasks:
Task2
Task3

}
}
import java.util.HashSet;

public class HashSetExample {


public static void main(String[] args) {
// Creating a HashSet of Strings
HashSet<String> cities = new HashSet<>();

// Adding elements to the HashSet


cities.add("Delhi");
cities.add("Mumbai");
cities.add("Chennai");
cities.add("Kolkata");

// Trying to add a duplicate element


cities.add("Mumbai"); // Will not be added

// Displaying the HashSet


System.out.println("Cities Set: " + cities);

// Checking if an element exists


System.out.println("Contains Delhi? " + cities.contains("Delhi"));

// Removing an element
cities.remove("Chennai");
System.out.println("After removing Chennai: " + cities);

// Iterating through the HashSet


System.out.println("All cities:");
for (String city : cities) {
System.out.println(city);
}
}
}
OUTPUT
Cities Set: [Kolkata, Mumbai, Delhi, Chennai]
Contains Delhi? true
Ater removing Chennai: [Kolkata, Mumbai, Delhi]
All cities:
Kolkata
Mumbai
Delhi
import java.util.LinkedHashSet;

public class LinkedHashSetExample {


public static void main(String[] args) {
// Creating a LinkedHashSet of Strings
LinkedHashSet<String> languages = new LinkedHashSet<>();
// Adding elements to the LinkedHashSet
languages.add("Java");
languages.add("Python");
languages.add("C++");
languages.add("JavaScript");
// Trying to add a duplicate element
languages.add("Python"); // Will not be added
// Displaying the LinkedHashSet
System.out.println("Programming Languages: " + languages);
// Checking for an element
System.out.println("Contains Java? " + languages.contains("Java"));
// Removing an element
languages.remove("C++");
System.out.println("After removing C++: " + languages);
// Iterating through the LinkedHashSet
System.out.println("All languages:");
for (String lang : languages) {
System.out.println(lang);
}
}
}
OUTPUT
Programming Languages: [Java, Python, C++, JavaScript]
Contains Java? true
After removing C++: [Java, Python, JavaScript]
All languages:
Java
Python
JavaScript
import java.util.SortedSet;
import java.util.TreeSet;

public class TreeSetExample {


public static void main(String[] args) {
// Creating a SortedSet using TreeSet
SortedSet<String> countries = new TreeSet<>();

// Adding elements (automatically sorted)


countries.add("India");
countries.add("Australia");
countries.add("USA");
countries.add("Germany");

// Attempt to add duplicate


countries.add("India"); // Duplicate, will not be added OUTPUT
Countries: [Australia,
// Displaying the TreeSet (sorted order) Germany, India, USA]
System.out.println("Countries: " + countries); First Country: Australia
Last Country: USA
// Getting first and last elements
After removing Germany:
System.out.println("First Country: " + countries.first());
[Australia, India, USA]
System.out.println("Last Country: " + countries.last());
All countries:
// Removing an element Australia
countries.remove("Germany"); India
System.out.println("After removing Germany: " + countries); USA

// Iterating through the TreeSet


System.out.println("All countries:");
for (String country : countries) {
System.out.println(country);
}
}
}
import java.util.Map;
import java.util.HashMap;
public class HashMapExample {
public static void main(String[] args) {
// Creating a Map using HashMap
Map<Integer, String> studentMap = new HashMap<>();
// Adding key-value pairs to the map
studentMap.put(101, "Alice");
studentMap.put(102, "Bob");
studentMap.put(103, "Charlie");
// Displaying the map
System.out.println("Student Map: " + studentMap);
// Accessing a value by key
System.out.println("Student with ID 102: " + studentMap.get(102));
// Removing an entry by key
studentMap.remove(101);
System.out.println("After removing ID 101: " + studentMap);
// Checking if a key exists
System.out.println("Contains key 103? " + studentMap.containsKey(103));
// Iterating through the map
System.out.println("All students:");
for (Map.Entry<Integer, String> entry : studentMap.entrySet()) {
System.out.println("ID: " + entry.getKey() + ", Name: " + entry.getValue());
}
}
}

OUTPUT
Student Map: {101=Alice, 102=Bob, 103=Charlie}
Student with ID 102: Bob
After removing ID 101: {102=Bob, 103=Charlie}
Contains key 103? true
All students:
ID: 102, Name: Bob
ID: 103, Name: Charlie

You might also like