These are all different questions, please answer them separately and label each question
Please find your answers below and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
//method to find the conflicts of two maps
public static Set<String> conflicts(Map<String, Integer> map1,
Map<String, Integer> map2) {
// creating a set to store the result
Set<String> result = new TreeSet<String>();
// looping through each key in map1
for (String key : map1.keySet()) {
// if map2 contains the same key but the value associated with it is
// different, adding key to result
if (map2.containsKey(key) && !(map2.get(key).equals(map1.get(key)))) {
result.add(key);
}
}
return result;
}
// method to merge two maps
public static Map<String, Integer> mergeMaps(Map<String, Integer> map1,
Map<String, Integer> map2) {
// if there are conflicts (at least one), returning null
if (conflicts(map1, map2).size() > 0) {
return null;
} else {
// otherwise creating a map
Map<String, Integer> result = new TreeMap<String, Integer>();
// adding all elements of map1 and map2 unchanged
result.putAll(map1);
result.putAll(map2);
// returning the merged map
return result;
}
}
// method to merge two maps into a multimap
public static Map<String, LinkedList<Integer>> mergeMapsToMultiMap(
Map<String, Integer> map1, Map<String, Integer> map2) {
// creating a multi map with key=String, value= LinkedList of integers
Map<String, LinkedList<Integer>> result = new TreeMap<String, LinkedList<Integer>>();
// looping through each key in map1
for (String key : map1.keySet()) {
// creating a linked list, adding the value associated with current
// key, adding key and list to the multi map
LinkedList<Integer> list = new LinkedList<Integer>();
list.add(map1.get(key));
result.put(key, list);
}
// looping through each key in map2
for (String key : map2.keySet()) {
// if result already contains the key, adding the value to the
// linked list associated with given key
if (result.containsKey(key)) {
result.get(key).add(map2.get(key));
} else {
// otherwise creating a linked list, adding the value associated
// with current key, adding key and list to the multi map
LinkedList<Integer> list = new LinkedList<Integer>();
list.add(map2.get(key));
result.put(key, list);
}
}
return result;
}
Last question: Suppose the following elements were added to a PriorityQueue in this order. In what order would they be removed from the queue? ["baseball", "apple", "mango", "football", "kiwi", "basketball"]
Answer: The elements will be removed in the order: apple, baseball, basketball, football, kiwi, mango. This is because the PriorityQueue class removes the minimum value stored in it each time the remove method is called. Here minimum value means the lowest value found using compareTo() implementation of the elements. By default, String class implements compareTo method to compare Strings based on the alphabetical (lexicographic) order. So elements will be removed in alphabetical order.
These are all different questions, please answer them separately and label each question Write a static...
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....
HW60.1. Using Maps Implement a public final class named wordCounter. WordCounter should provide a single static method countwords, which takes a string and returns a Map from strings to Integers. Each entry in the map should have a value (Integer) representing how many times that key (String) appeared in the String that was passed to countWords. For example: MapString, Integer» counts = Wordcounter.countilords ("cs 125 is awesome"); System.out.println(counts.get("CS")); // prints 1 System.out.println(counts.get( "125")); // prints 1 System.out.println(counts.get("225")); // prints null...
Hi, I need help with Python Dictionaries please. Question: Write a function named "add_key_value" that takes a key-value store as a parameter with strings as keys and integers as values. The function will add a key-value pair to the input store with a key of "slam" and a value of 39. There is no need to return any value. Thanks!
Use Java language You may assume that you always map strings to integers, and therefore you need not worry about using generics. Here are the methods that your map must implement. void put (String key, Integer value); insert this key-value pair into the map under the following rules: If key already exists in the map, change its value to value, overwriting what was there If key is not already there, insert this key-value pair into the map. boolean containsKey(String key);...
Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for the dictionary are strings and the values are lists of numbers. The function should create a new dictionary where the keys are the same strings as the original dictionary and the values are tuples. The first entry in each tuple should be the weighted average of the non-negative values in the list (where the weight was the number in the 0 position of the...
Can someone help me with this code, I not really understanding how to do this? import java.util.ArrayList; import java.util.HashMap; import java.util.Map; /** * @version Spring 2019 * @author Kyle */ public class MapProblems { /** * Modify and return the given map as follows: if the key "a" has a value, set the key "b" to * have that value, and set the key "a" to have the value "". Basically "b" is confiscating the * value and replacing it...
JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...
I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...
Problem 14: all 5 parts must be complete to receive credit for the question /** * q1: Write a public static method named q1 that takes no parameters and returns void. The * method should print all the integers from -8 to 47 to the screen. The output should be * inclusive of these end points */ /** * q2: Write a public static method named q2 that takes two ints as parameters and returns...
1. Write a static method named mode that takes an array of integers as a parameter and that returns the value that occurs most frequently in the array. Assume that the integers in the array appear in sorted order. For example, if a variable called list stores the following values: ist -3, 1, 4, 4, 4, 6, 7,8, 8, 8, 8, 9, 11, 11, 11, 12, 14, int 141i Then the call of mode (li array, appearing four times. st,...