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);
});

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章