+ Start a Discussion
Preetham ThummalaPreetham Thummala 

Set

How do you refer to the elements in the SET?
Best Answer chosen by Preetham Thummala
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for more information on set.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_sets.htm

Sets
A set is an unordered collection of elements that do not contain any duplicates. Set elements can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types. For example, the following table represents a set of strings, that uses city names:
Set<String> s1 = new Set<String>{'a', 'b + c'}; // Defines a new set with two elements
Set<String> s2 = new Set<String>(s1); // Defines a new set that contains the 
                                     // elements of the set created in the previous step
Set<Integer> s = new Set<Integer>(); // Define a new set
s.add(1);                            // Add an element to the set
System.assert(s.contains(1));        // Assert that the set contains an element
s.remove(1);                         // Remove the element from the set
Please let us know if this will help you.

Thanks,
Amit Chaudhary

All Answers

NagaNaga (Salesforce Developers) 
Hi Preetham,

Try an identity map (i.e. have each element mapped to itself). This way you still have the convenience of a set (the map's keyset) while still being able to call specific items in the set.

Your methods are not really "lacking" as they are both quite functional.  Sets are cool because you are guaranteed uniqueness of elements but the trade-off is you don't have a nice order of its elements. Lists are cool because the elements have an ordering, and if you have some knowledge of the ordering (e.g. from how the list was produced (e.g. SOQL query) or by using the sort() method), you can choose and use certain elements (like the first or last element) creatively. However, you lose (or gain) the uniqueness of elements constraint (good/bad depending on what you're trying to do). 

You gotta deal with the language as is, and really, it's quite accommodating. Everything has a trade-off. But in the end, you can always do what you need to. If sets were able to everything you wanted to, I think you'd be left with a paradox hence it is not possible. And this complexity makes it fun while providing job security :)

The methods you have offered are both efficient examples of how to achieve your goal. It's quite similar to what I've used before.

Please see the link below

http://salesforce.stackexchange.com/questions/15010/get-one-item-from-a-set

Best Regards
Naga Kiran
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for more information on set.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_sets.htm

Sets
A set is an unordered collection of elements that do not contain any duplicates. Set elements can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types. For example, the following table represents a set of strings, that uses city names:
Set<String> s1 = new Set<String>{'a', 'b + c'}; // Defines a new set with two elements
Set<String> s2 = new Set<String>(s1); // Defines a new set that contains the 
                                     // elements of the set created in the previous step
Set<Integer> s = new Set<Integer>(); // Define a new set
s.add(1);                            // Add an element to the set
System.assert(s.contains(1));        // Assert that the set contains an element
s.remove(1);                         // Remove the element from the set
Please let us know if this will help you.

Thanks,
Amit Chaudhary
This was selected as the best answer