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
R557R557 

How to create a Map of List elements with each other

Hi,

I have an AggregateResult List which has Dealer Names e.g. : 

List<AggregateResult> myList =  [Select Dealer_Name From SampleObject GROUP BY Dealer_Name ];

Result  :  Dealer1, Dealer2 ,Dealer3


I want to create a map of Each dealer with the remaining dealers.
E.g. : 

Dealer1 => Dealer2 ,Dealer3
Dealer2 => Dealer1,Dealer3
.....and so on


How to do that?
Nayana KNayana K
Set<String> dealerNames = new Set<String>();
Map<String, List<String>> mapDealerToOthers = new Map<String, List<String>>();
for(AggregateResult objAR : [Select Dealer_Name dn, COUNT(Id) cnt From SampleObject GROUP BY Dealer_Name]) {
	dealerNames.ada((String)objAR.get('dn'));
}

for(String key: dealerNames) {
	mapDealerToOthers.put(key, new List<String>());
	for(String other: dealerNames) {
		if(other != key) {
			mapDealerToOthers.get(key).add(other);
		}
	}
}

system.debug('==mapDealerToOthers=='+mapDealerToOthers);

Please check if this works