【H5开发】在ts中实现一个支持泛型的HashMap类(上集):实例源代码!


话不多说,直接上码!


HashMap.ts

namespace sunnyboxs
{

     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< string, number> = new HashMap();
hashmap. add( "中国", 100);
hashmap. add( "美国", 200);
hashmap. add( "大豆", 300);
Loger. trace( hashmap. get( "中国"));
Loger. trace( hashmap. get( "美国"));
Loger. trace( hashmap. get( "大豆"));
Loger. trace( hashmap. length);
hashmap. remove( "美国");
Loger. trace( hashmap. get( "中国"));
Loger. trace( hashmap. get( "美国"));
Loger. trace( hashmap. get( "大豆"));
Loger. trace( hashmap. length);


输出的结果:(上面的trace是我的日志打印类,你可以用console.log来输出)


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

由于是泛型实现,这里的HashMap<K,V>中的K和V,可以是任意强类型哦。


接下来,测试如何遍历:

console. log( "======遍历方法1==========");
hashmap. forEachKeyValue( function ( data: KeyValue< string, number>)
{
console. log( data. key, "--->", data. value);
});

console. log( "======遍历方法2==========");
hashmap. forEach( function ( key: string, value: number)
{
console. log( key, "--->", value);
});

遍历输出结果:

======遍历方法1==========
中国 ---> 100
大豆 ---> 300

======遍历方法2==========

中国 ---> 100
大豆 ---> 300


这样,在typescript中我们就可以爽爽的用hashmap类了。

泛型强类型,在vscode里有代码提示和错误检查功能,很方便!

OK!大功告成!


猜你喜欢

转载自blog.csdn.net/sjt223857130/article/details/80376912