Create a class “MapIntersectClient” and write a method
mapIntersect in that class that accepts two maps whose keys are
strings and whose values are integers as parameters and returns a
new map containing only the key/value pairs that exist in both of
the parameter maps. In order for a key/value pair to be included in
your result, not only do both maps need to contain a mapping for
that key, but they need to map it to the same value. For example,
if the two maps passed are {Janet=87, Logan=62, Whitaker=46,
Alyssa=100, Stefanie=80, Jeff=88, Kim=52, Sylvia=95} and {Logan=62,
Kim=52, Whitaker=52, Jeff=88, Stefanie=80, Brian=60, Lisa=83,
Sylvia=87}, your method would return the following new map (the
order of the key/value pairs does not matter): {Logan=62,
Stefanie=80, Jeff=88, Kim=52}.
Please write main method inside this class and create , initialize
maps and pass them to the mapIntersect method and print the
returned map.
Answer:
import java.util.*;
public class MapIntersectClient {
public static void main(String[] args) {
HashMap<String, Integer> m1 = new HashMap<String,
Integer>();//declaring map-1 varaibles
m1.put("Janet", 87);
m1.put("Logan", 62);
m1.put("Whitaker", 46);
m1.put("Alyssa", 100);
m1.put("Stefanie", 80);
m1.put("Jeff", 88);
m1.put("Kim", 52);
m1.put("Sylvia",95);
HashMap<String, Integer> m2 = new HashMap<String,
Integer>();//declaring map-2 vraibles
m2.put("Logan", 62);
m2.put("Kim", 52);
m2.put("Whitaker", 52);
m2.put("Jeff", 88);
m2.put("Stefanie", 80);
m2.put("Brian", 60);
m2.put("Lisa", 83);
m2.put("Sylvia",87);
System.out.println(mapintersect(m1,m2));//calling calling
function mapintersect
}
public static Map mapintersect(Map a,Map b)
{
Map m2 = new HashMap(); //intialising new map to return
after completion
Iterator<String> it = a.keySet().iterator();
//creating itearator for map-a
while (it.hasNext()) {//looping through map-a
values
Iterator<String> it1 = b.keySet().iterator();
//creating itearator for map-b
String key = it.next();
while(it1.hasNext()) //looping through map-b
values
{
String key2 = it1.next();
if(key2.equals(key) && (a.get(key)==b.get(key2)))
//comparing map-a values with map-b
{
m2.put(key,a.get(key));//inserting into map-2 if
equal
}
}
}
return m2;//returning map-2
}
}
Output:
FEEL FREE TO COMMENT ON
COMMENT SECTION
Create a class “MapIntersectClient” and write a method mapIntersect in that class that accepts two maps...
Create a class called Login which contains a HashMap as a private attribute. It should also have an instance method called loadCredentials which acccepts the two arrays. It will load the Hash Map with key/value pairs based on the two arrays above. The userNames should be the keys and the passwords the values. private static String[] userNameArray = {"John", "Steve", "Bonnie", "Kylie", "Logan", "Robert"); private static String) passwordArray = {"1111", "2222", "3333", "4444", "5555", "6666"}; Create a login method in...
* The Map class is used to create and manipulate voting maps. The value of a * cell on the map denotes the party for which the majority of the population * of that cell votes for. For instance, in the following map, PARTY_X is the * choice of voters in three cells, while PARTY_O is preferred in the rest of * the map: * O X O * X O X * O O O * A map is...
Implement the remove() and the rehash() methods for the following externally chained hash table. Note you should rehash when "size > arraySize" at the start of the put() method. The new size of the arraySize should be "(2 * old array size) + 1" import java.util.ArrayList; import java.util.Hashtable; import java.util.LinkedList; import java.util.Map; public class ExternalChainingHashTable<K,V> { private class Mapping { private Mapping next; private K key; private V value; private Mapping(K key, V value) { this.key = key; this.value =...
Write the definition of the class Tests such that an object of this class can store a student's first name, last name, five test scores, average test score, and grade. (Use an array to store the test scores.) Add constructors and methods to manipulate data stored in an object. Among other things, your class must contain methods to calculate test averages, return test averages, calculate grades, return grades, and modify individual test scores. The method toString must return test data...
Thank you in advance. Create a class called Main and write the main method. I need help with calling the printChart method Declare a variable to hold the grade as it is read in Declare 5 variables to hold counts to count the number of As, Bs, Cs, etc declare a variable to hold the class name and ask the user to enter the name of the class whose grades are going to be entered. Write a loop that reads...
Write a method in the HashIntSet class called addAll that accepts another hash set as a parameter and adds all of the elements from the other set into the current set. For example, if the set stores [-5, 1, 2, 3] and the method is passed [2, 3, 6, 44, 79], your set would store [-5, 1, 2, 3, 6, 44, 79]. Write a method in the HashIntSet class called containsAll that accepts another hash set as a parameter and...
Create a DataEntryException class whose getMessage() method returns information about invalid integer data. Write a program named GetIDAndAge that continually prompts the user for an ID number and an age until a terminal 0 is entered for both. If the ID and age are both valid, display the message ID and Age OK. Throw a DataEntryException if the ID is not in the range of valid ID numbers (0 through 999), or if the age is not in the range...
Need some help on homework practice problems, Thank you in advance! Problem 1: Write a method, parallelSum(), that uses the fork-join framework to compute the sum of the elements of an array. Its skeleton as well as its Javadoc is provided in the file ParallelSum.java. Simple code for testing and timing your method is also provided in the main method. This problem is essentially the same as ParallelMax in structure. Therefore, follow the code for ParallelMax to complete ParallelSum. Problem...
JAVA Create a Governor class with the following attributes: name : String party - char (the character will be either D, R or I) ageWhenElected : int Create a State class with the following attributes: name : String abbreviation : String population : long governor : Governor Your classes will have all setters, getters, typical methods to this point (equals(), toString()) and at least 3 constructors (1 must be the default, 1 must be the copy). You will be turning...
Java you are required to add a public Object get( Object key) method into the Hashtable class. As defined in the Java documentation, the get() method returns the value with which the specified key is associated, or null if this hash table contains no record for the key. More formally, if this hash table contains a mapping from a key k to a value v such that (key.equals(k)), then this method returns v; otherwise it returns null. (There can be...