Collections:
Difference between Arrays and Collections.
1.Array:
- Array is a collection of homogenous (similar) elements.
- array can not grow and shrink dynamically.
- Array can be accessed faster and less memory
2.Collections:
- its is a collection of homogeneous and heterogeneous elements.
- It can grow and shrink dynamically .
- Collections are slow compared to arrays. consume more memory.
1.List
2.Set
3.Map
1. LIST:
- List is an interface
- A list is an ordered collection of elements that are distinguished by their indexes.
- List elements can be of any datatype- primitive types, Collections,Sobjects, user-defined types and built-in apex types.
- Insertion order is preserved.
- Can grow dynamically at run time.
- Duplicate values are allowed.
- Null values are accepted.
Methods in List CLass:
- add(object): add an element to the emd of the list.
- add(Integer, Object): Insert an element into the at specified index position.
- addAll(List): Adds all of there elements in the specified list to the list that calls the method.Both lists must be of the sametype.
- addAll(set); Add all of the elements in the specified set to the list that calls the method. the set and the list must be of the same type.
- Clear(): removes all elements from a list, consequently setting the list's length to zero.
- Clone():makes a duplicate copy of list.
- deepClone)(Boolean, Boolean,Boolean); Makes a duplicate copy of a list of Sobject records,including the sobject records themselves.
- equals(List); compares this list with the specified list and returns true if both lists are equal. otherwise returns false.
- get(Integers): Returns the list elements stored at the specified index.
- getSObjectType(): Returns the token of the Sobject type that takes a list of sobjects.
- hashcode():returns the hsdhcode corresponding to this list and its contents.
- isEmpty(): Returns true if the list has zero elements.
- iterator): Return an instace of an iterator for this list.
- remove(Integer): Removes the list element stored at the specified index, returning the element that was removed.
- set(Integer, Object): sets the specified value for the element at the index.
- size(): Returns the number of elements in the list.
- sort(): sorts the items in the list in ascending order.
EX:
List<string> str=new List<string>();
string s1='sam';
string
string s1='sam';
string s2='Ram';
string s3='Ravi';
str.adds(s1);
str.add(s2);
str.add(1,s3);
LIst<string> finalist= new List<string>();
finallist.addAll(str);
string x=str.get(1); // Ravi
Ex: Write a programme to demonstrate the list
public class ListExample{
public List<string> result{set;get;}
public ListExample()
{
result =new List<string>();
result.add('sam');
result.add('Ram');
result.add('hari');
result.add('1,'kumar'');
}
}
<apex:page controller="ListExample">
<apex;pageBlock>
<apex:pageBlockTable value=={result}" var="a">
<apex:column value="{!a}"/>
<apex:pageBlockTable>
<apex:pageBlockTable>
</apex:page>
List of Object Demo:-
Public class ListExample{
Public List<account> result{set;get;}
Public ListExample()
{
Account a1=new Account(name='sam' Industry='Banking');
Account a2=new Account(name='ram; Industry='Eneregy';
result= new List<Account>();
result.add(a1);
result.add(a2);
}
}
<apex:page controller="ListExample">
<apex:pageBlock>
<apex:pageBlockTable value="{!result}" var="a">
<apex:column value="{!a.name}/>
<apex:pageBlockTbale>
<apex:pageBlock>
<apex:page>
Create a List pf Apex class Objects:-
Student Apex:
global class Student
{
Public string name{get;set;}
Public Integer age(get;set;};
Public Student(String name, Integer age)
{
this.name=name;
this,age=age;
}
}
List Example Apex:
Public Class ListExample
{
Public List<Student>rsult{set;get;}
Public ListExample()
{
result=new List<Student>();
Student s1=new Student('sam,20);
Student s2=new Student('Ram',40);
Student s3=new Student('apraveen',40);
result.add(s1);
result.add(s2);
result.add(s3);
}
}
ListExample VisualForce:
<apex:page controller="ListExample">
<apex:pageBlock>
<apex:pageBlockTable value="{!result}" var="a">
<apex:column value="{!a.name}/.>
<apex::column vallue="{!a.name}/>
<apex:column value="{!a.age}"/>
<apex:pageBlockTable>
<apex:pageBlock>
<apex:page>
<apex;pageBlock>
<apex:pageBlockTable value=={result}" var="a">
<apex:column value="{!a}"/>
<apex:pageBlockTable>
<apex:pageBlockTable>
</apex:page>
List of Object Demo:-
Public class ListExample{
Public List<account> result{set;get;}
Public ListExample()
{
Account a1=new Account(name='sam' Industry='Banking');
Account a2=new Account(name='ram; Industry='Eneregy';
result= new List<Account>();
result.add(a1);
result.add(a2);
}
}
<apex:page controller="ListExample">
<apex:pageBlock>
<apex:pageBlockTable value="{!result}" var="a">
<apex:column value="{!a.name}/>
<apex:pageBlockTbale>
<apex:pageBlock>
<apex:page>
Create a List pf Apex class Objects:-
Student Apex:
global class Student
{
Public string name{get;set;}
Public Integer age(get;set;};
Public Student(String name, Integer age)
{
this.name=name;
this,age=age;
}
}
List Example Apex:
Public Class ListExample
{
Public List<Student>rsult{set;get;}
Public ListExample()
{
result=new List<Student>();
Student s1=new Student('sam,20);
Student s2=new Student('Ram',40);
Student s3=new Student('apraveen',40);
result.add(s1);
result.add(s2);
result.add(s3);
}
}
ListExample VisualForce:
<apex:page controller="ListExample">
<apex:pageBlock>
<apex:pageBlockTable value="{!result}" var="a">
<apex:column value="{!a.name}/.>
<apex::column vallue="{!a.name}/>
<apex:column value="{!a.age}"/>
<apex:pageBlockTable>
<apex:pageBlock>
<apex:page>
No comments:
Post a Comment