Kirti Mishra
Kirti Mishra

Reputation: 23

Sets in Salesforce

Accessing elements of a set.

If I have to access an element of a list , I can use the get() method . EX:- myList.get(1) would return the element at the 1st index.But the same seems invalid for sets. I am aware that set is an unordered collection. But does that mean that we cant access individual elements of a set?

Upvotes: 1

Views: 1509

Answers (1)

Noor A Shuvo
Noor A Shuvo

Reputation: 2807

LIST class provides you the get method, but there is no get method in SET. As SET is an unordered collection, it is not certain to get the correct element in correct index.

The solution to this problem is, Cast the SET to LIST.

SET<String> str = new Set<String>();
str.add('4');
str.add('1');
str.add('2');
str.add('3');


string f = (new list<string>(str) )[0];
string s = (new list<string>(str) )[1];

You can get the element by index by this way.

Upvotes: 1

Related Questions