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'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("\nAll items: "+map.toString()+"\n");
*
* 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("\nAll items: "+map.toString()+"\n");
*
* }
*
* 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()+"\n";
}
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);
}
}
flex创建hashMap的更多相关文章
- 如何用Jpype创建HashMap和ArrayList
近期在Python中使用java语言的时候有涉及到如何创建HashMap和ArrayList等容器,最开始的疑惑是,java里面的容器是有泛型做类型检测的,而在python中却没有泛型这个说法,那么如 ...
- 阿里巴巴Java开发手册建议创建HashMap时设置初始化容量,但是多少合适呢?
集合是Java开发日常开发中经常会使用到的,而作为一种典型的K-V结构的数据结构,HashMap对于Java开发者一定不陌生. 关于HashMap,很多人都对他有一些基本的了解,比如他和hashtab ...
- 当我们创建HashMap时,底层到底做了什么?
jdk1.7中的底层实现过程(底层基于数组+链表) 在我们new HashMap()时,底层创建了默认长度为16的一维数组Entry[ ] table.当我们调用map.put(key1,value1 ...
- [翻译]Java HashMap工作原理
大部分Java开发者都在使用Map,特别是HashMap.HashMap是一种简单但强大的方式去存储和获取数据.但有多少开发者知道HashMap内部如何工作呢?几天前,我阅读了java.util.Ha ...
- hashmap实现原理浅析
看了下JAVA里面有HashMap.Hashtable.HashSet三种hash集合的实现源码,这里总结下,理解错误的地方还望指正 HashMap和Hashtable的区别 HashSet和Hash ...
- 关于Android中ArrayMap/SparseArray比HashMap性能好的深入研究
由于网上有朋友对于这个问题已经有了很详细的研究,所以我就不班门弄斧了: 转载于:http://android-performance.com/android/2014/02/10/android-sp ...
- java HashMap那点事
集合类的整体架构 比较重要的集合类图如下: 有序否 允许元素重复否 Collection 否 是 List 是 是 Set AbstractSet 否 否 HashSet TreeSet 是(用二 ...
- Java 集合系列10之 HashMap详细介绍(源码解析)和使用示例
概要 这一章,我们对HashMap进行学习.我们先对HashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用HashMap.内容包括:第1部分 HashMap介绍第2部分 HashMa ...
- java中HashMap详解
HashMap 和 HashSet 是 Java Collection Framework 的两个重要成员,其中 HashMap 是 Map 接口的常用实现类,HashSet 是 Set 接口的常用实 ...
随机推荐
- [实战]MVC5+EF6+MySql企业网盘实战(12)——新建文件夹和上传文件
写在前面 之前的上传文件的功能,只能上传到根目录,前两篇文章实现了新建文件夹的功能,则这里对上传文件的功能进行适配. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战] ...
- 三年.NET即将转Java,我该何去何从
2014年5月,大三报了某培训班5个月学习.NET 2014年12月-2015年6月,在某软件公司实习,用ASP.NET开发企业级系统 2015年7月-2017年3月,从毕业生到成为该公司的主要开发人 ...
- SQLyog-12.0.9-0.x64
SQLyog-12.0.9-0.x64 下载地址 链接:https://pan.baidu.com/s/1AjC2rdx8Bgtns1sYmN5NEw 提取码:icko 注册码: 姓名(Name):c ...
- 超简教程:Xgboost在Window上的安装(免编译)
Xboost在windows安装需要自己编译,编译的过程比较麻烦,而且需要复杂的软件环境.为了免去编译,我这里把编译好的文件上传到网盘供大家下载安装.有了编译好的文件,xgboost的安装变得超级简单 ...
- Vsftpd支持SSL加密传输
ftp传输数据是明文,弄个抓包软件就可以通过数据包来分析到账号和密码,为了搭建一个安全性比较高ftp,可以结合SSL来解决问题 SSL(Secure Socket Layer)工作于传输层和应用程 ...
- CodeForces 785A Anton and Polyhedrons
简单判断. 分别判断每个单词是几面体,加起来就是答案. #include <cstdio> #include <cmath> #include <cstring> ...
- Java多线程编程——volatile关键字
(本篇主要内容摘自<Java多线程编程核心技术>) volatile关键字的主要作用是保证线程之间变量的可见性. package com.func; public class RunThr ...
- 关于如何在 Unity 的 UI 菜单中默认创建出的控件 Raycast Target 属性默认为 false
关于如何在 Unity 的 UI 菜单中默认创建出的控件 Raycast Target 属性默认为 false 我们在 Unity 中通过 UI 菜单创建的各种控件,比如 Text, Image 等, ...
- python笔记11-多线程之Condition(条件变量)
前言 当小伙伴a在往火锅里面添加鱼丸,这个就是生产者行为:另外一个小伙伴b在吃掉鱼丸就是消费者行为.当火锅里面鱼丸达到一定数量加满后b才能吃,这就是一种条件判断了. 这就是本篇要讲的Condition ...
- Bzoj2038/洛谷P1494 小Z的袜子(莫队)
题面 Bzoj 洛谷 题解 考虑莫队算法,首先对询问进行分块(分块大小为\(sqrt(n)\)),对于同一个块内的询问,按照左端点为第一关键字,右端点为第二关键字排序.我们统计这个区间内相同的颜色有多 ...