Monday, December 20, 2010

LinkedHashMap

Some insight on LinkedHashMap:

The Class LinkedHashMap is an extension of HashMap with specific feature of retaining the insertion order. Hence useful when order of key-value pairs are to be retained. Also if one inserts the key again into the LinkedHashMap the original orders is retained.

Such Collection is useful when you want to convert a record fetched from the database into a Java Object.

The main advantage with this is that now we can shift the sorting logic of retrieved data into database rather than writing custom code here; that help in boosting performance.

Sunday, December 19, 2010

Number of Processors

If your coworker or colleague ever asked you to find out how many processor does the linux server have, then...

Simply issue the following command as root

# cat /proc/cpuinfo | grep processor

Everything in linux is files. This command simply retrieves the number of processors that linux detected from /proc/cpuinfo and displays it. Usually, the processor number comes at the first set of line from issuing the command.

Here’s what the command returned for me:
~~~~~~~~~~~~~~~~~~~~~~~~
processor : 0
~~~~~~~~~~~~~~~~~~~~~~~~

which means You have 1 processor with a processor ID number 0. Processor counting starts with 0.
So if you have a PC with core duo, you will probably have 2 lines that says 0 and 1.That is 2 processors.


You can run the command below and you will see number of physical processors in the server:

[server][root][~]# grep “^physical id” /proc/cpuinfo | awk ‘{print $NF}’ | uniq | wc -l
1

Now lets determine how many cores we have:

[server][root][~]# cat /proc/cpuinfo | grep processor | wc -l
4

32-bit or 64-bit ?!?!?

How to check if the machine is 32-bit or 64-bit ??

We often need to know if the machine is 32-bit or 64-bit before we install any software. Here are few commands which will help you figure out.

1. file /bin/ls

2. getconf LONG_BIT

Run either of the above two commands on the terminal and you will be able to figure it out.

Saturday, March 28, 2009

Convert ASCII to INTEGER



ASCII stands for American Standard Code for Information Interchange and is pronounced with a hard 'c' sound, as ask-ee. It is a way of defining a set of characters which can be displayed by computer on a screen. Basic ASCII uses seven bits to define each letter, so it can have up to 128 specific identifiers. It is represented by a byte, where eight bit is set aside for error checking function.


int atoi(char * input){

int output = 0;

int sign = 1; // default positive sign.

int i =0;

if(input==NULL||input==’’){

sysout(“Invalid input”);

return 0;

}

if(input[0]==’-’){

sign=-1;

i=1;

}else if(input[0]==’+’){

sign=1;

i=1;

}

while(input!=’\0’){

int temp = input[i] – ‘0’;

if(temp<0>9){

println(“Invalid Input”);

return output;

}

output=output*10+temp;

if(output>0 && sign==-1){

println(“Input underflow”);

return 0;

}

if(output<0 sign="=">

println(“Input Overflow”);

return 0;

}

i++;

}

return output*sign;

}

Test cases:

SNo

Input

Output

1

123

123

2.

+123

123

3.

-123

-123

4.

123ABC

123 and print a msg “Invalid Input”

5.

ABC123

print a msg “Invalid Input”

6.

99999999999999999999

Input OverFlow

7.

-99999999999999999999

Input UnderFlow

8.

NULL

Invalid Input

9.

“”

Invalid Input

Return output;

}