DoubleKeyMap_实现

本文介绍DoubleKeyMap接口及其具体实现DoubleKeyHashMap。DoubleKeyHashMap基于hashtable,提供双键映射的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

DoubleKeyMap接口类

package cs601.collections;
import java.util.List;

/** A Map (Dictionary) just like Java's Map<K,V> interface except that
 * this interface has 2 keys instead of just one. Otherwise it works
 * the same way.
 */
public interface DoubleKeyMap<K1,K2,V> {
    /** Add (key1,key2,value) to dictionary, overwriting previous
     *  value if any.  key1, key2, and value must all be non-null.
     *  If any is null, throw IllegalArgumentException.
     *  @return the previous value associated with <tt>key</tt>, or
     * <tt>null</tt> if there was no mapping for <tt>key</tt>.
     *
     */
    V put(K1 key1, K2 key2, V value);

    /** Return the value associated with (key1, key2). Return null if
     *  no value exists for those keys.  key1, key2 must be non-null.
     *  If any is null, throw IllegalArgumentException.  The value
     *  should be just the value added by the user via put(), and
     *  should not contain any of your internal bookkeeping
     *  data/records.
     */
    V get(K1 key1, K2 key2);

    /** Remove a value if present. Return previous value if any.
     *  Do nothing if not present.
     */
    V remove(K1 key1, K2 key2);

    /** Return true if there is a value associated with the 2 keys
     *  else return false.
     *  key1, key2 must be non-null. If either is null, return false.
     */
    boolean containsKey(K1 key1, K2 key2);

    /** Return list of a values in the map/dictionary.  Return an
     *  empty list if there are no values.  The values should be just
     *  the values added by the user via put(), and should not contain
     *  any of your internal bookkeeping data/records.
     */
    List<V> values();

    /** Return how many elements there are in the dictionary. */
    int size();

    /** Reset the dictionary so there are no elements. */
    void clear();
}

DoubleKeyHashMap  内部构建了一个hashtable实现

package cs601.collections;
import java.util.*;

public class DoubleKeyHashMap<K1, K2, V> implements DoubleKeyM
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值