PeopleCode中使用Map

本文介绍了一个简单的Hashtable类实现,用于基于字符串键存储和查找对象。包括构造函数、put、get、getKeys、getValues和isKey等方法,以及getKeyPosition私有方法。提供了对象存储和检索的完整流程。

转:https://blog.youkuaiyun.com/jl19880317/article/details/8198420

/** 
*@author Vinay Vegunta 
*@date 03/09/2004 

*This class provides a simple Hashtable implementation for storing and looking up objects based on String keys 
*/ 
 
class Hashtable 
   method Hashtable(); 
   method Put(&key As string, &value As object); 
   method Get(&key As string) Returns object; 
   method GetKeys() Returns array of string; 
   method IsKey(&key As string) Returns boolean; 
   method GetValues() Returns array of object; 
private 
   method GetKeyPosition(&key As string) Returns number; 
   instance array of string &keyArray; 
   instance array of object &valueArray; 
    
end-class; 
 
/* 
*Constructor 
*/ 
method Hashtable 
   /*Instantiate the arrays */ 
   &keyArray = CreateArrayRept("", 0); 
   &valueArray = CreateArrayRept( Null, 0); 
end-method; 
 
/** 
*Put a value in the Hashtable. If the key already exists, it's value will be replaced with a new one . 
*/ 
method Put 
   /+ &key as String, +/ 
   /+ &value as Object +/ 
   Local number &nbr_pos = %This.GetKeyPosition(&key); 
   /**Check if a new entry is to be made or an old value has to be replaced **/ 
   If (&nbr_pos <> - 1) Then 
      &keyArray [&nbr_pos] = &key; 
      &valueArray [&nbr_pos] = &value; 
   Else 
      &keyArray.Push(&key); 
      &valueArray.Push(&value); 
   End-If; 
end-method; 
 
/** 
*Get the value for a given key. Returns null if no value is found . 
*/ 
method Get 
   /+ &key as String +/ 
   /+ Returns Object +/ 
   Local number &nbr_pos = %This.GetKeyPosition(&key); 
   If (&nbr_pos <> - 1) Then 
      Return &valueArray [&nbr_pos]; 
   End-If; 
   Return Null; 
end-method; 
 
/** 
*Returns the  array of keys  
*/ 
method GetKeys 
   /+ Returns Array of String +/ 
   /**Return a clone of the array so that the original array cannot be tampered with **/ 
   Return &keyArray.Clone(); 
end-method; 
 
/** 
*Returns the array of values   
*/ 
method GetValues 
   /+ Returns Array of Object +/; 
   /**Return a clone of the array so that the original array cannot be tampered with **/ 
   Return &valueArray.Clone(); 
end-method; 
 
/** 
*Returns a bolean indicating if the provided String is a key or not  
*/ 
method IsKey 
   /+ &key as String +/ 
   /+ Returns Boolean +/ 
   Local number &nbr_pos = %This.GetKeyPosition(&key); 
   If (&nbr_pos <> - 1) Then 
      Return True; 
   End-If; 
   Return False; 
end-method; 
 
/** 
*Returns the position in the array of the requested key. Will return -1 if the key is not present . 
*/ 
method GetKeyPosition 
   /+ &key as String +/ 
   /+ Returns Number +/ 
    
   Local number &nbr_Len = &keyArray.Len; 
   Local number &i; 
   For &i = 1 To &nbr_Len 
      If (Exact(&key, &keyArray [&i])) Then 
         Return &i; 
      End-If; 
   End-For; 
   Return - 1; 
end-method;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值