Is it possible to access the index inside a set?
E.g. here's a set
Set <integer> numbers = new Set(){1,2,3,4,5,6,7,8};
Is there a way to access the index of the set like so ?
numbers[0]
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this communityYou can't index a Set like you can a List/Array. You can perform typical set operations - such as check if the Set contains an element by value, or remove an element by value. They are typically used for checking membership of data, not retrieving key/value pairs or values by index.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_set.htm
Just curious, what is your requirement where you would like to index a Set? I think a Map or List may be a more appropriate collection to use. If you were using a Set to eliminate duplicates, you could use a List to perform whatever index operations you desired, and then construct a set from the List:
List<Integer> ls = new List<Integer>();
ls.add(1);
ls.add(2);
Set<Integer> s1 = new Set<Integer>(ls);
System.debug(s1); // DEBUG|{1, 2}
As Brian says there is no index available so if you want one you have to convert to a list:
Set<Integer> numbers = new Set<Integer> {1,2,3,4,5,6,7,8};
List<Integer> numbersList = new List<Integer>(numbers);
for (Integer i = 0; i < numbersList.size(); i++) {
System.debug(numbersList[i]);
}
Usefully for-each loops do work with sets though:
Set<Integer> numbers = new Set<Integer>{1,2,3,4,5,6,7,8};
for (Integer n : numbers) {
System.debug(n);
}
Sets are unordered, unique lists. This means that they have no particular order, and do not contain duplicate values. What you'd usually use this for is to determine the presence or absence of a value already stored:
Set<Integer> values = new Set<Integer>();
values.add(5);
values.add(10);
System.assert(values.contains(10));
Since they have no order, it makes no sense to request a value by ordinal. You can convert a set into a list, however, simply by using the new list constructor:
List<Integer> valueList = new List<Integer>(values);
They will appear in no particular order within the list, although Summer '15 introduces a new feature that guarantees the lists will be in the same order each time, but the order is unspecified. In other words, even converting the set to a list doesn't guarantee that you'll find any particular value in the first index location, unless the set's size is exactly 1.
You may also iterate over a set as you would a for-each loop on a list:
for(Integer value: values) { ... }
As of Summer '15, it is guaranteed that two executions of the loop on the same set will output their values in the same order, although this order is not specified.
Based on your comments, I will assume that you are starting with a List<SObject>
, from which you are trying to map one field (Id
) to another (External_Id__c
). Let's assume it's a List<Account>
.
List<Account> incomingRecords = new List<Account(); // populated with whatever
Map<Id, String> idToExternal = new Map<Id, String>();
for (Account incomingRecord : incomingRecords)
{
idToExternal.put(incomingRecord.Id, incomingRecord.External_Id__c);
}
You are guaranteed to have unique map keys even if your incoming records have duplicates in the list. As for the values, you could validate that you don't add duplicates, but it sounds like that should be configured as unique at the database level.
map <id, string>
. The string would be the employee id and the id would be the sf id. Is there a better way?