Professional Documents
Culture Documents
Assignment 2 Remaining
Assignment 2 Remaining
import java.util.HashSet;
Output:
import java.util.*;
import java.util.Iterator;
public class Exercise2 {
public static void main(String[] args) {
// Create a empty hash set
HashSet<String> h_set = new HashSet<String>();
// use add() method to add values in the hash set
h_set.add("Red");
h_set.add("Green");
h_set.add("Black");
h_set.add("White");
h_set.add("Pink");
h_set.add("Yellow");
// set Iterator
Iterator<String> p = h_set.iterator();
// Iterate the hash set
while (p.hasNext()) {
System.out.println(p.next());
}
}
}
Output
Red
White
Pink
Yellow
Black
Green
import java.util.*;
Output:
import java.util.*;
import java.util.Iterator;
public class Exercise3 {
public static void main(String[] args) {
// Create a empty hash set
HashSet<String> h_set = new HashSet<String>();
// use add() method to add values in the hash set
h_set.add("Red");
h_set.add("Green");
h_set.add("Black");
h_set.add("White");
h_set.add("Pink");
h_set.add("Yellow");
System.out.println("Original Hash Set: " + h_set);
System.out.println("Size of the Hash Set: " + h_set.size());
}
}
Output:
import java.util.*;
Output:
import java.util.*;
import java.util.*;
import java.util.*;
Output:
import java.util.*;
import java.util.*;
Output:
Yes
No
Yes
No
L. compare two sets and retain elements which are same on both sets
import java.util.*;
Output:
import java.util.*;
Output:
import java.util.*;
Output:
1 Red
2 Green
3 Black
4 White
5 Blue
B. count the number of key-value (size) mappings in a map
import java.util.*;
Output:
C. copy all of the mappings from the specified map to another map
import java.util.*;
Output:
Values in first map: {1=Red, 2=Green, 3=Black}
import java.util.*;
Output:
import java.util.*;
Output:
import java.util.*;
import java.util.*;
import java.util.*;
import java.util.*;
hash_map.put(1,"Red");
hash_map.put(2,"Green");
hash_map.put(3,"Black");
hash_map.put(4,"White");
hash_map.put(5,"Blue");
Output:
import java.util.*;