You need to sign in to do that
Don't have an account?

access first element in a set
How can I access the first element in a set? Example in a list, you can use this to get the first element in the list
Integer x = xList.get(0);
What would be a quick equivalent way of doing this in a set?
This would be (marginally) faster, since you break early.
You can also:
Just make sure setElements.isEmpty() is false.
All Answers
A set is an unordered collection of primitives or sObjects that do not contain any duplicate elements.
So you never know which is first element.
Can you loop through a SET? Like you can a LIST?
string firstElement = null;
for (string setElement : setElements) {
if (setElement == null) {
firstElement = setElement;
}
}
As following....
Set<String> setName = new set<String>();
setName.add('XYZ');
setName.add('ABC');
for(String name : setName){
System.debug(name);
}
This would be (marginally) faster, since you break early.
You can also:
Just make sure setElements.isEmpty() is false.
Thanks sfdcfox! That's exactly what I needed.