首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在TwoWayHashMap中实现put()方法?

如何在TwoWayHashMap中实现put()方法?
EN

Stack Overflow用户
提问于 2015-11-22 23:53:36
回答 2查看 434关注 0票数 0

我必须将一个带有字符串键和两个字符串值的文件读入双向哈希映射中。如何实现put()方法以将键和值添加到hashmap中?

经过几个小时的研究,我只发现了一个向前和向后使用的例子。

代码语言:javascript
复制
backward.put(value, key);
return forward.put(key, value)

不幸的是,它只是给了我空。如有任何指导,将不胜感激。

这是课程。我不想你们替我做这个任务。我只需要帮助实现put()方法.仅此而已。我只是需要一些正确方向的指导。

代码语言:javascript
复制
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
}

}

EN

回答 2

Stack Overflow用户

发布于 2015-11-23 00:07:06

你在正确的轨道上,但看起来你在尝试做一个put和一个get在相同的操作。put方法返回映射中的现有值,在调用forward.put()时这个值不存在--您不需要注意这里返回的值。在填充映射之后,您将使用get()方法。

代码语言:javascript
复制
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);
}

map put()返回现有值这一事实只是针对特殊情况,在这些特殊情况下,您实际上想知道在替换之前存在什么值。它不是常用的,也不是你在这里需要的。

票数 1
EN

Stack Overflow用户

发布于 2015-11-23 00:19:03

你查过番石榴https://code.google.com/p/guava-libraries/吗?我强烈建议考虑使用这个库作为一个替代,它是由谷歌推出,并有许多其他很酷的东西。如果您真的想自己滚,那么仍然可以在这个类putInBothMaps()中签出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方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33861744

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档