Je dois lire dans un fichier avec une clé String et deux valeurs String dans un hashmap bidirectionnel. Comment puis-je implémenter la méthode put() pour ajouter la clé et les valeurs dans la hashmap?Comment implémenter la méthode put() dans TwoWayHashMap?
Après des heures de recherche, je ne trouve un exemple d'utilisation avant et arrière.
backward.put(value, key);
return forward.put(key, value)
Malheureusement, il me donne simplement nulle. Toute orientation est grandement appréciée.
Voici la classe. Je ne veux pas que vous fassiez le travail pour moi. J'ai juste besoin d'aide pour implémenter la méthode put() ... c'est tout. J'ai juste besoin d'un peu d'orientation dans la bonne 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
}
}
la méthode put renvoie la valeur existante dans la carte, qui n'existait pas lorsque vous avez appelé forward.put() - vous ne besoin de prendre connaissance de la valeur renvoyée ici. vous utiliserez les méthodes get() APRÈS avoir peuplé vos cartes. – slipperyseal
Alors, est-ce que j'utilise la bonne méthode pour remplir ma carte? – Eddy
oui. voir ma réponse ... – slipperyseal