[SalesForce] Apex equivalent to associative array

I have the following list definition in my Apex controller:

List<Object> mylist = new List<Object>();

And I would like to do something like:

mylist.add({
    key : objKey,
    label : objlabel,
    property1 : objproperty1
});

This code gives an error. Is there any way to achieve such a data structure in Apex?

Best Answer

You need to create a Wrapper class for this.

Example :

public class TestWrapper {

    public static void  pushValue(){
        list<fieldWrapper> n = new list<fieldWrapper>();

        fieldWrapper f = new fieldWrapper();
        f.key = 'a';
        f.label = 'b';
        f.property1 = 'c';
        n.add(f);
        system.debug('DD'+n);
    }

    public class fieldWrapper{
        public String key;
        public String label;
        public String property1;
    }

}