with sharing And without sharing


<!--  VF Page  -->
<apex:page controller="MainClass">
  lstAccountWithoutSharing:<br/>
  {!lstAccountWithoutSharing}<br/><br/>

  lstAccountWithSharing:<br/>
  {!lstAccountWithSharing}<br/><br/>
</apex:page>

//With sharing and without sharing when calling to other class

public with sharing class MainClass
{
  public List<Account> lstAccountWithoutSharing{get;set;}
  public List<Account> lstAccountWithSharing{get;set;}
  public MainClass()
   {
      lstAccountWithoutSharing=new List<Account>();
      lstAccountWithSharing=new List<Account>();

      lstAccountWithoutSharing=AccountsWithoutSharing.getAccountsWithoutSharing();
      lstAccountWithSharing=AccountsWithSharing.getAccountsWithSharing();
   }
}
--------------------------------------------------
public without sharing class AccountsWithoutSharing
{
  public  static  List<Account>  getAccountsWithoutSharing()
    {
          return [Select id,name from Account];
    }
}
--------------------------------------------------
public with sharing class AccountsWithSharing
{
  public  static  List<Account>  getAccountsWithSharing()
    {
          return [Select id,name from Account];
    }
}

-----------------------OUTPUT-----------------------------

lstAccountWithoutSharing:
[0010I00001ZFJgRQAX, 0010I00001ZFJhPQAX, 0010I00001ZFJh0QAH, 0012800001Yi6zXAAR, 0012800001Yi6xbAAB, 0012800001Yi74IAAR, 0010I00001ZFJiYQAX, 0012800001Yi6HhAAJ, 0010I00001ZFJh5QAH, 0012800001YhpexAAB, 0010I00001ZFJhFQAX, 0010I00001ZFJhKQAX, 0010I00001ZFJglQAH, 0010I00001ZFJgvQAH, 0012800001Yi71sAAB, 0012800000uVGpTAAW, 0012800000uVE7qAAG, 0012800000uVD7iAAG, 0012800000uVGkMAAW, 0012800001Yi73yAAB, 0012800001Yi6LAAAZ, 0012800001Yi6yPAAR, 0012800001Yi6zTAAR, 0012800001Yi71nAAB, 0012800001Yi6zSAAR, 0012800001Yi75cAAB, 0012800001YiKBTAA3, 0012800001Yi6JsAAJ, 0012800001Yi6JtAAJ, 0012800001Yi6JuAAJ, 0012800001Yi6JvAAJ, 0012800001Yi75XAAR, 0012800001Yi76QAAR, 0012800001Yi78HAAR, 0010I00001dvdJIQAY, 0010I00001dvdJcQAI, 0010I00001dvdMnQAI, 0010I00001dvdMdQAI, 0010I00001dvdJDQAY, 0010I00001dvdJXQAY, 0010I00001dvdJ3QAI, 0010I00001cyy8wQAA, 0012800001Yi6JxAAJ, 0012800001Yi76BAAR, 0012800001Yi75SAAR, 0012800001YiKOwAAN, 0012800001YhpNhAAJ, 0012800001YiKZVAA3, 0012800000tnBmuAAE, 0012800000tnBorAAE, 0012800000tnB0sAAE, 0012800000tnB0qAAE, 0012800000tnB0rAAE, 0012800000tnB0iAAE, 0012800000tnB0jAAE, 0012800000tnB0kAAE, 0012800000tnB0lAAE, 0012800000tnB0mAAE, 0012800000tnB0oAAE, 0012800000tnB0pAAE, 0012800000tnB0nAAE, 0012800000tnB0tAAE]

lstAccountWithSharing:
[]


----------------------------------------------
Conclusion:
Result is not dependent on what the sharing contaxt of Main Class it depends on what the sharing contaxt of the class in which SOQL is executing

Lets do some more exercise:
------------------------------------------------------------------------
create an other class:
public virtual without sharing class WithoutSharingContaxt
{


}
-------------
and
-------------
public with sharing class AccountsWithSharing extends WithoutSharingContaxt 
{
  public  static  List<Account>  getAccountsWithSharing()
    {
          return [Select id,name from Account];
    }
}
------------------------------
then lets check the output
-----------------------OUTPUT-----------------------------
lstAccountWithoutSharing:
[0010I00001ZFJgRQAX, 0010I00001ZFJhPQAX, 0010I00001ZFJh0QAH, 0012800001Yi6zXAAR, 0012800001Yi6xbAAB, 0012800001Yi74IAAR, 0010I00001ZFJiYQAX, 0012800001Yi6HhAAJ, 0010I00001ZFJh5QAH, 0012800001YhpexAAB, 0010I00001ZFJhFQAX, 0010I00001ZFJhKQAX, 0010I00001ZFJglQAH, 0010I00001ZFJgvQAH, 0012800001Yi71sAAB, 0012800000uVGpTAAW, 0012800000uVE7qAAG, 0012800000uVD7iAAG, 0012800000uVGkMAAW, 0012800001Yi73yAAB, 0012800001Yi6LAAAZ, 0012800001Yi6yPAAR, 0012800001Yi6zTAAR, 0012800001Yi71nAAB, 0012800001Yi6zSAAR, 0012800001Yi75cAAB, 0012800001YiKBTAA3, 0012800001Yi6JsAAJ, 0012800001Yi6JtAAJ, 0012800001Yi6JuAAJ, 0012800001Yi6JvAAJ, 0012800001Yi75XAAR, 0012800001Yi76QAAR, 0012800001Yi78HAAR, 0010I00001dvdJIQAY, 0010I00001dvdJcQAI, 0010I00001dvdMnQAI, 0010I00001dvdMdQAI, 0010I00001dvdJDQAY, 0010I00001dvdJXQAY, 0010I00001dvdJ3QAI, 0010I00001cyy8wQAA, 0012800001Yi6JxAAJ, 0012800001Yi76BAAR, 0012800001Yi75SAAR, 0012800001YiKOwAAN, 0012800001YhpNhAAJ, 0012800001YiKZVAA3, 0012800000tnBmuAAE, 0012800000tnBorAAE, 0012800000tnB0sAAE, 0012800000tnB0qAAE, 0012800000tnB0rAAE, 0012800000tnB0iAAE, 0012800000tnB0jAAE, 0012800000tnB0kAAE, 0012800000tnB0lAAE, 0012800000tnB0mAAE, 0012800000tnB0oAAE, 0012800000tnB0pAAE, 0012800000tnB0nAAE, 0012800000tnB0tAAE]

lstAccountWithSharing:
[]
------------------------------------------------------
Conclusion:
HA HA what the difference here!
If current class contaxt is forced  to with sharing we can not extend its contaxt to without sharing

Lets remove  that forced with sharing  keyword
--------------------------------------
public  class AccountsWithSharing extends WithoutSharingContaxt 
{
  public  static  List<Account>  getAccountsWithSharing()
    {
          return [Select id,name from Account];
    }
}
-----------------
and see the output
-----------------------OUTPUT-----------------------------
lstAccountWithoutSharing:
[0010I00001ZFJgRQAX, 0010I00001ZFJhPQAX, 0010I00001ZFJh0QAH, 0012800001Yi6zXAAR, 0012800001Yi6xbAAB, 0012800001Yi74IAAR, 0010I00001ZFJiYQAX, 0012800001Yi6HhAAJ, 0010I00001ZFJh5QAH, 0012800001YhpexAAB, 0010I00001ZFJhFQAX, 0010I00001ZFJhKQAX, 0010I00001ZFJglQAH, 0010I00001ZFJgvQAH, 0012800001Yi71sAAB, 0012800000uVGpTAAW, 0012800000uVE7qAAG, 0012800000uVD7iAAG, 0012800000uVGkMAAW, 0012800001Yi73yAAB, 0012800001Yi6LAAAZ, 0012800001Yi6yPAAR, 0012800001Yi6zTAAR, 0012800001Yi71nAAB, 0012800001Yi6zSAAR, 0012800001Yi75cAAB, 0012800001YiKBTAA3, 0012800001Yi6JsAAJ, 0012800001Yi6JtAAJ, 0012800001Yi6JuAAJ, 0012800001Yi6JvAAJ, 0012800001Yi75XAAR, 0012800001Yi76QAAR, 0012800001Yi78HAAR, 0010I00001dvdJIQAY, 0010I00001dvdJcQAI, 0010I00001dvdMnQAI, 0010I00001dvdMdQAI, 0010I00001dvdJDQAY, 0010I00001dvdJXQAY, 0010I00001dvdJ3QAI, 0010I00001cyy8wQAA, 0012800001Yi6JxAAJ, 0012800001Yi76BAAR, 0012800001Yi75SAAR, 0012800001YiKOwAAN, 0012800001YhpNhAAJ, 0012800001YiKZVAA3, 0012800000tnBmuAAE, 0012800000tnBorAAE, 0012800000tnB0sAAE, 0012800000tnB0qAAE, 0012800000tnB0rAAE, 0012800000tnB0iAAE, 0012800000tnB0jAAE, 0012800000tnB0kAAE, 0012800000tnB0lAAE, 0012800000tnB0mAAE, 0012800000tnB0oAAE, 0012800000tnB0pAAE, 0012800000tnB0nAAE, 0012800000tnB0tAAE]

lstAccountWithSharing:
[0010I00001ZFJgRQAX, 0010I00001ZFJhPQAX, 0010I00001ZFJh0QAH, 0012800001Yi6zXAAR, 0012800001Yi6xbAAB, 0012800001Yi74IAAR, 0010I00001ZFJiYQAX, 0012800001Yi6HhAAJ, 0010I00001ZFJh5QAH, 0012800001YhpexAAB, 0010I00001ZFJhFQAX, 0010I00001ZFJhKQAX, 0010I00001ZFJglQAH, 0010I00001ZFJgvQAH, 0012800001Yi71sAAB, 0012800000uVGpTAAW, 0012800000uVE7qAAG, 0012800000uVD7iAAG, 0012800000uVGkMAAW, 0012800001Yi73yAAB, 0012800001Yi6LAAAZ, 0012800001Yi6yPAAR, 0012800001Yi6zTAAR, 0012800001Yi71nAAB, 0012800001Yi6zSAAR, 0012800001Yi75cAAB, 0012800001YiKBTAA3, 0012800001Yi6JsAAJ, 0012800001Yi6JtAAJ, 0012800001Yi6JuAAJ, 0012800001Yi6JvAAJ, 0012800001Yi75XAAR, 0012800001Yi76QAAR, 0012800001Yi78HAAR, 0010I00001dvdJIQAY, 0010I00001dvdJcQAI, 0010I00001dvdMnQAI, 0010I00001dvdMdQAI, 0010I00001dvdJDQAY, 0010I00001dvdJXQAY, 0010I00001dvdJ3QAI, 0010I00001cyy8wQAA, 0012800001Yi6JxAAJ, 0012800001Yi76BAAR, 0012800001Yi75SAAR, 0012800001YiKOwAAN, 0012800001YhpNhAAJ, 0012800001YiKZVAA3, 0012800000tnBmuAAE, 0012800000tnBorAAE, 0012800000tnB0sAAE, 0012800000tnB0qAAE, 0012800000tnB0rAAE, 0012800000tnB0iAAE, 0012800000tnB0jAAE, 0012800000tnB0kAAE, 0012800000tnB0lAAE, 0012800000tnB0mAAE, 0012800000tnB0oAAE, 0012800000tnB0pAAE, 0012800000tnB0nAAE, 0012800000tnB0tAAE

------------------------------------------------------
Conclusion:
HA HA what the difference here!
we can  extend  the current class contaxt to without sharing only if no sharing contaxt(with sharing or without sharing) is  specified

Hope with and without sharing thing is more to understand here!
Cheers!✍...

Convert a Lead to person Account


If your organization uses person accounts, 
you can convert leads to either person accounts or business accounts. 
Leads with a blank Company field are converted to person accounts. 
The default person account record type for your profile is applied to the new person account
Note that you can only create leads with a blank Company field using the Force.com API. Leads with a value in the Company field are converted to business accounts. The default business account record type for your profile is applied to the new business account.

As a best practice, use different lead record types and page layouts to differentiate leads that will be converted to person accounts from those that will be converted to business accounts. 
In particular, remove the Company field from the page layouts of leads(So that it can be blank) that will be converted to person accounts, and make the Company field required on the page layouts of leads that will be converted to business accounts.

function to check if a given array is BST or Not

//function to check if a given array is BST or Not
boolean canRepresentBST(int pre[]) 
              {
Stack<Integer> s = new Stack<Integer>();
                 int root = Integer.MIN_VALUE;    //  Integer.MIN_VALUE=2147483648

// Traverse given array
for (int i = 0; i < pre.length; i++) 
                       {
 if (pre[i] < root) /* if current elemnt is less than root*/
                           {
return false;
                          }

/*if   TOP of the stack  is less than the current element  
                          then  make  the ROOT= TOP  and TOP=ELEMENT  */
while (!s.empty() &&  s.peek() < pre[i]) 
                          {
root = s.peek();
                        s.pop();
                        }
                           s.push(pre[i]); 
                     }
return true;
             }


_______________________________________________________________
                                                                      50

                             30                                                                                                70
      20                                35                                                                60                           75

10           25               32          36                                              55             65           72           77

_______________________________________________________________

50   30     20    25      10    35     32       36            70    60         55    65   75    72    77

Note:
          put all the LEFT values in the stack until first right(25) come to traverse. when first right(25) comes then remove all the values(10,20) those are less than first right(25)  and store the rest values(25,30,50) in the stack and make the root=20 i.e  just less than the right value(25) and so on...



java.lang.Integer

[COPIED]-https://www.tutorialspoint.com/java/lang/java_lang_integer.htm

Introduction

The java.lang.Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.

Class Declaration

Following is the declaration for java.lang.Integer class −
public final class Integer
  extends Number
    implements Comparable<Integer>

Field

Following are the fields for java.lang.Integer class −
  • static int MAX_VALUE − This is a constant holding the maximum value an int can have, 231-1.
  • static int MIN_VALUE − This is a constant holding the minimum value an int can have, -231.
  • static int SIZE − This is the number of bits used to represent an int value in two's complement binary form.
  • static Class<Integer> TYPE − This is the class instance representing the primitive type int.

Class constructors

S.N.Constructor & Description
1
Integer(int value)
This constructs a newly allocated Integer object that represents the specified int value.
2
Integer(String s)
This constructs a newly allocated Integer object that represents the int value indicated by the String parameter.

Class methods

S.N.Method & Description
1static int bitCount(int i)
This method returns the number of one-bits in the two's complement binary representation of the specified int value.
2byte byteValue()
This method returns the value of this Integer as a byte.
3int compareTo(Integer anotherInteger)
This method compares two Integer objects numerically.
4static Integer decode(String nm)
This method decodes a String into an Integer.
5double doubleValue()
This method returns the value of this Integer as a double.
6boolean equals(Object obj)
This method compares this object to the specified object.
7float floatValue()
This method returns the value of this Integer as a float.
8static Integer getInteger(String nm)
This method determines the integer value of the system property with the specified name.
9static Integer getInteger(String nm, int val)
This method determines the integer value of the system property with the specified name.
10static Integer getInteger(String nm, Integer val)
This method returns the integer value of the system property with the specified name.
11int hashCode()
This method returns a hash code for this Integer.
12static int highestOneBit(int i)
This method returns an int value with at most a single one-bit, in the position of the highest-order ("leftmost") one-bit in the specified int value.
13int intValue()
This method returns the value of this Integer as an int.
14long longValue()
This method returns the value of this Integer as a long.
15static int lowestOneBit(int i)
This method returns an int value with at most a single one-bit, in the position of the lowest-order ("rightmost") one-bit in the specified int value.
16static int numberOfLeadingZeros(int i)
This method returns the number of zero bits preceding the highest-order ("leftmost") one-bit in the two's complement binary representation of the specified int value.
17static int numberOfTrailingZeros(int i)
This method returns the number of zero bits following the lowest-order ("rightmost") one-bit in the two's complement binary representation of the specified int value.
18static int parseInt(String s)
This method parses the string argument as a signed decimal integer.
19static int parseInt(String s, int radix)
This method parses the string argument as a signed integer in the radix specified by the second argument.
20static int reverse(int i)
This method returns the value obtained by reversing the order of the bits in the two's complement binary representation of the specified int value.
21static int reverseBytes(int i)
This method returns the value obtained by reversing the order of the bytes in the two's complement representation of the specified int value.
22static int rotateLeft(int i, int distance)
This method returns the value obtained by rotating the two's complement binary representation of the specified int value left by the specified number of bits.
23static int rotateRight(int i, int distance)
This method returns the value obtained by rotating the two's complement binary representation of the specified int value right by the specified number of bits.
24short shortValue()
This method returns the value of this Integer as a short.
25static int signum(int i)
This method returns the signum function of the specified int value.
26static String toBinaryString(int i)
This method returns a string representation of the integer argument as an unsigned integer in base 2.
27static String toHexString(int i)
This method returns a string representation of the integer argument as an unsigned integer in base 16.
28static String toOctalString(int i)
This method returns a string representation of the integer argument as an unsigned integer in base 8.
29String toString()
This method returns a String object representing this Integer's value.
30static String toString(int i)
This method returns a String object representing the specified integer.
31static String toString(int i, int radix)
This method returns a string representation of the first argument in the radix specified by the second argument.
32static Integer valueOf(int i)
This method returns a Integer instance representing the specified int value.
33static Integer valueOf(String s)
This method returns an Integer object holding the value of the specified String.
34static Integer valueOf(String s, int radix)
This method returns an Integer object holding the value extracted from the specified String when parsed with the radix given by the second argument.

Methods inherited

This class inherits methods from the following classes −
  • java.lang.Object

//count set(1) bits in a integer

//count set(1) bits in a integer 
public static int coutSetBits(int num)
 {
       int count=0;
       while(num!=0)
               {
                   count+=num&1;
                   num=num>>1;
                }
        return count;
 }

  public static int coutSetBitsUsingBK_Algo(int num)
  {
  int count=0;
      while(num!=0)
  {
  num=num&(num-1);
  count++;
  }
return count;
  }

instanceof Operator

instanceof Operator :

Here's an example of instance of operator.
class instanceofOperator {
    public static void main(String[] args) {
     
     String test = "asdf";
     boolean result;
     
     result = test instanceof String;
     System.out.println(result);
    }
}

Unary Operators

Unary operator performs operation on only one operand.


OperatorMeaning
+Unary plus (not necessary to use since numbers are positive without using it)
-Unary minus; inverts the sign of an expression
++Increment operator; increments value by 1
--decrement operator; decrements value by 1
!Logical complement operator; inverts the value of a boolean

Doubly Linked List JAVA

import java.util.Scanner;
public class ImpLinkedList_D
{
  public static void main(String args[])
  {
Scanner scan = new Scanner(System.in);
    DLL objDLL=new DLL();
   int val=0;
 
   do
{
System.out.println("Enter Element: Zero(0) is not allowed");
val=scan.nextInt();
Node_d n=new Node_d();
n.val=val;
n.ref_n=null;
//n.ref_p=null;

if(objDLL.start==null)
{
objDLL.start=n;
objDLL.end=objDLL.start;
}
else

     
       if(objDLL.end==objDLL.start)
   {
n.ref_p=objDLL.start;
//objDLL.end.ref_p=null;
objDLL.end.ref_n=n;
objDLL.end=n;
   }
   else
   {
n.ref_p=objDLL.end;
objDLL.end.ref_n=n;
//objDLL.end.ref_p=objDLL.end;
objDLL.end=n;
   }
}


System.out.println("CURRENT LIST \n");
printList(objDLL);
}while(val!=0);
 
  }
 
  public static void  printList(DLL objDLL)
  {
   Node_d itrVar=objDLL.start;
   int i=0;
   while(itrVar!=null)
   {
System.out.println("List Element:-"+i+"\n itrVar.val="+itrVar.val+"\n itrVar.ref_p="+itrVar.ref_p+"\n itrVar.Ref_n="+itrVar.ref_n+"\n");
i++;
         itrVar=itrVar.ref_n;
   }
  }

 
}
class DLL
{
  Node_d start;
  Node_d end;
}
class Node_d
{
   int val=0;
   Node_d ref_n;
   Node_d ref_p;
 
}

Single Linked List JAVA

import java.util.Scanner;
public class ImpLinkedList
{
  public static void main(String args[])
  {
Scanner scan = new Scanner(System.in);
    SLL objSLL=new SLL();
   int val=0;
 
   do
{
System.out.println("Enter Element: Zero(0) is not allowed");
val=scan.nextInt();
Node n=new Node();
n.val=val;
n.ref=null;

if(objSLL.start==null)
{
objSLL.start=n;
objSLL.end=objSLL.start;
}
else
{
objSLL.end.ref=n;
objSLL.end=n;
}


System.out.println("CURRENT LIST \n");
printList(objSLL);
}while(val!=0);
 
  }
 
  public static void  printList(SLL objSLL)
  {
   Node itrVar=objSLL.start;
   int i=0;
   while(itrVar!=null)
   {
System.out.println("List Element:-"+i+"\n itrVar.val="+itrVar.val+"\n itrVar.Ref="+itrVar.ref+"\n");
i++;
         itrVar=itrVar.ref;
   }
  }

 
}
class SLL
{
  Node start;
  Node end;
}
class Node
{
   int val=0;
   Node ref;
}

call definition of $A.enqueueAction(action);

fn: function(cmp, event, helper)
    {
        var action=cmp.get("c.apexMethod");
         action.setParams({ "param1" : 'value1'});
       
         action.setCallback(this, function(response) {
            var state = response.getState();
             if (state === "SUCCESS")
            {
                var result=response.getReturnValue();
                 if(!$A.util.isUndefinedOrNull(result) && !$A.util.inEmpty(result))
                  {
                       //some code here
                   }
            }
            if (state === "NEW")
            {
                console.log("The action was created but is not in progress yet");
            }
           else if (state === "RUNNING")
            {
                console.log("The action is in progress");
               
            }
             else if (state === "ERROR")
            {
                console.log("The action is in progress");
               
            }
             else if (state === "INCOMPLETE")
            {
                console.log("The server didn't return a response. The server might be down or the client might be offline. The framework guarantees that an action's callback is always invoked as long as the component is valid. If the socket to the server is never successfully opened, or closes abruptly, or any other network error occurs, the XHR resolves and the callback is invoked with state equal to INCOMPLETE.");
               
            }
              else if (state === "ABORTED")
            {
                console.log("The action was aborted. This action state is deprecated. A callback for an aborted action is never executed so you can’t do anything to handle this state.");
               
            }
         });
       $A.enqueueAction(action);
      }

How to get all component's attributes value by one call of component.get();

In This article,I explained how to set all component's attributes value by one call.
In the similar way we can get all the componets values by one call of componet.get()

<!--- Lightning Component  ---->
<aura:component>
<aura:attribute name="map" type="Map" default="{str1:null,str2:null,obj:null}"/>
<aura:attribute name="str1" type="String"  default="{!v.map.str1}"/>
 <aura:attribute name="str2" type="String" default="{!v.map.str2}"/>
 <aura:attribute name="obj" type="Contact" default="{!v.map.obj}"/>
</aura:component>




------------Lightning Component Controller--------------------------
fn: function(cmp, event, helper)
    {
        var map=cmp.get("v.map");//top or START of function
           console.log(map.str1);
           console.log(map.str2);
       
}

Counters