Represents a strongly-typed collection of key-value pairs (map) backed by a hash table. A map contains unique keys, where each key is associated with a value. The values are not required to be unique. The key-value pairs make up the elements in the map collection. The ordering of the elements in the map collection is not important.

The class is final and cannot be extended.

Serializable:

Yes

Constructor(s)

The following is the default constructor, which creates an instance of HashMap with a default initial capacity (3) and a default equality comparer.. The default equality comparer requires that the elements added to the collection implement the Progress.Collections.IHashable interface. Note that that requirement is enforced later while trying to add key-value pairs to the HashMap collection.
PUBLIC HashMap( )
The following constructor returns an instance of HashMap with the specified equality comparer. The comparer is used by the collection to get the hash code of the key object and also to determine if the collection already contains the given key. The constructor raises an error if the comparer object is not valid, or if the object does not implement IEqualityComparer<K> (if invoked dynamically), where K is the same type as in HashMap<K,V>.
PUBLIC HashMap(INPUT comparer AS Progress.Collections.IEqualityComparer<K>)
The following constructors are variations of those described above, where the constructors take an additional parameter that defines the initial capacity of the hash table for the collection. The capacity value must be greater than or equal to zero (0), otherwise the constructor raises an error.
PUBLIC HashMap(INPUT initialCapacity AS INTEGER)

PUBLIC HashMap(INPUT comparer AS Progress.Collections.IEqualityComparer<K>,
               INPUT initialCapacity AS INTEGER)

Super Class

Progress.Lang.Object class

Interfaces

Progress.Collections.IMap<K,V> interface

Public Properties

Count property (Collections) IsEmpty property (Collections)
Keys property

Public Methods

Add( ) method (Map Collections) AddAll( ) method (Map Collections)
Clear( ) method (Collections) Contains( ) method (Map Collections)
ContainsKey( ) method GetIterator( ) method (Collections)
GetValue( ) method (Map Collections) Remove( ) method (Map Collections)
Set( ) method (Map Collections) TryGetValue( ) method

Public Events

This class does not contain events.

Examples

The following example shows how to instantiate a HashMap using the default constructor and the default equality comparer.
USING Progress.Collections.*.

VAR HashMap<Employee, Manager> employeeMap.

// This is ok because Employee implements IHashable
employeeMap = NEW HashMap<Employee, Manager>().
The following example shows how to instantiate a HashMap using a custom equality comparer.
USING Progress.Collections.*.

VAR HashMap<Employee, Manager> employeeMap.

// This one uses EmployeeEQComparer to hash the key elements
employeeMap = NEW HashMap<Employee,Manager>(NEW EmployeeEQComparer()).
The following example shows how to create, manage, and iterate over a HashMap. The example uses an Employee class (shown) and assumes a class called Manager (not shown).
Employee.cls
CLASS Employee IMPLEMENTS Progress.Collections.IHashable:

  CONSTRUCTOR Employee(pcID AS CHARACTER):
    EmployeeID = pcID.
  END.

  DEFINE PROPERTY EmployeeID AS CHARACTER GET. PRIVATE SET.

  METHOD PUBLIC INTEGER HashCode(). 
    // Hash on the value of property EmployeeID
    RETURN HASH-CODE(THIS-OBJECT:EmployeeID).
  END.

  METHOD PUBLIC OVERRIDE LOGICAL Equals(otherObj AS Progress.Lang.Object): 
    IF NOT VALID-OBJECT(otherObj) OR
      otherObj:GetClass() NE THIS-OBJECT:GetClass() THEN
      RETURN FALSE.

     // It’s up to the implementation to decide if case-sensitiveness
     // is required or not. This is case-insensitive.
     IF THIS-OBJECT:EmployeeID = CAST(otherObj, Employee):EmployeeID
     THEN
       RETURN TRUE.
     RETURN FALSE.
  END.
  
END.
VAR Progress.Collections.IMap<Employee,Manager> employeeMap.
VAR Progress.Collections.HashMapIterator<Employee,Manager> iterator.
VAR Employee employee1.
VAR Manager  manager1.
VAR Progres.Collections.KeyValuePair<Employee,Manager> pair.

employeeMap = NEW Progress.Collections.HashMasp<Employee,Manager>().

// Add 4 key-value pairs to the collection
employeeMap:Add(NEW Employee("thomas1","Thomas"), NEW Manager("George")).
employeeMap:Add(NEW Employee("paul1", "Paul "), NEW Manager("George")).
employeeMap:Add(NEW Employee("george1", "George"), NEW Manager("John")).
employeeMap:Add(NEW Employee("john1", "John"), NEW Manager("Tim")).

// Same key will not be added to the collection
IF employeeMap:Add(NEW Employee("george1", "George"), NEW Manager("bruce1","Bruce")) EQ FALSE THEN
   MESSAGE "George is already in the map".

// Print out the number of pairs in the set (4)
MESSAGE "Count:" employeeMap:Count.

employee1 = NEW Employee("paul1", "Paul").
// Remove the pair using a given key if it exists
IF employeeMap:ContainsKey(employee1) THEN 
   employeeMap:Remove(employee1).

// John now reports to David
manager1 = NEW Manager("john1", "John").
employeeMap:Set(manager1, NEW Manager("david1", "David")).

// Using a KeyValuePair is also allowed
// Paul was removed above. Add Paul reporting to John
pair = NEW Progress.Collections.KeyValuePair<Employee,Manager>(employee1,
           manager1). 

IF employeeMap:Add(pair1) EQ FALSE THEN
   MESSAGE "Paul is already in the map".

// If hashmap contains key and value in pair, then remove it
pair = NEW Progress.Collections.KeyValuePair<Employee,Manager>(employee1,
           NEW Manager("david1", "David")).
// This won’t find it or remove it because there is no pair 
// with Paul and David.
IF employeeMap:Contains(pair) THEN 
   employeeMap:Remove(pair).

// Iterate over the entries in the map
// which should show the following in no particular order
// Thomas -> George
// Paul -> John
// George -> John
// John -> David
iterator = CAST(employeeMap:GetIterator() 
           Progress.Collections.HashMapIterator<Employee,Manager>).
REPEAT WHILE iterator:MoveNext():
MESSAGE iterator:Current:Key:FullName “->” 
        iterator:Current:Value:FullName.
END.

Notes

  • The default initial capacity of the hash-table (number of buckets) is 3. If you can estimate the size of the HashMap, specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the collection. Note that the HashMap implementation never shrinks the hash table, if pairs are removed or the HashMap is cleared.
  • The HashMap implementation supports any object-oriented ABL object as keys and values. This includes .NET objects, however you must specify your own equality comparer when adding .NET objects to the HashMap.

See also

<T> Generic type reference, Progress.Collections.IEqualityComparer<T> interface, Progress.Collections.IHashable interface