Associative array/Creation: Difference between revisions

Added Apex Example
(Added Apex Example)
Line 181:
values: (16rff0000, 16r00ff00, 16r0000ff, 16r337733, 16r000000)
</pre>
 
=={{header|Apex}}==
Apex provides a Map datatype that maps unique keys to a single value. Both keys and values can be any data type, including user-defined types. Like Java, equals and hashCode are used to determine key uniqueness for user-defined types. Uniqueness of sObject keys is determined by comparing field values.
 
Creating a new empty map of String to String:
<lang apex>// Cannot / Do not need to instantiate the algorithm implementation (e.g, HashMap).
Map<String, String> strMap = new Map<String, String>();
strMap.put('a', 'aval');
strMap.put('b', 'bval');
 
System.assert( strMap.containsKey('a') );
System.assertEquals( 'bval', strMap.get('b') );
// String keys are case-sensitive
System.assert( !strMap.containsKey('A') );</lang>
 
Creating a new map of String to String with values initialized:
<lang apex>Map<String, String> strMap = new Map<String, String>{
'a' => 'aval',
'b' => 'bval'
};
 
System.assert( strMap.containsKey('a') );
System.assertEquals( 'bval', strMap.get('b') );</lang>
 
=={{header|APL}}==
Anonymous user