22

The following methodology may be somewhat faulty but demonstrates a large difference. The get approach is more than 4 times slower. Square Brackets List<Integer> numbers = new List<Integer>(); for (Integer i = 0; i < 1000000; i++) numbers.add(i); Integer x; Long start = Datetime.now().getTime(); for (Integer i = 0; i < 1000000; i++) x = ...


22

I don't know of an official reason "why," but there's an idea you can vote on. I'll be sure to ask about it when I get a chance... As I posted in that idea a few years ago, there's a quick one-liner you can use that's (relatively) faster than a straight loop, modified to be polymorphic: Boolean listContains(Object[] source, Object target) { return (new ...


21

Here's a clever (ugly) one-liner. Set<Id> ids = new Set<Id>{'001C000001DgWjE','001C000001DgWjD'}; // Here's the one line! Set<String> idStrs = (Set<String>)JSON.deserialize(JSON.serialize(ids), Set<String>.class); System.debug('idStrings=' + idStrs);


20

In short the answer is yes! To understand why you need to understand the language concept of passing by value and passing by reference. The Apex developers guide has this to say about this concept here. In Apex, all primitive data type arguments, such as Integer or String, are passed into methods by value. This means that any changes to the arguments ...


20

This cuts down on noise in the code and so is worth doing whether or not there is any performance benefit: List<Special_Codes__c> listOfCodes = new List<Special_Codes__c>{ new Special_Codes__c(Name='A1', Meaning__c='Special', Type__c='Customer Code'), new Special_Codes__c(Name='A2', Meaning__c='Bad', Type__c='Customer Code'), ...


17

The List<ANY> class implements Iterable<ANY>, but neither Map<ANY, ANY> nor Set<ANY> does. Try the following code in v30: System.debug((new set<id>()) instanceOf Iterable<Object>); System.debug((new list<id>()) instanceOf Iterable<Object>); System.debug((new map<id, object>()) instanceOf Iterable<...


16

The structure itself is fine, if you need to use it, but make sure you've commented it sufficiently. Sometimes the only way to get the performance you need is to build relatively massive structures. However, your code is overly complex, and would greatly benefit from a few changes: Don't Use Large Getters This is terribly hard to read and will likely trip ...


15

One reason may be that Set<SObject> is a risky mechanism to use: equality is based on all the fields (so is expensive) and if fields are changed logic can easily break. Same problem using SObject as a Map key. So not a pattern to be encouraged. But perhaps just because also supporting Set<SObject> adds a bunch of extra methods that need ...


13

I couldn't find a direct one line conversion but below is a way to convert a set of ids to a set of strings without using a for loop. I'm assuming you wanted a one liner but here you go anyway! Set<Id> setIds = new Set<Id>(); List<Id> setIdList = new List<Id>(); setIdList.addAll(setIds); String Ids = String.join(setIdList, ','); ...


13

You can explicitly cast from List<Id> to List<String> and vice-versa and use that to convert Sets between types by converting the Set to a List first and passing the List a new Set<>( List<> ) call. Here's the simplest single-line method to convert from a Set of Ids to a Set of Strings, where idSet is defined as Set<Id> idSet: Set&...


12

I tried a simplified version of the query using the Account to Contact parent-to-child relationship. With this I was able to access the Contacts for each Account. List<Account> queryResults = [Select Id, (Select Id from Contacts) from Account limit 10]; for (Account a : queryResults) { for (Contact c : a.Contacts) { System.debug(c); } }...


12

After doing a deeper dive with google, I think I have the answer. It turns out that this is a behavior defined by Java to ensure type safety. In Java, a Set<String> is a 'Generic' collection. Generics in Java cannot be up-cast (generics are not covariant) because (in part) Java erases the type information (of generics) upon compilation to bytecode (...


11

I wasn't satisfied with the accepted answer, specifically relating to the VisualForce aspect of it, so I performed some additional testing on this critical fix with regard to VF. What I learned is that when a VF iterator (apex:pageBlockTable, apex:repeat, etc.) is used against a Map, one of its undescribed actions is to sort the keys. This completely ...


11

You can't cast ANY to a more specific type, so you have to actually obtain the type that you want to return. Without proper reflection, you have to tell the function what type you'd like to return. There's several ways you can do that, but here's one way: public class Parser { public static List<List<Object>> splitList(List<Object> ...


9

The map's value (ProductWrapper) is incompatible with the list's value (Product2). You'd have to extract the values manually: for(ProductWrapper wrapper: mapProducts.values()) { productList.add(wrapper.product); }


9

Both String and Id are primitive data types in apex. Consider below scenario: public class poc { static List<Id> ids = new List<Id>(); public static void check() { try { ids.add('invalid_id'); System.debug('ids => '+ids); } catch(Exception ex) { System.debug('ERROR => '+ex....


8

The time reporting in the original code appears broken. (The code in the earlier answer.) Running the code below shows that there isn't a significant difference in performance, both reporting 18 to 22 ms with the "=>" approach normally being faster than the "put" approach. @isTest Public Class speed{ public void try1(){ Map<string,string>...


8

You can now use List.contains() with Spring 18 https://releasenotes.docs.salesforce.com/en-us/spring18/release-notes/rn_apex_new_classes_methods.htm#rn_apex_new_classes_methods


8

The Set collection type does not allow you to implicitly cast, unlike with List. You can, however, overload the signature to work around the issue. You have to do some tricky maneuvering to genericize the collection, but it can be done. public class Demo { public Boolean doStuff(Set<Object> values) { return values.isEmpty(); } ...


7

If you choose to use the <apex:pageBlockTable /> to render your table you are normally limited to 1000 items in the collection that it iterates over. If you set the readOnly attribute on the <apex:page tag with a true value - this limit is increased to 10,000 records in the collection. <apex:page readOnly="true" ...> <!-- your markup ...


7

Try adding this instead of your last line of code: String jsonstr = (String)JSON.serialize(parameters.get('SObject')); SObject restoredAcme = (Account)JSON.deserialize(jsonstr, Account.class); The problem with what you're currently trying to do is that you're essentially trying to cast a Map to an Account - which as you've seen, you cannot do. As a ...


7

One case is this sfdcfox answer to a question about how to catch duplicate records. In that case equality is defined as a record that has the same first name, last name and email address. Rather than having to compare those three fields for every entry in an existing list, the code can just see if the set of existing keys contains the new key using one line ...


7

You have to loop through and use addAll. There is no shortcut. Set<String> allValues = new Set<String>(); for (Set<String> subset : st.values()) { allValues.addAll(subset); }


6

There are positives and negatives to creative data structures like this. As you mentioned, this allows you to use a better algorithm when you need to do work on these records. Thats great. The downside is that someone has to maintain it. How long will it take them to wrap their head around what you have done and why? In a professional, enterprisey world ...


6

Bellow is not correct answer. For calculating time lapsed we should use System.currentTimeMillis() and not datetime.now() . For correct answer please refer Keith C post Here is how: Created a following class: Public Class speed{ public void try1(){ Map<string,string> shortStudentMap; shortStudentMap = new Map<string,string>(); ...


6

My suggestion: just turn it on and test it on a sandbox or developer org. Should become pretty clear. Update: reading MarkPonds comment on behavior in VF, I would recommend to stick with the current pattern sfdcfox describes using sorted lists from keysets as iterators. The weird and incomplete documentation might be a hint that this isn't shipped as a big ...


5

The special map constructor only handles the case of creating a map where the key is the ID and the value is the SObject. To do what I think you want to do requires a loop: Map<Id, List<Custom__c>> m = new Map<Id, List<Custom__c>>(); for (Custom__c c : [ SELECT Lookup_To_Custom__c, Id, Name FROM Custom__c ...


5

Looking at the documentation you can put an object (Map> is an object) and hence we can use complex types as well . You can definitely put map> into the Org Cache Apex code public class OrgCacheController { // Constructor of the controller for the Visualforce page. public OrgCacheController() { } // Adds various values to the cache. // This method ...


5

Create a List View Custom button and use this code to pass parameters to the flow. Flow parameters: vSelectedCaseIds which contains case ids collection which can be found from {!GETRECORDIDS($ObjectType.Case)}. vCaseCount which contains number of selected case id count. retURL where it will be landed after completion. {!RequireScript("/soap/...


5

You don't need any helper classes at all. You can just iterate through the results and update the parents accordingly. In this case the "correct" structure is the one you find easiest to write and test. But it doesn't have to be any more complicated than the below: List<Parent__c> toUpdate = new List<Parent__c>(); for (Parent__c parent : [ ...


Only top voted, non community-wiki answers of a minimum length are eligible