Iterate over the keys instead of the values:
for (Id id : mailContainer.keySet())
{
System.debug(id);
System.debug(mailContainer.get(id));
}
Answer from Adam Butler on Stack OverflowHow to display Map's list value based on 1 key in Apex Salesforce? - Stack Overflow
Using Maps in Apex (beginner)
How do I get key from a value in map
apex - How to check the map key value with field value? - Salesforce Stack Exchange
Videos
I’ve been learning Apex for a little bit now and have a pretty novice understanding of how it all works, but the concept I’m struggling with the most is using maps instead of lists or sets.
Does anyone have good reference material or examples on when Map is useful/what’s really happening in the background? I feel like I’m really close to fully grasping it, but there’s still something missing where I’m not fully understanding the value.
I keep watching tutorials online and they’re definitely inching me closer to a revelation but I’ve failed to find one that really digs in.
You are creating the maps like these:
Mapfield1.put(abRecs.id,abRecs.field1__c);
Mapfield2 .put(abRecs.id,abRecs.field2__c );
And later using this Mapfield2.get(books.field2Notes__c)value for comparison.
I think books.field2Notes__c which is using as key is not same key as abRecs.id. Thats why it is returning null.
Add the map like id as key and Sobject as value
Map<String,ABC__C> Mapfield1 = new Map<String,ABC__C>();
Map<String,ABC__C> Mapfield2 = new Map<String,ABC__C>();
for(ABC__C abRecs: [select Id,field1__c,field2__c from ABC__C])
{
Mapfield1.put(abRecs.field1__c,abRecs);
Mapfield2.put(abRecs.field2__c,abRecs );
}
Now you can check the value with eh help of map as like below,
books.field2Notes__c != Mapfield2.get(books.field2Notes__c).Field2__c
Its always prefer to check the null point and then use the map.get() values.
Keep somthing like below,
if(Mapfield2.get(books.field2Notes__c) != null && Mapfield1.get(books.field1Notes__c) != null &&
(books.field2Notes__c != Mapfield2.get(books.field2Notes__c).Field2__c ) && books.field1Notes__c != Mapfield1.get(books.field1Notes__c).field1__c)
{
//Do stuff
}
If you want to fetch them all simultaneously, you have to do it outside the loop:
Map<String,String> aMap = new Map<String,String>();
Set<String> mapKeys = aMap.keySet();
List<String> mapValues = aMap.values();
Source: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htm
If you absolutely need to all the keys inside a for loop (I don't know why... it sounds like bad practice to reference keys other than the one you're currently iterating), you can reference the mapKeys variable inside the for loop.
Or, just iterate and reference them one at a time as @MSCB described above
Map<String,String> myMap = new Map<String,String>();
//iterate over the map key
for (String key : myMap.keySet()) {
system.debug('=======map key====='+key);
//get the value based on key
system.debug('=======map value====='+myMap.get(key));
}
You are correct - when you add a record to a collection, that simply passes a reference to the existing record. This means there would never be more than one instance of the record, regardless of how many collections you add it to.
This isn't explicitly documented in the Apex developer's guide, but it explained in the following developerforce blog post:
http://blogs.developerforce.com/developer-relations/2012/05/passing-parameters-by-reference-and-by-value-in-apex.html
This works in the same way as Java, and there are a number of posts explaining pass by reference versus value in Java - here's one example:
https://stackoverflow.com/questions/40480/is-java-pass-by-reference/7034719#7034719
Yes that is correct. Primitive types are passed by value and non-primitive by reference.
This is a really good explanation of how it works exactly.
If you want to make changes and not affect the record in your collection, I'd suggest to clone the record using the .clone() method and then add it to a separate collection if you need to perform a DML on.