Consider the following client class:import java.util.Collection;import java.util.Collections;import java.util.HashMap;import java.util.Map;import java.util.Set;public class PresidentsMain { public static void main(String[] args) { Map PresidentsOfTheUnitedStates = new HashMap(); PresidentsOfTheUnitedStates.put("George Washington", "Unaffiliated"); PresidentsOfTheUnitedStates.put("John Adams", "Federalist"); PresidentsOfTheUnitedStates.put("Thomas Jefferson", "Democratic-Republican"); PresidentsOfTheUnitedStates.put("James Madison", "Democratic-Republican"); PresidentsOfTheUnitedStates.put("James Monroe", "Democratic-Republican"); PresidentsOfTheUnitedStates.put("John Quincy Adams", "Democratic-Republican"); PresidentsOfTheUnitedStates.put("Andrew Jackson", "Democratic"); PresidentsOfTheUnitedStates.put("Martin Van Buren", "Democratic"); PresidentsOfTheUnitedStates.put("William Henry Harrison", "Whig"); PresidentsOfTheUnitedStates.put("John Tyler", "Whig"); } }}Extend given client class:Implement a static method called FilterMapByValue, that takes 2 parameters:Map InMap;String TargetValue;Method should print out all map elements, for which Value == TargetValue. Test the implementation by filtering PresidentsOfTheUnitedStates map so that only presidents, affiliated with Democratic-Republican party are printed. Note: use the following to iterate over a map: for (Map.Entry Entry : InMap.entrySet()) Implement a method PrintValues, that prints all values for a given map (use map’s values() function). Test the implementation on PresidentsOfTheUnitedStates map.Implement a method PrintKeys, that prints all keys for a given map (use map’s keySet() function). Test the implementation on PresidentsOfTheUnitedStates map.

Respuesta :

Answer:

import java.util.Collection;

import java.util.Collections;

import java.util.HashMap;

import java.util.Map;

import java.util.Set;

public class PresidentsMain {

     public static void filterMapByValue(Map<String,String> inMap, String targetValue){

           for (Map.Entry<String,String> entry : inMap.entrySet()){

                 if(targetValue.equals(entry.getValue())){

                       System.out.println(entry.getKey()+" - "+entry.getValue());

                 }

           }

     }

     public static void printValues(Map<String,String> map){

           for(String value : map.values()){

                 System.out.println(value);

           }

     }

     public static void printKeys(Map<String,String> map){

           for(String key : map.keySet()){

                 System.out.println(key);

           }

     }

     public static void main(String[] args) {

           Map<String, String> PresidentsOfTheUnitedStates = new HashMap<String, String>();

           PresidentsOfTheUnitedStates.put("George Washington", "Unaffiliated");

           PresidentsOfTheUnitedStates.put("John Adams", "Federalist");

           PresidentsOfTheUnitedStates.put("Thomas Jefferson", "Democratic-Republican");

           PresidentsOfTheUnitedStates.put("James Madison", "Democratic-Republican");

           PresidentsOfTheUnitedStates.put("James Monroe", "Democratic-Republican");

           PresidentsOfTheUnitedStates.put("John Quincy Adams", "Democratic-Republican");

           PresidentsOfTheUnitedStates.put("Andrew Jackson", "Democratic");

           PresidentsOfTheUnitedStates.put("Martin Van Buren", "Democratic");

           PresidentsOfTheUnitedStates.put("William Henry Harrison", "Whig");

           PresidentsOfTheUnitedStates.put("John Tyler", "Whig");

           System.out.println("Presidents of Democratic-Republican party: ");

           filterMapByValue(PresidentsOfTheUnitedStates,"Democratic-Republican");

           System.out.println();

           System.out.println("Keys: ");

           printKeys(PresidentsOfTheUnitedStates);

           System.out.println();

           System.out.println("Values: ");

           printValues(PresidentsOfTheUnitedStates);

      }

}

Explanation:

See the code above