Apex Code
Set<string> Colors = new Set<string>
{'Red', 'Blue', 'Green'};
System.debug('Colors: '+Colors);
Execution Log
Apex Code
Set<string> Colors = new Set<string>
{'Red', 'Blue', 'Green'};
System.debug('The size of the set is : '+Colors.size());
The keyword add()
can be used to add more element in the set:
Execution Log
Set<string> Colors = new Set<string>
{'Red', 'Blue', 'Green'};
Colors.add('Orange');
System.debug('The size of the set is : '+Colors.size());
Apex Code
Set<string> Colors = new Set<string>
{'Red', 'Blue', 'Green','Orange'};
Colors.clear();
System.debug('Colors: '+Colors);
Execution Log
Apex Code
Set<string> Colors = new Set<string>
{'Red', 'Blue', 'Green','Orange'};
Colors.remove('Green');
System.debug('Colors: '+Colors);
Execution Log
Apex Code
Set<string> Colors = new Set<string>
{'Red', 'Blue', 'Green','Orange'};
for (String i : Colors) {
System.debug(i);
}
Execution Log
Apex Code
Set<string> ColorsLowerCase = new Set<string>
{'Red', 'Blue', 'Black','Pink'};
Set<string> ColorsUpperCase = new Set<String>();
for (String i : ColorsLowerCase) {
ColorsUpperCase.add(i.toUpperCase());
}
System.debug(ColorsUpperCase);
Execution Log