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

Lab - 8

The document contains two Java programs demonstrating the use of HashSet and Stack. The HashSet program illustrates adding and removing elements, handling duplicates, and checking the size, while the Stack program shows how to push and pop elements, check if the stack is empty, and search for an element. Key characteristics of HashSet and Stack are also summarized, including their behavior and methods.

Uploaded by

3atntv9hpx
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)
10 views4 pages

Lab - 8

The document contains two Java programs demonstrating the use of HashSet and Stack. The HashSet program illustrates adding and removing elements, handling duplicates, and checking the size, while the Stack program shows how to push and pop elements, check if the stack is empty, and search for an element. Key characteristics of HashSet and Stack are also summarized, including their behavior and methods.

Uploaded by

3atntv9hpx
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

Lab - 8

Student ID - AF0438946

Student Name – Lavanya R

Topic – Stack, HashSet

Question 1:

Write a Java Program to create HashSet.

Program:

import java.util.HashSet;

public class HashSetExample {

public static void main(String[] args) {

HashSet<String> str = new HashSet<String>();

str.add("lav"); //Adding elements in the Set using add() method

str.add("kar");

str.add("har");

str.add("sha");

str.add("swa");

str.add("ha");

System.out.println("The Elements are: " +str);


System.out.println("");

str.add("lav"); // Trying to add the duplicate element in the Set

System.out.println("After Adding Another Element : " +str);


System.out.println("");

str.add(null);

System.out.println("The Elements are: "+str);


System.out.println("");
System.out.println("Size of HashSet: "+str.size()); // Finding size of the Set Using
size() method
System.out.println("");

str.remove("lav"); // Removing an Element from the Set

System.out.println("After removing an element: "+str);


System.out.println("");

System.out.println("Size of HashSet: "+str.size());

HashSet:

HashSet is a class in collection framework, which doesn’t maintain order.

 No duplicates
 Allow null value
 Non – synchronized
 UnIndex

Output:
Question 2:

Write a program to declare Stack. Store 10 elements into it. Remove 4 elements from Stack and display it.

Program:

import java.util.Stack;

public class StackExample {


public static void main(String[] args) {

Stack<Integer> st = new Stack<>();

boolean check_stack_status = st.empty(); // Checking the status of the Stack(is empty or


not?)

System.out.println("Stack is Empty : "+check_stack_status);

st.push(10); // Pushing the elements into the Stack using push() method

st.push(20);

st.push(30);

st.push(40);

st.push(50);

st.push(60);

st.push(70);

st.push(80);

st.push(90);

st.push(100);

System.out.println("Elements:"+ st);

System.out.println();

st.pop(); // Deleting the elements from the Stack using pop() method

st.pop();

st.pop();

st.pop();
System.out.println("Elements:"+ st);

System.out.println("Peek Element:"+ st.peek());

int result=st.search(300);

System.out.println(result);

if (result==-1)

System.out.println(" Element is not Found");

else

System.out.println("Element is Found");

}
Stack:

 Stack is a built-in class. It follows LIFO (Last-In-First-Out) concept.


 It is a Single pointer operation. Using push() and pop() method we can add and remove the elements
in the Stack.

Output:

You might also like