TypeScript 实现 HasMap

HasMap.ts

namespace shuye2017
{

    export interface KeyValue<K, V>
    {
        key: K,
        value: V
    }

    /**
     * HashMap泛型实现
     */
    export class HashMap<K, V>
    {
        //存储列表
        private _list: KeyValue<K, V>[];

        constructor()
        {
            this.clear();
        }

        //通过key获取索引
        private getIndexByKey(key: K): number
        {
            var count: number = this._list.length;
            for (let index = 0; index < count; index++)
            {
                const element: KeyValue<K, V> = this._list[index];
                if (element.key == key)
                {
                    return index;
                }
            }
            return -1;
        }

        /**
         * 添加键值
         */
        public add(key: K, value: V): void
        {
            var data: KeyValue<K, V> = { key: key, value: value };
            var index: number = this.getIndexByKey(key);
            if (index != -1)
            {
                //已存在:刷新值
                this._list[index] = data;
            }
            else
            {
                //不存在:添加值
                this._list.push(data);
            }
        }

        /**
         * 删除键值
         */
        public remove(key: K): any
        {
            var index: number = this.getIndexByKey(key);
            if (index != -1)
            {
                var data: KeyValue<K, V> = this._list[index];
                this._list.splice(index, 1);
                return data;
            }
            return null;
        }

        /**
         * 是否存在键
         */
        public has(key: K): boolean
        {
            var index: number = this.getIndexByKey(key);
            return index != -1;
        }

        /**
         * 通过key获取键值value
         * @param key
         */
        public get(key: K): V
        {
            var index: number = this.getIndexByKey(key);
            if (index != -1)
            {
                var data: KeyValue<K, V> = this._list[index];
                return data.value;
            }
            return null;
        }

        /**
         * 获取数据个数
         */
        public get length(): number
        {
            return this._list.length;
        }


        /**
         * 遍历列表,回调(data:KeyValue<K, V>)
         */
        public forEachKeyValue(f: { (data: KeyValue<K, V>): void })
        {
            var count: number = this._list.length;
            for (let index = 0; index < count; index++)
            {
                const element: KeyValue<K, V> = this._list[index];
                f(element);
            }
        }

        /**
         * 遍历列表,回调(K,V)
         */
        public forEach(f: { (key: K, value: V): void })
        {
            var count: number = this._list.length;
            for (let index = 0; index < count; index++)
            {
                const element: KeyValue<K, V> = this._list[index];
                f(element.key, element.value);
            }
        }

        /**
         * 清空全部
         */
        public clear(): void
        {
            this._list = [];
        }
    }
}

使用案例

//申明
var hashmap: HashMap<number, string> = new HashMap();

//增删改查如下
//增
hashmap.add(1, "测试一");
hashmap.add(2, "测试二");
hashmap.add(3, "测试三");
hashmap.add(4, "测试四");
//删
hashmap.remove(1);
//改
hashmap.add(1,"测试一(改)");
//查
hashmap.get(1);

//for 循环
hashmap.forEach(function (key: string, value: number)
{
    console.log(key, "--->", value);
});
hashmap.forEachKeyValue(function (data: KeyValue<string, number>)
{
    console.log(data.key, "--->", data.value);
});

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值