Question

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

returns true iff key already exists in the map.

boolean containsValue(Integer value);

returns true iff value appears in the map. (N.B. Since the values need not be unique, this

doesn’t necessarily tell you anything interesting.)

boolean remove (Object key);

If key is in the map, remove it. (This is a little bit tricky).

Integer get(String key);

If key is in the map, return the associated value.

If key is NOT in the map, return null.

Set<Pair> entrySet();

Returns a Set of all pairs in the map.

List<Integer> values();

Returns a list of all of the associated values in the map.

Set<String> keySet();

Returns a set of all keys in the map.

int size();

returns the number of elements in the map

boolean isEmpty();

returns true iff the number of elements in the map is 0.

You will use a hash table as the underlying structure.

The declaration for the private data member should be:

private ArrayList<LinkedList<Pair>> theTable;

Pair should be an inner static class with the following methods.

Pair(String str, Integer value);

Constructs a pair

String getKey();

Returns the key

Integer getValue();

Returns the value

Integer setValue(Integer value);

Changes the value to a new value and returns the old value.

As always, your code must be heavily commented and documented.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOLUTION

import java.util.HashMap;

import java.util.LinkedList;

import java.util.List;

import java.util.Map.Entry;

import java.util.Set;

public class HashMapMethods {

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

void put(String key, Integer value) {

if (!map.containsKey(key)) {

map.put(key, value);// if Key is not already present then Store the

// key-value pair

} else {

map.put(value.toString(), value);

// Else Stores into the Value

}

}

boolean containsKey(String key) {

if (map.containsKey(key))// Check if the key is already exist or not.

return true;

else

return false;

}

boolean containsValue(Integer value) {

if (map.containsValue(value))// Check if the value is already exist or

// not.

return true;

else

return false;

}

boolean remove(Object key) {

if (map.containsKey(key)) {

map.remove(key);

return true;

} else

return false;

}

Integer get(String key)

{

if(map.containsKey(key))

return map.get(key);

else

return null;

}

Set<Entry<String, Integer>> entrySet()

{

return map.entrySet();

}

List<Integer> values()

{

LinkedList<Integer>list=new LinkedList<Integer>();

list.addAll(map.values());

return list;

}

Set<String> keySet()

{

return map.keySet();

}

int size()

{

return map.size();

}

boolean isEmpty()

{

return map.isEmpty();

}

public static void main(String[] args) {

HashMapMethods obj = new HashMapMethods();

obj.map.put("Babu", 101);

obj.map.put("Ganesh", 102);

obj.map.put("Suresh", 103);

obj.map.put("Mahesh", 104);

obj.map.put("MAdhu", 105);

// Calling First Method

obj.put("Babu", 101);

// calling second Method

obj.containsKey("Babu");

// Calling Third Method

obj.containsValue(103);

//Calling Fourth Method Remove Key

System.out.println("Removed Key Successfully "+obj.remove("MAdhu"));

//Calling the Fifth Method

System.out.println("The value of the Key is : "+obj.get("Suresh"));

//Calling sixth Method

System.out.println("The Set of Key value pairs are: "+obj.entrySet());

//Calling the Seventh Method

System.out.println("The List of Value are: "+obj.values());

//Calling the Eight Method

System.out.println("The Set of Keys are: "+obj.keySet());

//Calling the 9th Method

System.out.println("The Size of the HashMAp is: "+obj.size());

//Calling 10th Method

System.out.println(" Empty or Not? "+obj.isEmpty());

}

}

********************************************************************************************************************************

Sample Output

Removed Key Successfully true

The value of the Key is : 103

The Set of Key value pairs are: [Suresh=103, Mahesh=104, 101=101, Babu=101, Ganesh=102]

The List of Value are: [103, 104, 101, 101, 102]

The Set of Keys are: [Suresh, Mahesh, 101, Babu, Ganesh]

The Size of the HashMAp is: 5

Empty or Not? false

Add a comment
Know the answer?
Add Answer to:
Use Java language You may assume that you always map strings to integers, and therefore you...
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
  • Java Quadratic Probing Hash table HELP! Complete the Map and Entry class provided in Map.java. Create...

    Java Quadratic Probing Hash table HELP! Complete the Map and Entry class provided in Map.java. Create a tester/driver class to show the Map class is working as intended. Methods/Constructor correctly completed (2pt) This is the Map class methods Map(), put(key, value), get(key), isEmpty(), makeEmpty() Private class correctly completed (2pt) This is the Entry class methods Add the methods that are required for this to work correctly when stored in the QuadraticProbingHashTable Tester class (1pt) Show the methods of the Map...

  • Can anyone helps to create a Test.java for the following classes please? Where the Test.java will...

    Can anyone helps to create a Test.java for the following classes please? Where the Test.java will have a Scanner roster = new Scanner(new FileReader(“roster.txt”); will be needed in this main method to read the roster.txt. public interface List {    public int size();    public boolean isEmpty();    public Object get(int i) throws OutOfRangeException;    public void set(int i, Object e) throws OutOfRangeException;    public void add(int i, Object e) throws OutOfRangeException; public Object remove(int i) throws OutOfRangeException;    } public class ArrayList implements List {   ...

  • a. Write a function arrayToMap that takes an array of strings and returns a std::map<int, string>...

    a. Write a function arrayToMap that takes an array of strings and returns a std::map<int, string> such that the values in the map are the string values in the array of strings, and the keys in the map are the corresponding array indices of the string values. You may assume all necessary libraries have been included in your program and your solution must be syntactically correct in order to receive full credit. map<int, string> arrayToMap(string arr[], int arrSize) { b....

  • In this assignment, you will add several methods to the Binary Search Tree. You should have compl...

    In this assignment, you will add several methods to the Binary Search Tree. You should have completed the following three methods in the lab: public void insert(Key key, Value value) public Value get(Key key) public void inorder(Node root) For this assignment, you will implement the following: public void remove(Node root, Key key) public Key getMin(Node n) public Key getMax(Node n) public int height(Node n) The main method contains the statements to check whether your implementation works. You need to change...

  • The task of this project is to implement in Java Hash Table structure using Linear Probing...

    The task of this project is to implement in Java Hash Table structure using Linear Probing Collision Strategy.   You can assume that no duplicates or allowed and perform lazy deletion (similar to BST). Specification Create a generic class called HashTableLinearProbe <K,V>, where K is the key and V is the value. It should contain a private static class, HashEntry<K,V>. Use this class to create array to represent Hashtable:             HashEntry<K,V> hashtable[]; Implement all methods listed below and test each method...

  • Comparing the insertion and removing performance between ALPQueue with HeapPQueue. Requirements: ...

    Comparing the insertion and removing performance between ALPQueue with HeapPQueue. Requirements: 1/ Task1: implement a concrete ALPQueue class (an ArrayList based priority queue) that extends the AbstractPQueue discussed in the class, along with PQueue.java, ArrayList.java, List.java, PQEntry.java , EntryComparitor.java, and OutOfRangeException.java given in the class (any other implementation of above classes that are not compatible with List interface and AbstractPQueue class will not receive any credit). 2/ Task2: implement a concrete HeapPQueue class (a Heap based priority queue) that extends...

  • Comparing the insertion and removing performance between ALPQueue with HeapPQueue. Requirements: 1/ Task1: implement a concrete...

    Comparing the insertion and removing performance between ALPQueue with HeapPQueue. Requirements: 1/ Task1: implement a concrete ALPQueue class (an ArrayList based priority queue) that extends the AbstractPQueue discussed in the class, along with PQueue.java, ArrayList.java, List.java, PQEntry.java , EntryComparitor.java, and OutOfRangeException.java given in the class (any other implementation of above classes that are not compatible with List interface and AbstractPQueue class will not receive any credit). 2/ Task2: implement a concrete HeapPQueue class (a Heap based priority queue) that extends...

  • Removing Nodes from a Binary Tree in Java This section requires you to complete the following...

    Removing Nodes from a Binary Tree in Java This section requires you to complete the following method within BinaryTree.java: public void remove(K key) { } The remove method should remove the key from the binary tree and modify the tree accordingly to maintain the Binary Search Tree Principle. If the key does not exist in the binary tree, no nodes should be removed. In case there is a non-null left child, it should take the place of the removed node....

  • Can someone help me with this code, I not really understanding how to do this? import...

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

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