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

  • Java you are required to add a public Object get( Object key) method into the Hashtable...

    Java you are required to add a public Object get( Object key) method into the Hashtable class. As defined in the Java documentation, the get() method returns the value with which the specified key is associated, or null if this hash table contains no record for the key. More formally, if this hash table contains a mapping from a key k to a value v such that (key.equals(k)), then this method returns v; otherwise it returns null. (There can be...

  • 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...

  • PROGRAM DESCRIPTION Implement a hash table class. Your hash table should resolve collisions by chaining and...

    PROGRAM DESCRIPTION Implement a hash table class. Your hash table should resolve collisions by chaining and use the multiplication method to generate hash keys. You may use your own linked list code from a previous assignment or you can use a standard sequence container. Partial definitions for the hash table class and a generic data class are provided (C++ and Java versions). You may use them as a starting point if you wish. If you choose to design your own...

  • 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...

  • Implement the remove() and the rehash() methods for the following externally chained hash table. Note you...

    Implement the remove() and the rehash() methods for the following externally chained hash table. Note you should rehash when "size > arraySize" at the start of the put() method. The new size of the arraySize should be "(2 * old array size) + 1" import java.util.ArrayList; import java.util.Hashtable; import java.util.LinkedList; import java.util.Map; public class ExternalChainingHashTable<K,V> { private class Mapping { private Mapping next; private K key; private V value; private Mapping(K key, V value) { this.key = key; this.value =...

  • 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...

  • -----------Python program------------------- Instructions: For this assignment, you will write complete a program that allows a customer...

    -----------Python program------------------- Instructions: For this assignment, you will write complete a program that allows a customer to plan for retirement. Part 2: Generate a Retirement Planning Table: It's hard to decide how much you need to save for retirement. To help your customer visualize how long her nest egg will last, write a program that allows the user to generate a retirement planning table showing the number of months the savings will last for various combinations of starting account balance...

  • Declare the following three variables: char letter; int number; double decimalNumber; Prompt the user to enter...

    Declare the following three variables: char letter; int number; double decimalNumber; Prompt the user to enter a letter. Read the letter into the variable   letter Assign to the variable   number   the value from the variable   letter Assign to the variable   decimalNumber   the value from the variable   number Print the values of the three variables using the following format: Character: K Number: 75 Decimal number: 75 Test the program with the input letter   a   and copy the results into a comment...

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