Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134670942
Author: Y. Daniel Liang
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Expert Solution & Answer
Chapter 30.2, Problem 30.2.4CP
Explanation of Solution
Expression of “map” type for given scenario:
map.entrySet().stream().mapToDouble(e->e.getValue()).sum()
Explanation:
The “entrySet()” was retrieved all elements in map and add all the double values using aggregate method “sum()”. It fetch the double values using the method “mapToDouble()”.
Program:
//Import necessary package
import java.util.stream.Stream;
import java.util...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Write a method called flipMap() that takes in a map from string keys to string values, and returns a "reverse" or "inverse" version of the same map contents, mapping values from the first map to keys from the first map. For example, if the map provided has key "hello" mapped to value "world", then the map returned by your method should contain key "world" mapping to value "hello". You can modify the provided map, or create a new map.
How do you check whether a given key is contained in a map? (Give a Java code example.)
import java.util.HashMap;
import java.util.Map;
public class LinearSearchMap {
// Define a method that takes in a map and a target value as parameters
public static boolean linearSearch(Map<String, Integer> map, int target) {
// Iterate through each entry in the map
for () {
// Check if the value of the current entry is equal to the target
if () {
// If the value is equal to the target, return true
}
}
// If no entry with the target value is found, return false
}
public static void main(String[] args) {
// Create a HashMap of strings and integers
Map<String, Integer> numbers = new HashMap<>();
// Populate the HashMap with key-value pairs
numbers.put("One", 1);
numbers.put("Two", 2);
numbers.put("Three", 3);
numbers.put("Four", 4);
numbers.put("Five", 5);
// Set the target value to search for…
Chapter 30 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Ch. 30.1 - Prob. 30.1.1CPCh. 30.2 - Prob. 30.2.1CPCh. 30.2 - Prob. 30.2.2CPCh. 30.2 - Prob. 30.2.3CPCh. 30.2 - Prob. 30.2.4CPCh. 30.3 - Prob. 30.3.1CPCh. 30.3 - Prob. 30.3.2CPCh. 30.3 - Prob. 30.3.3CPCh. 30.3 - Prob. 30.3.4CPCh. 30.3 - Given an array names in Listing 30.1, write the...
Ch. 30.4 - Prob. 30.4.1CPCh. 30.4 - How do you create a parallel stream?Ch. 30.4 - Prob. 30.4.3CPCh. 30.4 - Prob. 30.4.4CPCh. 30.4 - Prob. 30.4.5CPCh. 30.4 - Write a statement to obtain an array of 1000...Ch. 30.5 - Prob. 30.5.1CPCh. 30.5 - Prob. 30.5.2CPCh. 30.5 - Prob. 30.5.3CPCh. 30.5 - Prob. 30.5.4CPCh. 30.6 - Prob. 30.6.1CPCh. 30.7 - Prob. 30.7.1CPCh. 30.8 - Can the following code be used to replace line 19...Ch. 30.8 - Prob. 30.8.2CPCh. 30.8 - Prob. 30.8.3CPCh. 30.8 - Prob. 30.8.4CPCh. 30.8 - Write the code to obtain a one-dimensional array...Ch. 30 - Prob. 30.1PECh. 30 - Prob. 30.2PECh. 30 - Prob. 30.3PECh. 30 - (Print distinct numbers) Rewrite Programming...Ch. 30 - Prob. 30.5PECh. 30 - Prob. 30.6PECh. 30 - Prob. 30.7PECh. 30 - Prob. 30.8PECh. 30 - Prob. 30.9PECh. 30 - Prob. 30.10PECh. 30 - Prob. 30.11PECh. 30 - (Sum the digits in an integer) Rewrite Programming...Ch. 30 - (Count the letters in a string) Rewrite...Ch. 30 - Prob. 30.14PECh. 30 - (Display words in ascending alphabetical order)...Ch. 30 - Prob. 30.16PECh. 30 - Prob. 30.17PECh. 30 - (Count the occurrences of words in a text file)...Ch. 30 - (Summary information) Suppose the file test.txt...
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- Explain code and complete. //the third and final class which implements Hashmapspublic class OurMapUse { public static void main(String[] args) { Map<String, Integer> map = new Map<>(); for(int i=0;i<20;i++) { map.insert("abc"+i+1+i); System.out.print("i = "+i+"lf = "+map.loadFactor()); } } }.arrow_forwardCan 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 with the empty string.** @param map to be edited* @return map*/public Map<String, String> confiscate(Map<String, String> map) {if (map.containsKey("a") && map.containsValue("a")) {}}/*** Modify and return the given map as follows: if the key "duck" has a value, set the key* "goose" to have that same value. In all cases remove the key "swan", the rest of the map* should not change.** @param map to be edited* @return map*/public Map<String, String> mapBird1(Map<String, String> map) {throw new UnsupportedOperationException("Not…arrow_forwardQ6) Answer the following: [2 Marks] a) Write a small code fragment that generates a random integer number using Random class . b) How do you check whether a given key is contained in a map? (Give a Java code example.) c) Create a class MapTester. In it, use a HashMap to implement a phone Book. In this class, implement two methods: public void enterNumber(String name, String number) public String lookupNumber(String name) The methods should use the put and get methods of the HashMap class to implement their functionalityarrow_forward
- Write the output produced when the following method is passed each of the following maps: public static void mystery(Map<String, String> map) { Map<String, String> result = new TreeMap<String, String>(); for (String key : map.keySet()) { if (key.compareTo(map.get(key)) < 0) { result.put(key, map.get(key)); } else { result.put(map.get(key), key); } } System.out.println(result); } a. {two=deux, five=cinq, one=un, three=trois, four=quatre} b. {skate=board, drive=car, program=computer, play=computer} c. {siskel=ebert, girl=boy, H=T, ready=begin, first=last, begin=end} d. {cotton=shirt, tree=violin, seed=tree, light=tree, rain=cotton}arrow_forwardWrite a method named rarest that accepts a TreeMap from strings to strings as a parameter and returns the value that occurs least frequently in the map. If there is a tie, return the value that comes earlier in ABC order. For example, if a variable called map containing the following elements: {"Alyssa":"Harding", "Char":"Smith", "Dan":"Smith", "Jeff":"Jones", "Kasey":"Jones", "Kim":"Smith", "Morgan":"Jones", "Ryan":"Smith", "Stef":"Harding"} Then a call of rarest(map) would return "Harding" because that value occurs 2 times, fewer than any other. Note that we are examining the values in the map, not the keys. If the map passed is empty, throw an IllegalArgumentException. Constraints: Obey the following restrictions in your solution. You may create one additional data structure (stack, queue, set, map, etc.) as auxiliary storage. (You can have as many simple variables as you like.) You should not modify the contents of the map passed to your method.Declare your method in such a way…arrow_forwardThe map file holds a bunch of rectangles. Some of the rectangles are solid, and you can think of them like crates in a warehouse. They can be enclosed by a non-solid rectangle, and those rectangles are just there to make theintersection testing and such go faster. If a line doesn't cross a non-solid rectangle, then it cannot hit any rectangle inside it. Nice! The format is:Solid(1)/Nonsolid(0) x_min y_min x_max y_max (list of children iff not solid) The list of children are line numbers (the lines start at 0) of solid rectangles inside the non-solid rectangle. Solid rectangles do not have children. All non-solid rectangles must have children. Here's a sample file:0 0 0 100 100 1 21 1.1 1.0 1.2 1.31 10 10 11 110 50 60 70 80 40 55 65 65 75 51 56 66 64 74 The first line (line 0) is a non-solid rectangle with two children (the next two rows, lines 1 and 2). The first rectangle ranges from (0,0) to (100,100). The children of it are both small solid rectangles, the first is only .1 x .3,…arrow_forward
- Consider the following class map, class map { public: map(ifstream &fin); void print(int,int,int,int); bool isLegal(int i, int j); void setMap(int i, int j, int n); int getMap(int i, int j) const; int getReverseMapI(int n) const; int getReverseMapJ(int n) const; void mapToGraph(graph &g); bool findPathRecursive(graph &g, stack<int> &moves); bool findPathNonRecursive1(graph &g, stack<int> &moves); bool findPathNonRecursive2(graph &g, queue<int> &moves); bool findShortestPath1(graph &g, stack<int> &bestMoves); bool findShortestPath2(graph &, vector<int> &bestMoves); void map::printPath(stack<int> &s); int numRows(){return rows;}; int numCols(){return cols;}; private: int rows; // number of latitudes/rows in the map int cols; // number of longitudes/columns in the map matrix<bool> value; matrix<int> mapping; // Mapping from latitude and longitude co-ordinates (i,j) values to node index…arrow_forwardIn Java, Explain when it might be preferable to use a map instead of a set.arrow_forward"Make a program using a map where the mapping value is a custom object of your design. It can be a one-to-one mapping or a one-to-many mapping, your choice." Maps Object Extension, Java, AP Computer Science A, simple. Hash Map. One Class.arrow_forward
- In the existing class UnsortedTableMap, the method put(k,v) is used to add an entry to a map. This method spends time to locate an existing item with the given key. Provide a non-static method named putOnlyIfAbsent(k,v) that adds the entry to the map only if there is no entry with a key k. In case an entry with key k already exists, then just return the existing value corresponding to the key. The new method should be provided in the class UnsortedTableMap. Write the testing code in the main method of the class UnsortedTableMap.arrow_forwardA map has the form Map <k,v> where: K: specifies the type of keys maintained in this map.V: defines the type of mapped values.Furthermore, the Map interface provides a set of methods that must be implemented. In this section, we will discuss about the most famous methods: clear: Removes all the elements from the map.containsKey: Returns true if the map contains the requested key.containsValue: Returns true if the map contains the requested value.equals: Compares an Object with the map for equality.get: Retrieve the value of the requested key.entrySet: Returns a Set view of the mappings contained in this map.keySet: Returns a Set that contains all keys of the map.put: Adds the requested key-value pair in the map.remove: Removes the requested key and its value from the map, if the key exists.size: Returns the number of key-value pairs currently in the map. Of interest to us is the TreeMap. Here is an example of TreeMap with a Map: import java.util.Map; import…arrow_forwardA map has the form Map <k,v> where: K: specifies the type of keys maintained in this map.V: defines the type of mapped values.Furthermore, the Map interface provides a set of methods that must be implemented. In this section, we will discuss about the most famous methods: clear: Removes all the elements from the map.containsKey: Returns true if the map contains the requested key.containsValue: Returns true if the map contains the requested value.equals: Compares an Object with the map for equality.get: Retrieve the value of the requested key.entrySet: Returns a Set view of the mappings contained in this map.keySet: Returns a Set that contains all keys of the map.put: Adds the requested key-value pair in the map.remove: Removes the requested key and its value from the map, if the key exists.size: Returns the number of key-value pairs currently in the map. Here is an example of TreeMap with a Map: import java.util.Map; import java.util.TreeMap; public class TreeMapExample {…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education