0

I must read in a file with a String key and two String values into a two-way hashmap. How do I go about implementing the put() method to add the key and the values into the hashmap?

After hours of research, I only found one example of using forward and backward.

backward.put(value, key);
return forward.put(key, value)

Unfortunately, it simply gives me null. Any guidance is greatly appreciated.


Here's the class. I do not want you guys to do the assignment for me. I just need help implementing the put() method...that's all. I just need a bit of guidance in the right direction.

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@SuppressWarnings("serial")
public class TwoWayHashMap<K, V> extends HashMap<K, V> 
{
// Declare all the data members and instance variables
// that are required for this class

/** 
 * Construct an empty two-way hash map.
 */
public TwoWayHashMap() 
{
    // Provide your definition/implementation of this constructor
}

/** 
 *  Construct a two-way hash map with the given hash map, which must have a
 *  one-to-one relationship between elements.
 *
 *  @param map The hash map.
 */
public TwoWayHashMap(Map<? extends K, ? extends V> map) 
{
    // Provide your definition/implementation of this constructor
}

/** 
 *  Construct an empty two-way hash map with an initial capacity.
 *
 *  @param initialCapacity The initial capacity.
 */
public TwoWayHashMap(int initialCapacity) 
{
    // Provide your definition/implementation of this constructor
}

/** 
 *  Construct an empty two-way hash map with an initial capacity and a load
 *  factor.
 *
 *   @param initialCapacity The initial capacity.
 *   @param loadFactor The load factor.
 */
public TwoWayHashMap(int initialCapacity, float loadFactor) 
{
    // Provide your definition/implementation of this constructor
}


/** 
 *  Clear this two-way hash map.
 */
@Override
public void clear() 
{
    // Provide your definition/implementation of this method
}

/** 
 *  Clone this two-way hash map and return the clone.
 *
 *  @return The clone.
 */
@Override
public Object clone() 
{
    // Provide your definition/implementation of this method
}

/** 
 *  Test whether this two-way hash map contains the given value.
 *
 *  @param value The value.
 *  @return true if the value is contained.
 */
@Override
public boolean containsValue(Object value) 
{
    // Provide your definition/implementation of this method
}

/** 
 *  Given a value, return the corresponding key in this two-way hash map.
 *
 *  @param value The value.
 *  @return the key.
 */
public K getKey(Object value) 
{
    // Provide your definition/implementation of this method
}

/** 
 *  Put a value into this two-way hash map and associate it with a key.
 *
 *  @param key The key.
 *  @param value The value.
 *  @return The value previously associated with the key in this two-way
 *   hash map.
 */
@Override
public V put(K key, V value) 
{
    // Provide your definition/implementation of this method
}

/** 
 *  Remove the value associated with the given key.
 *
 *  @param key The key.
 *  @return The removed value, or null if not found.
 */
@Override
public V remove(Object key) 
{
    // Provide your definition/implementation of this method
}

/**
 *  Returns the inverse view of this TwoWayHashMap, which maps each 
 *  of this TwoWayHashMap's values to its associated key.
 * 
 *  @return the inverse view of this TwoWayHashMap
 */
public HashMap<V, K> inverse()
{
    // Provide your definition/implementation of this method
}

/** 
 *  Return a set containing all the values in this two-way hash map.
 *
 *  @return The set.
 */
@Override
public Set<V> values() 
{
    // Provide your definition/implementation of this method
}

/**
 * Return a string representation of this object
 *
 * @return The string object
 */
@Override
public String toString() 
{   
    // Provide your definition/implementation of this method
}

}

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Eddy
  • 15
  • 5
  • the put method returns the existing value in the map, which didn't exist when you called forward.put() - you dont need to take any notice of the value returned here. you will use get() methods AFTER you have populated your maps. – slipperyseal Nov 23 '15 at 00:00
  • So am I using the right method in order to populate my map? – Eddy Nov 23 '15 at 00:05
  • yep. see my answer... – slipperyseal Nov 23 '15 at 00:07
  • Is `TwoWayHashMap` your own custom class? Does it implement `Map`? Also, the explanation mentions two values, but the code only one. I think we need more information to answer this. – Paul Boddington Nov 23 '15 at 00:09
  • This is a template that I must follow. It also has a separate class for the two values, which contains a constructor, toString() and equals(). I just need help with implementing the put() method into a TwoWayHashMap. The rest, I think, I can figure out. – Eddy Nov 23 '15 at 00:18
  • maybe, make a bean which contains key and value and then have the put method: super.put(key, bean); super.put(value,bean); so that the bean is keyed on both the key and the value. calling get with either the key or the value will return the same bean – slipperyseal Nov 23 '15 at 03:54

2 Answers2

1

You are on the right track, but it looks like you are trying to do a put and a get in the same operation. The put method returns the existing value in the map, which didn't exist when you called forward.put() - you dont need to take any notice of the value returned here. you will use get() methods AFTER you have populated your maps.

public void add(Object key, Object value) {
    forward.put(key, value);
    backward.put(value, key);
}

public Object getForward(Object key) {
    return forward.get(key);
}

public Object getBackward(Object key) {
    return backward.get(key);
}

The fact that the map put() returns the existing value is simply for special cases where you actually wanted to know what value existed before it was replaced. It's not commonly used and not what you need here.

slipperyseal
  • 2,728
  • 1
  • 13
  • 15
  • It's more complicated than this. For example, if you do `add(1, "X")` and then `add(1, "Y")`, `forward` will have one mapping (`1=Y`) whereas `backward` will have 2 (`X=1` and `Y=1`). – Paul Boddington Nov 23 '15 at 00:14
  • you are right, but I'm going to guess the exercise he was given involves "read this text file in and map the name value pairs in both directions". based on the actual question asked i doubt the exercise involves multidimensional relationships. – slipperyseal Nov 23 '15 at 00:18
  • actually, i just noticed the "two string values" part of the question. so, yes, i think some detail is missing from the question to answer it properly. – slipperyseal Nov 23 '15 at 00:20
  • You're probably right that this is giving the OP enough info to answer problem though. – Paul Boddington Nov 23 '15 at 00:21
  • Would it help if I included the empty template with all of the methods that I'm expected to implement? – Eddy Nov 23 '15 at 00:23
  • it would, but then i'd be doing your assignment for you :) – slipperyseal Nov 23 '15 at 00:24
  • I do not want you guys to do the assignment for me. I just need help implementing the put() method...that's all. I just need a bit of guidance in the right direction. – Eddy Nov 23 '15 at 00:41
0

Have you looked into guava https://code.google.com/p/guava-libraries/? I would highly suggest looking into using this library as an alternative, it's put out by google and has a lot of other cool stuff. If you really want to roll your own then you could still checkout the putInBothMaps() method in this class https://code.google.com/p/guava-libraries/source/browse/guava/src/com/google/common/collect/AbstractBiMap.java?spec=svn7178ea3850dddda7acdf8f5709984764d2ae87d8&name=refs/remotes/origin/release12&r=7178ea3850dddda7acdf8f5709984764d2ae87d8

Charles Durham
  • 2,445
  • 16
  • 17
  • This was not talked about in class. I do not want to over-complicate the assignment. Thank you for the suggestion but there must be an easier and more straightforward way. – Eddy Nov 23 '15 at 00:21