My code is taking time to execute. I want to optimize my code

 import java.util.ArrayList;
  import java.util.*;

  import sailpoint.object.Identity; 
  import sailpoint.object.QueryOptions;
  import sailpoint.object.Filter;
  import sailpoint.object.Application;
  import sailpoint.object.Link;
  import sailpoint.object.ManagedAttribute;
  import sailpoint.object.Bundle;
  import sailpoint.object.AuditEvent;
  import sailpoint.object.IdentityRequest;
  import sailpoint.tools.GeneralException;
   import sailpoint.object.AuditEvent;
  import java.util.List;

 
  int countCWK=0;
  int countEMP=0;
  int countVIR=0;
  int countPAR=0;

  
  
  System.out.println("1");
  QueryOptions qo = new QueryOptions();
  qo.addFilter(Filter.notnull("name"));

  List IdentityList = context.getObjects(Identity.class, qo);

 
    
  for (Identity identity : IdentityList) {
    //   System.out.println("Inside for:::::"+identity.getName());
    if(identity.isInactive()==false){

  
String employeeType = (String) identity.getAttribute("employeeType");
    if ("CWK".equals(employeeType)) {
      countCWK++;
    }
    
    
    
    // Check if the employeeType attribute is "EMP", with null check
    String employeeType = (String) identity.getAttribute("employeeType");
    if ("EMP".equals(employeeType)) {
      countEMP++;
    }

     // Check if the employeeType attribute is "VIR", with null check
    String employeeType = (String) identity.getAttribute("employeeType");
    if ("VIR".equals(employeeType)) {
      countVIR++;
    }
    
    
    
    // Check if the employeeType attribute is "PAR", with null check
    String employeeType = (String) identity.getAttribute("employeeType");
    if ("PAR".equals(employeeType)) {
      countPAR++;
    }
      
    }
  }
    
    System.out.println("count EMP Identity::"+countEMP);  
   System.out.println("count VIR Identity::"+countVIR);  
   System.out.println("count PAR Identity::"+countPAR);  
   System.out.println("count CWK Identity::"+countCWK);  
   
  
  
  

See if the code can be optimized using switch as below. the below code should be quick.

import java.util.List;
import sailpoint.object.Identity;
import sailpoint.object.QueryOptions;
import sailpoint.object.Filter;
import sailpoint.tools.GeneralException;

public class IdentityCounter {

    public static void main(String[] args) throws GeneralException {
        int countCWK = 0;
        int countEMP = 0;
        int countVIR = 0;
        int countPAR = 0;

        System.out.println("1");
        QueryOptions qo = new QueryOptions();
        qo.addFilter(Filter.notnull("name"));

        // Assume context is already defined and initialized
        List<Identity> identityList = context.getObjects(Identity.class, qo);

        for (Identity identity : identityList) {
            if (!identity.isInactive()) {
                String employeeType = (String) identity.getAttribute("employeeType");

                if (employeeType != null) {
                    switch (employeeType) {
                        case "CWK":
                            countCWK++;
                            break;
                        case "EMP":
                            countEMP++;
                            break;
                        case "VIR":
                            countVIR++;
                            break;
                        case "PAR":
                            countPAR++;
                            break;
                    }
                }
            }
        }

        System.out.println("count EMP Identity::" + countEMP);
        System.out.println("count VIR Identity::" + countVIR);
        System.out.println("count PAR Identity::" + countPAR);
        System.out.println("count CWK Identity::" + countCWK);
    }
}

thanks for sharing but it also taken more time.

If employeeType is searchable attribute you can just build correctly query optoins

int countCWK = 0;
QueryOptions qo = new QueryOptions();
Filter f1 = Filter.eq("inactive",false");
Filter f2 = Filter.eq("employeeType","CWK");

qo.add(Filter.and(f1,f2);
countCWK = context.countObjects(Identity.class,qo);

This is the example for employeeType == CWK, you can just replicate it for other types.

@amanKsingh
Few points from my side.

  1. When you are processing large set of objects, don’t use getObjects() method; instead you can use search() method.
    Note: getObjects() gets all the objects matching the criteria and stores in the memory. If the search result is more, it would definitely take a lot of your memory and processing will slow.

  2. Get only the attribute values from the objects which is necessary for your processing.

QueryOptions qo = new QueryOptions();
List returnAtts = Arrays.asList("name", "employeetype"); //Assuming employeeType is a searchable attribute
Filter f1 = Filter.eq("inactive", "true");
Filter f2 = Filter.notnull("name")
  qo.addFilter(Filter.and(f1,f2));

Iterator idIterator = context.search(Identity.class, qo, returnAtts);
//Iterate and process the data
  1. And finally decache the object to clear up the memory.

Hope this helps.
Let me know for more details.

thanks for sharing but not working

Hi @amanKsingh,

Is it not giving expected output, or you are getting some error?

Thanks

not giving expected output.

Hi @amanKsingh,

Try below code. It is just modified as per your case. If possible, just check for single employee type. If it works, then try with other.

int countCWK=0;
		int countEMP=0;
		int countVIR=0;
		int countPAR=0;



		System.out.println("1");
		QueryOptions qo = new QueryOptions();
		Filter nameFilter=Filter.notnull("name");

		Filter inactiveFilter=nameFilter.and(Filter.eq("inactive",false));

		Filter employeeTypeFilter=inactiveFilter.and(Filter.eq("employeeType","CWK"));
		qo.add(employeeTypeFilter);
		countCWK= context.countObjects(Identity.class, qo);

		employeeTypeFilter=inactiveFilter.and(Filter.eq("employeeType","EMP"));
		qo.add(employeeTypeFilter);
		countEMP= context.countObjects(Identity.class, qo);

		employeeTypeFilter=inactiveFilter.and(Filter.eq("employeeType","VIR"));
		qo.add(employeeTypeFilter);
		countVIR= context.countObjects(Identity.class, qo);

		employeeTypeFilter=inactiveFilter.and(Filter.eq("employeeType","PAR"));
		qo.add(employeeTypeFilter);
		countPAR= context.countObjects(Identity.class, qo);


		System.out.println("count EMP Identity::"+countEMP);  
		System.out.println("count VIR Identity::"+countVIR);  
		System.out.println("count PAR Identity::"+countPAR);  
		System.out.println("count CWK Identity::"+countCWK);

Thanks