Question
__________________________________________________________________________________________
HashMaps
A hashtable is a data structure that allows the user to look up things by a key value. Java implements a hashtable
called “HashMap”. The way it works is to specify two types of classes. One is the “key”, and another is the
“value”. You can put data values into the HashMap at an “index” specified by the key.
Like ArrayList, HashMap is a generic class, so you declare it using angle brackets. For HashMap, you must
include both the key type and the value type in this declaration, so the declaration looks like
HashMap<Key,Value>. As an example, the following declares a HashMap with a String key and an Integer
value:
HashMap<String, Integer> h = new HashMap<String, Integer>();
HashMap has quite a few useful methods. A couple of the most important are “put”, to put (key,value) pairs into
the HashMap, and “get” to get a value based on a key. For example:
h.put("Hello", 1);
int helloVal = h.get("Hello");
Other methods of HashMap clear the table, determine if a certain key or value is stored in the table, remove a value, and get a list of all keys in the table.
Q7) Suppose that your program has a HashMap called plants with keys that are Strings and values that are
Integers. What code would you use to print the value associated with the string "grass"?
Part 3) Copy and complete the following program that does a little bit with HashMap.


Part 3) Copy and complete the following program that does a little bit with HashMap. // Lab 7b // <Name> // <Section> import
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code for the above assignment.

Feel free to comment on any doubt and give thumbs up!

Code:

import java.util.HashMap;

public class Lab7b

{

public static void main(String[] args) {

// Create a new HashMap called "songsStarts"

HashMap<String, Integer> songStars = new HashMap<>();

// Yes, you should listen to all these on Youtube

songStars.put("The balled of Bilbo Baggins", 5);

songStars.put("A still more glorious dawn", 4);

songStars.put("A finite simple group of order two", 4);

songStars.put("Code monkey", 2);

songStars.put("Fish heads", 2);

songStars.put("I;ll form the head", 3);

songStars.put("Honeybee", 5);

songStars.put("Silver apples of the moon", 4);

// Get the number of stars for "Code monkey", and print the # of stars

int numberOfStars = songStars.get("Code monkey");

System.out.println("Number of stars for the song Code monkey on Youtube are : " + numberOfStars);

// Remove the song "Honeybee"

songStars.remove("Honeybee");

// Determine if "Honeybee" is still in the table.

if(songStars.containsKey("Honeybee")){

System.out.println("Honeybee still in table");

}else{

System.out.println("Successfully removed Honeybee");

}

}

}

Screenshot

Number of stars for the song Code monkey on Youtube are : 2 Successfully removed Honeybee

Thank You!

Add a comment
Know the answer?
Add Answer to:
__________________________________________________________________________________________ HashMaps A hashtable is a data structure that allows the user to look up things...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use Java language You may assume that you always map strings to integers, and therefore you...

    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);...

  • Please answer this problem in C++, Thank you! Read and study the sample program: "hashing with chaining using singly...

    Please answer this problem in C++, Thank you! Read and study the sample program: "hashing with chaining using singly linked lists", as well as "hashing with chaining using doubly linked lists". Notice this program is complete except it doesn't include the remove function. Write the remove function and test it with the rest of the program including the given main program. Upload your C++ source file. ---Here is the referenced program: "hashing with chaining using singly linked lists". Below this...

  • Hey guys I need help with this assignment. However it contains 7 sub-problems to solve but I figured only 4 of them can...

    Hey guys I need help with this assignment. However it contains 7 sub-problems to solve but I figured only 4 of them can be solved in one post so I posted the other on another question so please check them out as well :) Here is the questions in this assignment: Note: Two helper functions Some of the testing codes for the functions in this assignment makes use of the print_dict in_key_order (a dict) function which prints dictionary keyvalue pairs...

  • Write a program that allows the user to enter an unsigned integer (the maximum value of...

    Write a program that allows the user to enter an unsigned integer (the maximum value of an unsigned 4-byte int is 232 = 4,294,967,296) and reverses its format (from little to big endian, or vice versa). Print out the user-entered number in hexadecimal and binary, reverse the endianness, and print the reverse in hexadecimal and binary. Integers in most machine architectures are represented in little endian format: the least significant byte is stored in the smallest address; for instance, the...

  • Read a file called populations.txt and populate a php associative array with the values read from...

    Read a file called populations.txt and populate a php associative array with the values read from the file. Here is an example populations.txt file: Dhaka, 23 million Bangaluru, 21 million New York, 9 million Emporia, 23 thousand Here's some pseudo code: open the file loop through the lines in the file read a line into a string variable explode (or is it split, or something else) the string into key value pairs save the key value pair into your associative...

  • Q16-Decoder (0.5 points) Write a code block to decode strings from secret encodings back to reada...

    Q16-Decoder (0.5 points) Write a code block to decode strings from secret encodings back to readable messages. To do so: Initialize a variable called decoded as an empty string . Use a for loop to loop across all characters of a presumed string variable called encoded Inside the loop, convert the character to another character o Get the unicode code point for the character (using ord o Subtract the value of key to that code point (which should be an...

  • This should be in Java Create a simple hash table You should use an array for...

    This should be in Java Create a simple hash table You should use an array for the hash table, start with a size of 5 and when you get to 80% capacity double the size of your array each time. You should create a class to hold the data, which will be a key, value pair You should use an integer for you key, and a String for your value. For this lab assignment, we will keep it simple Use...

  • Hash Tables. (Hint: Diagrams might be helpful for parts a) and b). ) When inserting into...

    Hash Tables. (Hint: Diagrams might be helpful for parts a) and b). ) When inserting into hash table we insert at an index calculated by the key modulo the array size, what would happen if we instead did key mod (array_size*2), or key mod (array_size/2)? (Describe both cases). Theory answer Here Change your hashtable from the labs/assignments to be an array of linkedlists – so now insertion is done into a linkedlist at that index. Implement insertion and search. This...

  • Write a program that allows the user to enter a series of exam scores. The number...

    Write a program that allows the user to enter a series of exam scores. The number of scores the user can enter is not fixed; they can enter any number of scores they want. The exam scores can be either integers or floats. Then, once the user has entered all the scores they want, your program will calculate and print the average of those scores. After printing the average, the program should terminate. You need to use a while loop...

  • You will create a class to keep student's information: name, student ID, and grade. The program...

    You will create a class to keep student's information: name, student ID, and grade. The program will have the following functionality: - The record is persistent, that is, the whole registry should be saved on file upon exiting the program, and after any major change. - The program should provide the option to create a new entry with a student's name, ID, and grade. - There should be an option to lookup a student from his student ID (this will...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT