function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Frank N.Frank N. 

Implementing custom maps using parameterized types

Hi all,

 

To optimize our code we would like to be able to create custom maps using parameterized types. For example, I want to be able to define an interface with two undefined types:

 

global interface SortedMapInterface<V,K> {}

 And then down the line be able to instantiate the class and determine the types used for the map:

 

CustomMap m = new CustomMap<Id, someObject>();

 

However, I am unsure whether APEX allows you to do so as I would need to add the map somewhere but this is not allowed:

 

Map<V, K> m = new Map<V,K>();

 

 Hopefully this make sense and someone can point me in the right direction.

 

 

ClintLeeClintLee

Have you checked out the section in the Apex docs regarding parameterized typing and interfaces?

 

Here's the link - 

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_classes_interfaces_parameterized_types.htm

 

~ Clint

Frank N.Frank N.

Hi Billy,

 

I have but it will not allow me to do what I'd like to do. Basically I want to be able to create my own 'parameterized' class for example:

 

CustomMap<Id, String> customMap = new customMap<Id, String>();

 Next, inside my custom class I want to create a new map based on types Id and Map:

 

Map<Id, String> map = new Map<Id, String>();

 

 This however, does not seem possible unless I create a pre-defined class that contains that map based on those types. If anyone does know a way of extending maps, please let me know.