gpt4 book ai didi

java - 如何在双向HashMap中实现put()方法?

转载 作者:行者123 更新时间:2023-12-01 10:51:24 28 4
gpt4 key购买 nike

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

经过几个小时的研究,我只找到了一个使用向前向后的示例。

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

不幸的是,它只是给了我null。非常感谢任何指导。

<小时/>

这是类(class)。我不想让你们帮我做作业。我只需要帮助实现 put() 方法......仅此而已。我只需要一些正确方向的指导。

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
}

}

最佳答案

您的方向是正确的,但您似乎正在尝试在同一操作中执行 put 和 get 操作。 put 方法返回映射中的现有值,当您调用forward.put() 时该值并不存在 - 您无需注意此处返回的值。填充 map 后,您将使用 get() 方法。

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() 返回现有值的事实只是针对特殊情况,您实际上想知道在替换之前存在什么值。它不常用,也不是您所需要的。

关于java - 如何在双向HashMap中实现put()方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33861744/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com