{"id":211,"date":"2014-12-30T10:42:03","date_gmt":"2014-12-30T05:42:03","guid":{"rendered":"http:\/\/learnsf.wordpress.com\/?p=211"},"modified":"2014-12-30T10:42:03","modified_gmt":"2014-12-30T05:42:03","slug":"another-trick-how-to-obtain-a-set-from-any-field-in-a-list-faster-and-without-loops","status":"publish","type":"post","link":"https:\/\/blog.felineflock.com\/index.php\/2014\/12\/30\/another-trick-how-to-obtain-a-set-from-any-field-in-a-list-faster-and-without-loops\/","title":{"rendered":"Another trick:  how to obtain a set from any field in a list &#8211; faster and without loops!"},"content":{"rendered":"<p>This is like the <a href=\"http:\/\/learnsf.wordpress.com\/2014\/12\/29\/trick-how-to-obtain-a-map-indexed-by-any-field-not-just-id-faster-and-without-loops\/\">previous post about getting maps out of a list<\/a>, but with the same trick applied for getting a set. <\/p>\n<p>In triggers, we usually code a loop like below to collect certain IDs in a set and later use the set in a query.<\/p>\n<blockquote style=\"width:90%;font-family:Monospace;\"><p>&#8230;<br \/>\nSet&lt;String&gt; acctIDSet = new&nbsp;Set&lt;String&gt;();<br \/>\nfor( Contact aContact : trigger.new ) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;acctIDSet.add( aContact.AccountID );<br \/>\n}<br \/>\n&#8230;\n<\/p><\/blockquote>\n<p>The new <strong>SetCreator class<\/strong> does that without any loops and in a single line of code.<\/p>\n<blockquote style=\"width:90%;font-family:Monospace;\"><p>Set&lt;String&gt; acctIDSet =<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SetCreator.createSetFromField( trigger.new, &#8216;AccountID&#8217; );<\/p><\/blockquote>\n<p>Below is the definition of the <strong>SetCreator class<\/strong> that allows that.<\/p>\n<blockquote style=\"width:90%;font-family:Monospace;\"><p><code>public class SetCreator {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;public static Set&lt;String&gt; createSetFromField(<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List&lt;SObject&gt; aList, String fieldName ) {<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ get the list in JSON format<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;String jsonList = JSON.serialize( aList );<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ copy only the fieldName value using RegEx substitution<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;jsonList = jsonList.replaceAll(<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'(\\\\{.*?\"' + fieldName + '\":\"(.*?)\"(,\".*?\")*\\\\},?)'<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;, ',\"$2\"' ).replace( '[,', '[' );<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ create set from the modified JSON<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;Set&lt;String&gt; extractedSet =<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Set&lt;String&gt;) JSON.deserialize(<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jsonList, Set&lt;String&gt;.class );<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;return extractedSet;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n<p>}<\/code><\/p><\/blockquote>\n<p>It converts the list to a JSON string and modifies the string to look as if it was a serialized Set. It uses RegEx to keep the specified field values and remove everything else. It then deserializes the modified JSON string into a Set.<br \/>\nAgain, there are no explicit loops &#8211; all of them are internal to the RegEx\/JSON implementations, and are much faster. <\/p>\n<p>Even if there are duplicates, they are automatically eliminated during the deserialization of the JSON string.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is like the previous post about getting maps out of a list, but with the same trick applied for getting a set. In triggers, we usually code a loop like below to collect certain IDs in a set and later use the set in a query. &#8230; Set&lt;String&gt; acctIDSet = new&nbsp;Set&lt;String&gt;(); for( Contact aContact [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[13],"tags":[],"class_list":["post-211","post","type-post","status-publish","format-standard","hentry","category-no-category"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/posts\/211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/comments?post=211"}],"version-history":[{"count":0,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/posts\/211\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/media?parent=211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/categories?post=211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/tags?post=211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}