function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MunmunMunmun 

How to use this

How to  iterate through map

 

Map<Date, List<String>>  m1=new Map <Date, List<String>>();

 

I had the list ,now how to papulate list inside the map..

 

How to do this....bit urgent...

 

Thanks

 

dmchengdmcheng

Use nested for-loops to construct your Lists and the put method to construct the map inside the loop.

sfdcfoxsfdcfox

 

for(Object__c obj:listObj) {
  if(!m1.containsKey(obj.datevalue__c))
    m1.put(obj.datevalue__c,new list<string>());
  m1.get(obj.datevalue__c).add(obj.textvalue__c);
}

This is an arbitrary bit of code, since I don't know what you're exactly doing, but this is the code to construct your map<date, list<string>>.

 

MunmunMunmun

Can u explane more

sfdcfoxsfdcfox

The get method of Map returns a data type value equal to the template parameter that you pass into it.

 

Consider this example:

 

 

List<String> list_a;
List<String> list_b;
List<String> list_c;

What are the values contained in list_a, list_b, and list_c at this point in the code?

 

 

If you said that there were three "empty lists", you would be incorrect. In truth, you have not yet created (instantiated) the list (called an object), and so there is no memory associated with the list, and it is unable to call any of its instance methods. As another short example, consider the class:

 

 

public class HelloWorld {
  public HelloWorld() { this.world = 'world'; }
  private string world;
  public static string getHello() { return 'Hello '; }
  public string getWorld() { return world; }
  public string getHelloWorld() { return getHello() + this.world; }
}

To use this object, i first have to allocate memory to it through the process of "instantiation". However, the "static" members are freely available to me, even without instantiation. That means I can use the class like this:

 

 

 

System.debug(HelloWorld.getHello()); // Okay, this function is static.
HelloWorld theWorld = new HelloWorld(); // Create some memory for this object.
System.debug(theWorld.getHelloWorld()); // And call an instance method.

If I remove the new HelloWorld() part of the above code, what is the result? Of course, there is no object, so the line after fails with an exception, because it can't operate without an instance.

 

 

Now, you might ask, what was the point of the preceding text? The answer is, of course, that Maps are almost exactly like infinitely sized lists, but using letters instead of numbers They never complain if you refer to an object that does not exist, they simply return a null value.

 

In my example above, it is therefore necessary that the list is instantiated before we attempt to use it, or you will observe the same problem as I've outlined. The previous poster mentioned also that you could use two loops to create your Map. The primary inconvenience with that is when you can not reliably determine which dates will be used in advance.

 

For example, if we do the following:

 

 

Map<Date,List<String>> taskSubjects = new Map<Date,List<String>>();
for(Task t:[select id,duedate,subject from task])
  taskSubjects.put(t.duedate,new list<string>());
for(Task t:[select id,duedate,subject from task])
  taskSubjects.get(t.duedate).add(t.subject);

This certainly works (although it uses a query redundantly). We can optimize this one of two ways:

 

 

1) We cache the query, as follows:

 

 

Map<Date,List<String>> taskSubjects = new Map<Date,List<String>>();
List<Task> tasks = [select id,subject,duedate from task];
for(task t:tasks)
  taskSubjects.put(t.duedate,new list<string>());
for(task t:tasks)
  taskSubjects.get(t.duedate).add(t.subject);

This is better, as it "saves a SOQL query", and it uses only one more script statement. On the downside, it also uses more "heap memory", because that list of tasks no longer gets to fluidly pass through an optimized query loop, but has to occupy its full girth of memory for the rest of its scope. We could add some tricks to conserve that memory and free it as early as possible, but it'd be better if that memory were never "used" to begin with.

 

Thus, you would want to take advantage of a for loop, but remember that an unused map key returns a null value, which we can't use without getting an exception.

 

This leads us to the next layer of evolution with optimizing our code:

 

 

map<date,list<string>> taskSubjects = new map<date,list<string>>();
for(task t:[select id,duedate,subject from task]) {
  if(taskSubjects.get(t.duedate)==null)
    taskSubjects.put(t.duedate,new list<string>());
  taskSubjects.get(t.duedate).add(t.subject);
}

Our new code uses one additional script line for the if statement for each unique due date that is queried by this call. However, we save our extra SOQL call, as well as our heap memory. The more duplicated the date fields are, the better script-line-counter performance we would have.

 

 

Finally, we could over-optimize and come up with the following code:

 

 

map<date,list<string>> taskSubjects = new map<date,list<string>>();
for(task t:[select id,duedate,subject from task]) {
  taskSubjects.put(t.duedate,taskSubjects.get(t.duedate)==null?new list<string>():taskSubjects.get(t.duedate));
  taskSubjects.get(t.duedate).add(t.subject);
}

 

I would expect that no sane developer would use a ternary operator if they could help it, but if optimization were truly a concern, there's always ways to squeeze a bit more out of the system. This happens to use exactly the same number of script statements as the first script, with the memory usage of the prior script, all wrapped up in a tidy package. Just remember to comment code that looks like this, or you (or some other unfortunate developer) might have a wonderful time trying to debug code like this.

 

 

Just remember, you don't want your code to end up looking like this:

 

 

                                  #include\
                                  <stdio.h>
                     #include                <stdlib.h>
                     #include                <string.h>

                    #define w "Hk~HdA=Jk|Jk~LSyL[{M[wMcxNksNss:"
                   #define r"Ht@H|@=HdJHtJHdYHtY:HtFHtF=JDBIl"\
                  "DJTEJDFIlMIlM:HdMHdM=I|KIlMJTOJDOIlWITY:8Y"
                 #define S"IT@I\\@=HdHHtGH|KILJJDIJDH:H|KID"\
                "K=HdQHtPH|TIDRJDRJDQ:JC?JK?=JDRJLRI|UItU:8T"
               #define _(i,j)L[i=2*T[j,O[i=O[j-R[j,T[i=2*\
              R[j-5*T[j+4*O[j-L[j,R[i=3*T[j-R[j-3*O[j+L[j,
             #define t"IS?I\\@=HdGHtGIDJILIJDIItHJTFJDF:8J"

    #define y                  yy(4),yy(5),                yy(6),yy(7)
  #define yy(              i)R[i]=T[i],T[i ]            =O[i],O[i]=L [i]
#define Y _(0          ], 4] )_ (1 ], 5] )_ (2      ], 6] )_ (3 ], 7] )_=1
#define v(i)(      (( R[ i ] * _ + T [ i ]) * _ + O [ i ]) * _ + L [ i ]) *2
double b = 32  ,l ,k ,o ,B ,_ ; int Q , s , V , R [8 ], T[ 8] ,O [8 ], L[ 8] ;
#define q( Q,R ) R= *X ++ % 64 *8 ,R |= *X /8 &7 ,Q=*X++%8,Q=Q*64+*X++%64-256,
# define  p      "G\\QG\\P=GLPGTPGdMGdNGtOGlOG"   "dSGdRGDPGLPG\\LG\\LHtGHtH:"
#  define W         "Hs?H{?=HdGH|FI\\II\\GJlHJ"    "lFL\\DLTCMlAM\\@Ns}Nk|:8G"
# define   U           "EDGEDH=EtCElDH{~H|AJk}"       "Jk?LSzL[|M[wMcxNksNst:"
#  define u                  "Hs?H|@=HdFHtEI"             "\\HI\\FJLHJTD:8H"
char  *   x                   ,*X , ( * i )[               640],z[3]="4_",
*Z = "4,8O4.8O4G" r U "4M"u S"4R"u t"4S8CHdDH|E=HtAIDAIt@IlAJTCJDCIlKI\\K:8K"U
 "4TDdWDdW=D\\UD\\VF\\FFdHGtCGtEIDBIDDIlBIdDJT@JLC:8D"t"4UGDNG\\L=GDJGLKHL\
FHLGHtEHtE:"p"4ZFDTFLT=G|EGlHITBH|DIlDIdE:HtMH|M=JDBJLDKLAKDALDFKtFKdMK\
\\LJTOJ\\NJTMJTM:8M4aGtFGlG=G|HG|H:G\\IG\\J=G|IG|I:GdKGlL=G|JG|J:4b"W
S"4d"W t t"4g"r w"4iGlIGlK=G|JG|J:4kHl@Ht@=HdDHtCHdPH|P:HdDHdD=It\
BIlDJTEJDFIdNI\\N:8N"w"4lID@IL@=HlIH|FHlPH|NHt^H|^:H|MH|N=J\\D\
J\\GK\\OKTOKDXJtXItZI|YIlWI|V:8^4mHLGH\\G=HLVH\\V:4n" u t t
"4p"W"IT@I\\@=HdHHtGIDKILIJLGJLG:JK?JK?=JDGJLGI|MJDL:8M4\
rHt@H|@=HtDH|BJdLJTH:ITEI\\E=ILPILNNtCNlB:8N4t"W t"4u"
p"4zI[?Il@=HlHH|HIDLILIJDII|HKDAJ|A:JtCJtC=JdLJtJL\
THLdFNk|Nc|\
:8K"; main (
int C,char**        A) {for(x=A[1],i=calloc(strlen(x)+2,163840);
C-1;C<3?Q=_=       0,(z[1]=*x++)?((*x++==104?z[1]^=32:--x), X =
strstr(Z,z))      &&(X+=C++):(printf("P2 %d 320 4 ",V=b/2+32),
V*=2,s=Q=0,C     =4):C<4?Q-->0?i[(int)((l+=o)+b)][(int)(k+=B)
]=1:_?_-=.5/    256,o=(v(2)-(l=v(0)))/(Q=16),B=(v(3)-(k=v(1)
))/Q:*X>60?y   ,q(L[4],L[5])q(L[6],L[7])*X-61||(++X,y,y,y),
Y:*X>57?++X,  y,Y:*X >54?++X,b+=*X++%64*4:--C:printf("%d "
,i[Q][s]+i[Q ][s+1]+i[Q+1][s]+i[Q+1][s+1])&&(Q+=2)<V||(Q=
0,s+=2)<640
||(C=1));}

 

(This code from http://www0.us.ioccc.org/years.html, a website for developers to write purposefully difficult-to-read code, known as obfuscation).

MunmunMunmun

Thanks,

Class  A{

List <Account> accs=new List<Accoyunts>();

Iterator<Accounts> itr=acc.iterator();
 while(itr.hasNext()){
 Accounts  acc1=litr.next();
AccountName=acc1.Name;


}

 

Class B{
List<String> AccountName;

 

}


Now my querry is how to get the List  value in 2nd class.only the List value(i.e AccountName )