10

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]

  • What is your goal? Why do you desire that feature? – Adrian Larson Jun 11 '15 at 19:15
  • I need to use a set so that they're unique keys; but i need to loop through it to get an individual item by index and create a map from the index – chrisjlee Jun 11 '15 at 20:07
  • Yeah it is still not clear why building a map would require an index. – Adrian Larson Jun 11 '15 at 20:59
  • Good point. I'm trying to build two lists with matching indexes. One list would contain the unique id, the other would contain the salesforce id; from there, i'm merge the lists with matching sf ids to uid; map <id, string>. The string would be the employee id and the id would be the sf id. Is there a better way? – chrisjlee Jun 12 '15 at 3:37
13

You 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}
  • 1
    That's correct. I'm using a set to reduce duplicates. – chrisjlee Jun 11 '15 at 20:12
7

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);
}
5

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.

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.