Day7 Colection Framework

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

/*Write a Java program to print Common elements from given two

HashSets in main method itself

i/p: 1 2 3 4 5 6

2 4 5 9 10

o/p: 2 4 5

i/p: 20 5 -45 100 30

98 -45 20 5 35

o/p: -45 5 20

classname: ClassCollectionFramework1
*/

import java.util.HashSet;

public class ClassCollectionFramework1 {


public static void main(String[] args) {
// Create two HashSet objects
HashSet<Integer> set1 = new HashSet<>();
HashSet<Integer> set2 = new HashSet<>();

// Add elements to the first set


set1.add(1);
set1.add(2);
set1.add(3);
set1.add(4);
set1.add(5);
set1.add(6);

// Add elements to the second set


set2.add(2);
set2.add(4);
set2.add(5);
set2.add(9);
set2.add(10);

// Print the common elements


System.out.println("Common elements:");
for (int num : set1) {
if (set2.contains(num)) {
System.out.print(num + " ");
}
}
}
}

/*Write a java program to print student name and his ID in sorted order by name

using HashMap in main method

i/p: {Anand=ID102, Ramesh=ID101,Kiran=ID100}

String[] keys=new String[3];

String[] values=new String[3];

hm.put(keys[i],values[i])

o/p:

Anand: ID102

Kiran: ID100

Ramesh:ID101

classname: ClassCollectionFramework2
*/

import java.util.*;

public class ClassCollectionFramework2 {


public static void main(String[] args) {
// Create a HashMap to store student names and IDs
HashMap<String, String> studentMap = new HashMap<>();

// Add elements to the HashMap


studentMap.put("Anand", "ID102");
studentMap.put("Ramesh", "ID101");
studentMap.put("Kiran", "ID100");

// Create a TreeMap to automatically sort the elements by key (student


name)
TreeMap<String, String> sortedMap = new TreeMap<>(studentMap);

// Iterate over the sorted map and print student names and IDs
for (Map.Entry<String, String> entry : sortedMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
/*
Write a Java Program to rename a key from the given HashMap

and sort them based on key

{apples=10, bananas=20, mangoes=15, oranges=200, watermelons=50}

i/p:

apples

pomegranates

o/p:

bananas 20

mangoes 15

oranges 200

pomegranates 10

watermelons 50

classname: ClassHashMapRenameKey
*/

import java.util.*;

public class ClassHashMapRenameKey {


public static void main(String[] args) {
// Create the input HashMap
HashMap<String, Integer> map = new HashMap<>();
map.put("apples", 10);
map.put("bananas", 20);
map.put("mangoes", 15);
map.put("oranges", 200);
map.put("watermelons", 50);

// Print the original map


System.out.println("Original HashMap:");
printMap(map);

// Rename the key from "apples" to "pomegranates"


renameKey(map, "apples", "pomegranates");

// Sort the map by keys


TreeMap<String, Integer> sortedMap = new TreeMap<>(map);
// Print the sorted map
System.out.println("\nSorted HashMap after renaming the key:");
printMap(sortedMap);
}

// Method to rename a key in the HashMap


public static void renameKey(HashMap<String, Integer> map, String oldKey,
String newKey) {
if (map.containsKey(oldKey)) {
Integer value = map.remove(oldKey); // Remove the entry with the old
key
map.put(newKey, value); // Add a new entry with the new key and the
same value
}
}

// Method to print the HashMap


public static void printMap(Map<String, Integer> map) {
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
}

You might also like