COLLECTIONS
Lists, Sets, and Maps

COLLECTIONS

List

List<String>Family=new List<String>{'Cemal','Songul','Esra','Seda'};//==>List 1

System.debug('My family members' + ' ' + Family);

Family.add('Selin');//==>Add Selin

System.debug('Last member of the family' +' '+ Family[4]); //Get Selin

Family.remove(2);//==>Remove Esra

Family.set(0,'Cemalim');//==>Set Cemal?

System.debug('My family members' + ' ' + Family);

System.debug('Size of Family is ' + '?' + Family.size()); //==>Size the List


List<String>Boys=new List<String>{'Cuneyt', 'Alican'}; // List 2?

?Family.addAll(Boys);//==>addAll Lists

System.debug('Last?boy in family'+ ' '+Family.get(Family.size()-1));//==>Get+Size the List

System.debug('Is Cuneyt in the Family' + ' '+Family.contains('Cuneyt'));//==> Contains?


if(Family.contains('Cuneyt')){

??System.debug('If?yes contains');// ==>if...else contains

}

??else{

????System.debug('if?no contains');

}


System.debug('Index of mother' + ' ' + Family.indexof('Songul'));//==>indexof

Boys.clear();//==>Clear List 2

System.debug(Boys.isEmpty());//==>isEmpty List 2, yes

Family.sort();

System.debug(Family);//==>Sort the List

for(String letter : Family){//>toLowercase done

??Family.set(Family.indexof(letter),letter.toLowercase());

}

System.debug(Family);

for(String letter : Family){

??Family.set(Family.indexof(letter),letter.toUppercase());

}

System.debug(Family);

//(If no list, we do for loop

//?For each loop must have a list!!

//it goes the same amount of elements)

List<String>mood=new List<String>{'Happy','Sad','Strong','Weak'};

for (String m:mood){

System.debug(m.toUpperCase());

}


List<String>FamilyClone=Family.clone();//==>Clone the List

System.debug(Family.equals(FamilyClone));//==>Equals

System.debug(Family);

for(Integer i=0; i<=5; i++){

??System.debug(Family.get(i));

}

/*for(Integer i=0; i<=Family.size()-1; i++){?

???System.debug(Family.get(i));

}*/


for(String each : Family){

??System.debug(each);

}


//Example 2


List<string> Fruits = new List<string>

{'Banana', 'Apple', 'Cherry'};

??for (String i : Fruits) {

???System.debug(i);

??}


/*SET


To remove the duplicates in the List, we use Set

No index==>no get, set, indexof*/


List<String>Mysisters=new List<String>{'esra','selin','esra','selin'};

System.debug(Mysisters);


Set<String>Mysistersnew=new Set<String>(Mysisters);//?convert list to set

System.debug(Mysistersnew);


Set<String>Friends=new Set<String>{'Medine','Esma','Merve'};

Friends.add('Fatma');

Friends.addAll(Mysistersnew);

Friends.remove('esra');

System.debug('My new friends are?'+ Friends);

System.debug(Friends.size());


If (Friends.contains('selin')){

??System.debug('yess');

}

else {

??System.debug('noo');

}

System.debug(Friends.contains('Merve'));


Friends.removeAll(Mysistersnew);

System.debug(Friends);

System.debug(Mysistersnew.isEmpty());

Mysistersnew.clear();

System.debug(Mysistersnew);


Set<String>FriendsClone=Friends.clone();

System.debug(Friends.equals(FriendsClone));

Boolean status=Friends.contains('Kucuk Merve');

System.debug(status);


for(String each:Friends){

??System.debug(each);

}


/*Friends.addAll(Mysistersnew);

for(Integer i=0; i<=Friends.size()-1; i++){

??System.debug(Friends.get(i));

}*/


/*MAP

Set + List? Map

'Keyset-values' means index

Left side is always unique and in order!*/


Map<Integer, String> Electronics=new Map<Integer, String>{4=>'Xbox', 7=>'?',3=>'Phone', 1=>'Xbox'};

??Electronics.put(4,'Radio');

??System.debug(Electronics.get(3));

System.debug(Electronics);

??Electronics.remove (3);

System.debug(Electronics.size());

System.debug(Electronics);

Electronics.put(1,'Laptop');

??Electronics.put(5,'Phone');

System.debug(Electronics);

System.debug(Electronics.containskey(4));


Map<String, Integer> Clothes=new Map<String, Integer>{'?'=>8,'Hat'=>5, 'Shoe'=>3, 'Sweather'=>4 };???

??System.debug(Clothes);

??Clothes.put('Tshirt', 2);

??System.debug(Clothes.get('?'));

System.debug(Clothes.values());

System.debug(Clothes.keyset());

System.debug(Clothes.containskey('Hat'));


Map<String, Integer> Planets=new Map<String, Integer>{'Earth'=>1,'Mars'=>5, 'Jupiter'=>3, 'Saturn'=>4 };

System.debug(Planets);

Clothes.putAll(Planets);

?System.debug(Clothes);

System.debug(Clothes.equals(Planets));

System.debug(Planets.isEmpty());

Planets.clear();

???System.debug(Planets);


Map<String,Integer>Sky=Clothes.clone();

???System.debug(Sky);

for(Integer lime: Sky.values()){

??System.debug(lime);

}

for(String kiwi: sky.keyset()){

??System.debug(kiwi);

}


Map<String, String> country_currencies = new Map<String, String>{'United States'=>'Dollar' , 'Japan'=>'Yen' , 'France'=>'Euro' , 'England'=>'Pound' , 'India'=>'Rupee'};

System.debug(country_currencies.values().get(3).toUpperCase());?//DEBUG; POUND

要查看或添加评论,请登录

社区洞察

其他会员也浏览了