zoukankan      html  css  js  c++  java
  • flex创建hashMap

    1,定义hashMap的接口。



    import flash.events.IEventDispatcher;
    import mx.events.CollectionEvent;

    /**
    *  Dispatched when the Map has been updated in some way.
    *
    *  @eventType mx.events.CollectionEvent.COLLECTION_CHANGE
    */
    [Event(name="collectionChange", type="mx.events.CollectionEvent")]

    /**
    * <code>IMap</code> is the contract for the <code>HashMapCollection</code>. The class extentiate the 
    * <code>IEventDispatcher</code> interface to insure the creating of all the classes to dispach change events.

    */
    public interface IMap extends IEventDispatcher
    {
    /**
    * Add a pair to the collection.
    * key - vale 的形式,添加数据到map中,
    * @param Map key.
    * @param Map value

    */
    function addItem(key:*, value:*):void;

    /**
    * Remove an item based on key. 
    * 根据key删除相对应的记录
    * @param key    The collection key.

    */
    function removeItemAt(key:*):void;

    /**
    * Check if the collection contains a key.
    * 检测是否包含相对应的key
    * @param key    collection key.
    * @return    true|false

    */
    function containsKey(key:*):Boolean;

    /**
    * Check if collection contain value. 
    * @param value    value from any type.
    * @return    true|false
    * 检测是否包含对应的的value
    */
    function containsValue(value:*):Boolean;

    /**
    * Return the item key based on it&apos;s value.
    * 根据value获取相应的数据记录 
    * @param value    The item value.
    * @return    The item Key.

    */
    function getItemKey(value:*):String;

    /**
    * Retrieve an item value based on its key.
    *  根据key获取相应的数据记录 
    * @param key    Key can be any type. Usually string.
    * @return    The value.

    */
    function getItemValue(key:*):*;

    /**
    * Method to retrieve the values. 
    * @return An array with all the values in the map.
    * 获取所有的value值
    */
    function getItemsValues():Array;

    /**
    * Method to check the size of the HashMap Collection.
    *  获得map的长度
    * @return    Size of the collection. 

    */
    function get length():int;


    function get isEmpty():Boolean;


    function reset():void;   


    function removeAll():void;

    /**
    * Clone the map, the keys and values themselves are not cloned.
    *  
    * @return    Returns a shallow copy.

    */        
    function clone():Object;

    /**
    * Returns an array collection of the keys contained in this map.

    * @return Returns an array view of the keys contained in this map.

    */        
    function get keySet():Array;

    /**
    * Copies all of the mappings from the specified map to this map 
    * These mappings will be replace for any mappings that this map had 
    * for the keys currently in the specified map.
    *  
    * @param m    a HashMap collection containing keys and values.

    */        
    function set addAll(m:HashMapCollection):void;

    /**
    * Compare specified key with the map value for equality. 
    */        
    function compare(key:*, value:*):Boolean;

    /**
    * Method to convert the map into a string representation consists of a list of key-value. 
    *  
    * @return Returns a string representation of this map. The string representation consists of a list of key-value. 

    */        
    function toString():String;

    /**
    * 方法是把map类型转化为Array类型。
    * */
    function toArray():Array;

    }

    2,实现hashMap接口

    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.utils.Dictionary;

    import mx.events.CollectionEvent;
    import mx.events.CollectionEventKind;

    /**
    *  Dispatched when the <code>HashMapCollection</code> has been updated in some way.
    *
    *  @eventType mx.events.CollectionEvent.COLLECTION_CHANGE
    */
    [Event(name="collectionChange", type="mx.events.CollectionEvent")]


    /**

    * Hash table based implementation of the <code>IMap</code> interface. 
    * <code>HashMapCollection</code> class makes no guarantees to the order of the map or that the order 
    * will remain the same.  The class let you pass a pair of keys and values and
    * provides constant-time performance for the basic operations of 
    * <code>addItem</code>, <code>removeItemAt</code> and <code>getItemValue</code>
    * since they require no iteration over the collection.

    * <p>
    * Example shows how to use the <code>HashMapCollection</code>
    * </p>
    *
    * @example
    * <listing version="3.0">

    *        private function map():void {
    *            
    *            var map:IMap = new HashMapCollection();
    *            map.addEventListener(CollectionEvent.COLLECTION_CHANGE, handler);
    *            
    *            map.addItem("John", "212-452-8086");
    *            map.addItem("James", "718-345-3455");
    *            map.addItem("Micheal", "917-782-8822");
    *            map.addItem("Ron", "212-426-8855");
    *            map.addItem("Mike", "212-255-2436");
    *            map.addItem("Jenny", "718-344-2433");
    *            map.addItem("Jack", "917-222-4352");
    *            map.addItem("Riki", "981-222-1122");
    *            trace(" All items: "+map.toString()+" ");
    *            
    *            trace("containsKey Jack? "+map.containsKey("Jack"));
    *            trace("containsValue 718-344-2433? "+map.containsValue("718-344-2433"));
    *            trace("getItemKey 718-344-2433: "+map.getItemKey("718-344-2433"));
    *            trace("getItemValue Jenny: "+map.getItemValue("Jenny"));
    *            
    *            map.removeItemAt("Riki");
    *            trace("Remove Riki.");
    *            trace("getItemValue Riki: "+map.getItemValue("Riki"));                
    *            trace("Comapre: "+map.compare("Ron", "212-426-8855"));
    *            
    *            map.removeAll();
    *            trace(" All items: "+map.toString()+" ");
    *            
    *        }
    *        
    *        private function handler(event:CollectionEvent):void 
    *        {
    *            trace("Event: "+event.kind);
    *        }
    *  
    * </listing>

    * @see mx.events.CollectionEvent
    * @see com.elad.framework.utils.collections
    * @see flash.events.IEventDispatcher
    * @see flash.utils.Dictionary
    *
    */
    public class HashMapCollection implements IMap
    {
    /**
    *  @private
    *  Internal event dispatcher.
    */
    private var eventDispatcher:EventDispatcher;

    /**
    * The Dictionary class is the base for the Hashtable, it allows to maps keys to value by 
    * creating a dynamic collection of properties.
    *  
    * @see flash.utils.Dictionary

    */
    private var map:Dictionary;

    /**
    * Defualt constractor create the <code>Dictionary</code> and set the event dispatcher.

    * @param useWeakReferences Instructs the Dictionary object to use "weak" references on object keys. 

    * @see flash.utils.Dictionary

    */
    public function HashMapCollection(useWeakReferences:Boolean = true)
    {
    map = new Dictionary( useWeakReferences );
    eventDispatcher = new EventDispatcher(this);
    }

    /**
    * Add a pair consists of key and value.

    */
    public function addItem(key:*, value:*) : void
    {
    map[key] = value;
    triggerDispatchEvent(new Array({key: key, value: value}) , CollectionEventKind.ADD);           

    }

    /**
    * Remove the value based on the key without the need to iteration.
    * The method also call an event dispatcher.
    */
    public function removeItemAt(key:*) : void
    {   
    triggerDispatchEvent(new Array({key: key, value: map[key]}) , CollectionEventKind.REMOVE);
    delete map[key];                        
    }

    /**
    * Method to check if a key exists in the map collection.
    */
    public function containsKey(key:*):Boolean
    {
    return map[key] != null;
    }

    /**
    * Method to check if a value exist in the map colletcion.
    */
    public function containsValue(value:*) : Boolean
    {
    var result:Boolean;

    for ( var key:* in map )
    {
    if (map[key] == value)
    {
    result = true;
    break;
    }
    }
    return result;
    }

    /**
    * Method to retrieve the list of keys avaliable in the map.

    * @return    An array collection with the keys. 

    */
    public function getKeys():Array
    {
    var keys:Array = [];

    for (var key:* in map)
    {
    keys.push( key );
    }
    return keys;
    }

    /**
    * Retrieve an item key based on a value.

    * @param value
    * @return 

    */        
    public function getItemKey(value:*):String
    {
    var keyName:String = null;

    for (var key:* in map)
    {
    if (map[key] == value) 

    keyName = key;
    break; 
    }
    }

    return keyName;     
    }

    /**
    * Retrieve item value based on the item key.

    * @param key
    * @return 

    */
    public function getItemValue(key:*):*
    {
    return map[key];
    }

    /**
    * Method to retrieve all the items values in the collection.

    * @return An array collection with all the values.

    */
    public function getItemsValues():Array
    {
    var values:Array = new Array();

    for (var key:* in map)
    {
    values.push(map[key]);
    }
    return values;
    }

    /**
    * Will result in the length of the map collection.

    * @return Size of collection. 

    */
    public function get length():int
    {
    var length:int = 0;

    for (var key:* in map)
    {
    length++;
    }
    return length;
    }

    /**
    * Check if the collection has pairs of values and keys or empty
    * @return true|false

    */
    public function get isEmpty():Boolean
    {
    return length <= 0;
    }

    /**
    * Method to clear the values of the collection. Keys will stays but values will be removed.

    */
    public function reset():void
    {
    for (var key:* in map)
    {
    map[key] = null;
    }

    triggerDispatchEvent(new Array() , CollectionEventKind.RESET);          
    }

    /**
    * Remove the entire pairs in the collection. 

    */
    public function removeAll():void
    {
    for (var key:* in map)
    {
    removeItemAt(key);
    }

    triggerDispatchEvent(new Array() , CollectionEventKind.REMOVE);            
    }

    /**
    * Clone the map, the keys and values themselves are not cloned.
    *  
    * @return    Returns a shallow copy of this HashMap instance

    */        
    public function clone():Object
    {
    var cloneMap:IMap = this;
    cloneMap.removeAll();
    return cloneMap;
    }

    /**
    * Returns an array collection of the keys contained in this map.

    * @return Returns an array view of the keys contained in this map.

    */        
    public function get keySet():Array
    {
    var keys:Array = [];

    for (var key:* in map)
    {
    keys.push( key );
    }
    return keys;
    }

    /**
    * Copies all of the mappings from the specified map to this map 
    * These mappings will replace any mappings that this map had for any of the keys currently in the specified map.
    *  
    * @param m    a HashMap collection containing keys and values.

    */        
    public function set addAll(m:HashMapCollection):void
    {
    for (var key:* in map)
    {
    if (map[key] != null)
    {
    delete map[key];
    }
    }

    for (key in m)
    {
    map[key] = m.getItemValue(key);
    }

    triggerDispatchEvent(new Array("AddAll") , CollectionEventKind.ADD);            
    }

    /**
    * Compare specified key with the map value for equality.
    */        
    public function compare(key:*, value:*):Boolean
    {            
    var result:Boolean = (map[key] == value) ? true : false
    return result;
    }

    /**
    * Method to convert the map into a string representation consists of a list of key-value. 
    *  
    * @return Returns a string representation of this map. The string representation consists of a list of key-value. 

    */        
    public function toString():String
    {
    var string:String = "";

    for (var key:* in map)
    {
    string = string + "key: " + key + ", value: " + map[key].toString()+" ";
    }

    return string;
    }

    /**
    *  把map转化成array;
    *  
    * @return Array

    */        
    public function toArray():Array
    {
    var arr:Array = new Array();


    for (var key:* in map)
    {
    var obj:Object = new Object();
    obj.key=key;
    obj.val=map[key].toString();
    arr.push(obj);
     
    }

    return arr;
    }

    /**
    * Method to dispatch the event. This method will be called every time there is any change
    * in the collection. 

    * @param array    Pairs that are being changed.
    * @param eventKind    Event kind that got initiated

    */        
    private function triggerDispatchEvent(array:Array, eventKind:String):void
    {
    // dispatch Event
    var event:CollectionEvent = new CollectionEvent(CollectionEvent.COLLECTION_CHANGE);
    event.kind = eventKind;
    event.items = array;
    this.dispatchEvent(event);                
    }

    //--------------------------------------------------------------------------
    //
    // Methods needed for the extention of IEventDispatcher
    //
    //--------------------------------------------------------------------------

    public function addEventListener(type:String, listener:Function, useCapture:Boolean = false,
    priority:int = 0, useWeakReference:Boolean = false):void
    {
    eventDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
    }

    public function dispatchEvent(event:Event):Boolean
    {
    return eventDispatcher.dispatchEvent(event);
    }

    public function hasEventListener(type:String):Boolean
    {
    return eventDispatcher.hasEventListener(type);
    }    

    public function willTrigger(type:String):Boolean
    {
    return eventDispatcher.willTrigger(type);
    }

    public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
    {
    eventDispatcher.removeEventListener(type, listener, useCapture);
    }
    }

  • 相关阅读:
    xplan.sql(本脚本获取执行计划显示执行顺序)
    闪回查询(SELECT AS OF)
    闪回事务查询
    闪回版本查询
    闪回数据库
    shell循环语句
    前后端交互json字符串
    element vuex 语音播报
    highcharts中数据列点击事件
    highcharts为X轴标签添加链接
  • 原文地址:https://www.cnblogs.com/pangblog/p/3398195.html
Copyright © 2011-2022 走看看