^ Click Here
Showing posts with label map. Show all posts
Showing posts with label map. Show all posts

Thursday, March 29, 2012

get a random element from Set or Map

To get a random element from a list is quite easy......you just have to get the element t the nth position where n is a random number. But A Set is an unordered collection so there is no question of any definite element at definite position.

In this situation the way to get a random element from a Set might not have a straightforward way.......But the thing that can be done is iterate through the set and get the nth element where n is a random number. Conceptually it's the same as getting a random element from a List or array.


public String getRandomId(Set<String> idSet){
  int Size = idSet.size();
  int item = new Random().nextInt(Size);
  return (String) idSet.toArray()[item];
 }

 
Above method will return a random String from a Set of String........

Now suppose there is a Map with a String type as key and anything else say an Object as value...for example


Map<String,User> userMap = new HashMap<String,User>();

where User  is a Class and userMap holds the map between String id as key and user object as value.......to get a random user from this map we will use the above method in the example


String randomId = this.getRandomId(userMap.keySet());
User randomUser = userMap.get(randomId);

Hence we can get a random elements through this method. we just got a set of String of keys of the map and then used the previous getRandomId method to get a random key and got the corresponding value from the map.

Hope it helps.....

Saturday, January 28, 2012

Showing Opportunities for each Contact (parent - child) through Map without using Wrapper Class

We can show map from controller on the visualforce page, We also can put one map inside another map or in other words nested map.

With the help of these two factors, I successfully showed the contact and corresponding opportunities without using the wrapper classes. Though it put some restriction however for
the purpose of showing all the opportunities corresponding to each Contact (or any parent - child relationship) it works fine.

The process is simple -
 -I created a map of Contact as key and another map of Opportunity with id as value say A.
 -I used a query which brings all the opportunities for Contacts.
 -Now in a loop get the list of opportunities for each Contact.
 -After checking if there are Opportunities put those Opportunities in a map with id say B.
 -And lastly the Contact and the map B was put into map A.



Visualforce Page :-

 <apex:page controller="MapClass" action="{!getValues}">
<apex:pageblock >
<apex:pageBlockTable value="{!checkMap}" var="con">
    <apex:column value="{!con.Name}"/>
    <apex:column headerValue="Opportunities">
        <apex:dataList value="{!checkMap[con]}" var="opp">
        <apex:outputField value="{!checkMap[con][opp].Name}"/>
        </apex:dataList>
    </apex:column>
    <apex:column value="{!con.Account.Name}"/>
    <apex:column value="{!con.createddate}"/>
   
</apex:pageBlockTable>
</apex:pageblock>
</apex:page>



Apex Class :-

public class MapClass{
  public transient Map<Contact,Map<id,Opportunity>> checkMap{get; set;}
  public void getValues(){
  checkMap  = new Map<Contact,Map<id,Opportunity>>();
  for(Contact s : Database.query('select id, name, Account.name, owner.Name,    createddate, (select id, name from Opportunities) from Contact')){
      List<Opportunity> opLi = s.getSobjects('Opportunities');
      map<id,Opportunity> opMap = new map<id,Opportunity>();
      if(opLi != null){
      for(Opportunity op: opLi){
      opMap.put(op.id,op);   
      }
      }
      checkMap.put(s,opMap);
  }   
}
}

 Last but not the least you can stretch this concept to look what else can be done. feel free to add on it.