0% found this document useful (0 votes)
30 views7 pages

10 A, B, C

program

Uploaded by

durgabhavani60
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)
30 views7 pages

10 A, B, C

program

Uploaded by

durgabhavani60
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
You are on page 1/ 7

10 b)

import java.util.Hashtable;
import java.util.Scanner;

class HashTableRemoveDuplicateKeys {
public static void main(String... args) {
// Create a Hashtable to store the data
Hashtable<Integer, String> ht = new Hashtable<>();
Scanner scanner = new Scanner(System.in);

// Prompt the user to enter the number of elements they want to insert
System.out.println("How many elements do you want to insert?");
int size = scanner.nextInt();
scanner.nextLine(); // Consume the newline character

// Inserting data dynamically into the Hashtable


for (int i = 0; i < size; i++) {
System.out.println("Enter Student ID (Key): ");
int studentId = scanner.nextInt();
scanner.nextLine(); // Consume the newline character

// Check if the key already exists in the Hashtable


if (ht.containsKey(studentId)) {
System.out.println("Duplicate key found! The student ID already exists.");
} else {
System.out.println("Enter Student Name (Value): ");
String studentName = scanner.nextLine();
ht.put(studentId, studentName); // Insert the key-value pair
System.out.println("Student added successfully!");
}
}

// Printing the elements of the Hashtable after all insertions


System.out.println("\nFinal Hashtable (without duplicates): ");
printHashtable(ht);

scanner.close(); // Close the scanner object after use


}

// Helper method to print the Hashtable contents


public static void printHashtable(Hashtable<Integer, String> ht) {
for (Integer key : ht.keySet()) {
System.out.println("Student ID: " + key + " -- Student Name: " + ht.get(key));
}
}
}

Out put:
How many elements do you want to insert?
3
Enter Student ID (Key):
101
Enter Student Name (Value):
John Doe
Student added successfully!
Enter Student ID (Key):
102
Enter Student Name (Value):
Jane Smith
Student added successfully!
Enter Student ID (Key):
101
Duplicate key found! The student ID already exists.

Final Hashtable (without duplicates):


Student ID: 101 -- Student Name: John Doe
Student ID: 102 -- Student Name: Jane Smith

(or)
import java.util.Hashtable;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

class RemoveDuplicateValues {
public static void main(String[] args) {
// Create a Hashtable and add some entries (with possible duplicate values)
Hashtable<Integer, String> ht = new Hashtable<>();
ht.put(1, "John");
ht.put(2, "Jane");
ht.put(3, "John"); // Duplicate value "John"
ht.put(4, "Alice");
ht.put(5, "Bob");
ht.put(6, "Alice"); // Duplicate value "Alice"

System.out.println("Original Hashtable:");
printHashtable(ht);
// Remove duplicates based on values
Hashtable<Integer, String> uniqueHashtable = removeDuplicates(ht);

System.out.println("\nHashtable after removing duplicate values:");


printHashtable(uniqueHashtable);
}

// Function to remove duplicate values from the Hashtable


public static Hashtable<Integer, String> removeDuplicates(Hashtable<Integer, String> ht) {
// Create a new Hashtable to store unique key-value pairs
Hashtable<Integer, String> uniqueHashtable = new Hashtable<>();

// Use a HashSet to track unique values


Set<String> uniqueValues = new HashSet<>();

// Iterate over the original Hashtable


for (Map.Entry<Integer, String> entry : ht.entrySet()) {
String value = entry.getValue();

// If the value is not already in the HashSet, add it to the new Hashtable
if (!uniqueValues.contains(value)) {
uniqueHashtable.put(entry.getKey(), value);
uniqueValues.add(value); // Mark this value as encountered
}
}

return uniqueHashtable; // Return the new Hashtable with unique values


}

// Helper function to print the Hashtable


public static void printHashtable(Hashtable<Integer, String> ht) {
for (Map.Entry<Integer, String> entry : ht.entrySet()) {
System.out.println("ID: " + entry.getKey() + ", Name: " + entry.getValue());
}
}
}

Output:
Original Hashtable:
ID: 1, Name: John
ID: 2, Name: Jane
ID: 3, Name: John
ID: 4, Name: Alice
ID: 5, Name: Bob
ID: 6, Name: Alice

Hashtable after removing duplicate values:


ID: 1, Name: John
ID: 2, Name: Jane
ID: 4, Name: Alice
ID: 5, Name: Bob

10 c

import java.util.TreeMap;
import java.util.Map;
import java.util.Set;
import java.util.Iterator;

public class TreeMapIteration {


public static void main(String[] args) {
// Create a TreeMap and insert some entries
TreeMap<Integer, String> treeMap = new TreeMap<>();

treeMap.put(1, "John");
treeMap.put(3, "Jane");
treeMap.put(2, "Alice");
treeMap.put(4, "Bob");

// 1. Iterating using for-each loop with entrySet()


System.out.println("Iterating using for-each with entrySet():");
for (Map.Entry<Integer, String> entry : treeMap.entrySet()) {
System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}

// 2. Iterating using for-each loop with keySet()


System.out.println("\nIterating using for-each with keySet():");
for (Integer key : treeMap.keySet()) {
System.out.println("Key: " + key + ", Value: " + treeMap.get(key));
}

// 3. Iterating using for-each loop with values()


System.out.println("\nIterating using for-each with values():");
for (String value : treeMap.values()) {
System.out.println("Value: " + value);
}

// 4. Iterating using Iterator with entrySet()


System.out.println("\nIterating using Iterator with entrySet():");
Iterator<Map.Entry<Integer, String>> iterator = treeMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Integer, String> entry = iterator.next();
System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}
}
}

Output:
Iterating using for-each with entrySet():
Key: 1, Value: John
Key: 2, Value: Alice
Key: 3, Value: Jane
Key: 4, Value: Bob

Iterating using for-each with keySet():


Key: 1, Value: John
Key: 2, Value: Alice
Key: 3, Value: Jane
Key: 4, Value: Bob

Iterating using for-each with values():


Value: John
Value: Alice
Value: Jane
Value: Bob

Iterating using Iterator with entrySet():


Key: 1, Value: John
Key: 2, Value: Alice
Key: 3, Value: Jane
Key: 4, Value: Bob

You might also like