Concept explainers
A 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 {
public static void main(String[] args) {
Map<String, Integer> vehicles = new TreeMap<>();
// Add some vehicles.
vehicles.put("BMW", 5);
vehicles.put("Mercedes", 3);
vehicles.put("Audi", 4);
vehicles.put("Ford", 10);
System.out.println("Total vehicles: " + vehicles.size());
// Iterate over all vehicles, using the keySet method.
for (String key : vehicles.keySet())
System.out.println(key + " - " + vehicles.get(key));
System.out.println();
System.out.println("Highest key: " + ((TreeMap) vehicles).lastKey());
System.out.println("Lowest key: " + ((TreeMap) vehicles).firstKey());
System.out.println("\nPrinting all values:");
for (Integer val : vehicles.values())
System.out.println(val);
System.out.println();
// Clear all values.
vehicles.clear();
// Equals to zero.
System.out.println("After clear operation, size: " + vehicles.size());
}
}
The suspected output looks like this:
Total vehicles: 4
Audi - 4
BMW - 5
Ford - 10
Mercedes - 3
Highest key: Mercedes
Lowest key: Audi
Printing all values:
4
5
10
3
After clear operation, size: 0
Press any key to continue . . .
Assume that we have an actual FactBook2008.txt which you need to read. We want to create a wordcount map made out of K=String, and V=Integer. The program is to count the frequency of words in the above file. Basically, if the word shows up for the first time we count it as being 1. If we have encountered already we increment the word count by 1.
We would like to print the words in the files and count how many of them we read.Hint: Use the following loop to print the output as follows:
for(String word : wordCount.keySet())
System.out.println(word + " " + wordCount.get(word));
Step by stepSolved in 2 steps
- If a collection stores 10 objects, what value would be returned from a call to its size method?arrow_forwardYou may have found it somewhat tedious and unpleasant to use the debugger and visualizer to verify the correctness of your addFirst and addLast methods. There is also the problem that such manual verification becomes stale as soon as you change your code. Imagine that you made some minor but uncertain change to addLast]. To verify that you didn't break anything you'd have to go back and do that whole process again. Yuck. What we really want are some automated tests. But unfortunately there's no easy way to verify correctness of addFirst and addLast] if those are the only two methods we've implemented. That is, there's currently no way to iterate over our list and get bad its values and see that they are correct. That's where the toList method comes in. When called, this method returns a List representation of the Deque. For example, if the Deque has had addLast (5) addLast (9) addLast (10), then addFirst (3) called on it, then the result of toList() should be a List with 3 at the…arrow_forwarddef upgrade_stations(threshold: int, num_bikes: int, stations: List["Station"]) -> int: """Modify each station in stations that has a capacity that is less than threshold by adding num_bikes to the capacity and bikes available counts. Modify each station at most once. Return the total number of bikes that were added to the bike share network. Precondition: num_bikes >= 0arrow_forward
- The compareTo method. First you compare this.ticker which is a String, then if they are the same then you compare by the this.purchasePrice which is a double hashCode public int hashCode() The hashCode method using the Code-> Generate equals and hashCode with no changes Overrides: hashCode in class Object Returns: int Representing the hashCode compareTo public int compareTo(Stock another) The compareTo method not auto generated Compares first by ticker and if the tickers are the same secondarily compares by price Specified by: compareTo" in interface Comparable<stock> Parameters: another - the object to be compared. Returns: int Representing order Throws: IllegalArgumentException"- if another is null.arrow_forwardFor any element in keysList with a value smaller than 40, print the corresponding value in itemsList, followed by a comma (no spaces). Ex: If keysList = {32, 105, 101, 35} and itemsList = {10, 20, 30, 40}, print: 10,40, #include <iostream>#include <string.h>using namespace std; int main() { const int SIZE_LIST = 4; int keysList[SIZE_LIST]; int itemsList[SIZE_LIST]; int i; cin >> keysList[0]; cin >> keysList[1]; cin >> keysList[2]; cin >> keysList[3]; cin >> itemsList[0]; cin >> itemsList[1]; cin >> itemsList[2]; cin >> itemsList[3]; /* Your code goes here */ cout << endl; return 0;}arrow_forwardEstimated Completion Time: 2-3 minutes Which of the following situations will lead to an object being aliased? Select all that apply. n Assigning a list to L1, then assigning L1 to L2 n Assigning a string to S1, then assigning the result of slicing S1 (i.e. S1[:]) to S2 n Assigning a list to L1, then assigning the result of a copy() method on L1 to L2 n Assigning a list to L1, then assigning the result of slicing L1 (i.e., L1[:]) to L2 n Assigning a string to S1, then assigning the result of a replace() method on S1 to S2 n Assigning a list to L1, then using L1 in a function call with header "def foo(x: List) -> List" n Assigning a string to S1, then using S1 in a function call with header "def foo(x: str) -> None" n Assigning two lists to L1 and L2, then assigning the result of adding them to L3arrow_forward
- 7- question What keys and values are contained in the following map after execution of the following piece of code? Map map = new HashMap (); map.put (8, "Eight"); map.put (41, "Forty-one"); map.put (8, "Ocho"); map.put (18, "Eighteen"); map.put (50, "Fifty"); map.put (132, "OneThreeTwo"); map.put (28, "Twenty-eight"); map.put (79, "Seventy-nine"); map.remove ( 41); map.remove (28); map.put (28, "18"); map.remove (18); a. {8=Eight, 41=Forty-one, 8=0cho, 18=Eighteen, 50=Fifty, 132=OneThreeTwo, 28=Twenty-eight, 79=Seventy-nine, 28=18} b. {8=Eight, 8=Ocho, 50=Fifty, 132=OneThreeTwo, 79=Seventy-nine, 28=18} c. {50=Fifty, 132=DOneThreeTwo, 8=Ocho, 28=18, 79=Seventy-nine} d. {8=Eight, 50=Fifty, 132=OneThreeTwo, 79=Seventy-nine, 28=18}arrow_forwardHow does the map react when a new entry is added with an already existing key?arrow_forwardWhen sorting objects using the Comparable interface, what happens if two objects have the same comparison value in the compareTo() method?arrow_forward
- 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