Wednesday 21 January 2015

Java Program to find even numbers and odd numbers

It is very basic program to start with programming.You might be knowing that even numbers are those numbers which are divisible by 2 and odd numbers are those which are not.

So lets see  implementation in the example given below:


 import java.util.Scanner;  
 /**  
  *   
  */  
 /**  
  * @author Dixit  
  *  
  */  
 public class EvenOddExample {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
              int x;  
              System.out.println("Enter number to check if it is odd or even ");  
              Scanner in = new Scanner(System.in);  
              x = in.nextInt();  
              if ( x % 2 == 0 )  
                System.out.println("You entered an even number.");  
              else  
                System.out.println("You entered an odd number.");  
      }  
 }  



Enjoy Reading.


Tuesday 20 January 2015

How to iterate or traverse ArrayList in Java


How to iterate or traverse ArrayList in Java


There are many ways to traverse over ArrayList.For ex: simple for loop,for-each loop and Iterator.

Lets have a look at the implementation of all different ways of traversing ArrayList.


1:  import java.util.ArrayList;  
2:  import java.util.Iterator;  
3:  /**  
4:   *   
5:   */  
6:  /**  
7:   * @author   
8:   *  
9:   */  
10:  public class ArrayListTraverseExample {  
11:       /**  
12:        * @param args  
13:        */  
14:       public static void main(String[] args) {  
15:      ArrayList<String> listCompanies = new ArrayList<String>();  
16:      listCompanies.add("Infosys");  
17:      listCompanies.add("TCS");  
18:      listCompanies.add("Cognizant");  
19:      listCompanies.add("Google");  
20:      System.out.println("Size of ArrayList : " + listCompanies.size());  
21:      System.out.println("\n*******Traversing ArrayList using for-each loop*******");  
22:      for(String company: listCompanies){  
23:        //print all elements from ArrayList one by one.  
24:        System.out.println("List of Companies are:"+company);  
25:      }  
26:      System.out.println("\n*******Traversing ArrayList using Iterator*******");  
27:      Iterator<String> itr = listCompanies.iterator();  
28:      while(itr.hasNext()){  
29:            System.out.println("List of Companies are:"+itr.next());  
30:      }  
31:      //You can also Loop over ArrayList using traditional for loop  
32:      System.out.println("\n*******Traversing ArrayList using simple for loop*******");  
33:      for(int i =0; i<listCompanies.size(); i++){  
34:          System.out.println("List of Companies are:"+listCompanies.get(i));  
35:      }  
36:       }  
37:  }  

Output:

Size of ArrayList : 4

*******Traversing ArrayList using for-each loop*******
List of Companies are:Infosys
List of Companies are:TCS
List of Companies are:Cognizant
List of Companies are:Google

*******Traversing ArrayList using Iterator*******
List of Companies are:Infosys
List of Companies are:TCS
List of Companies are:Cognizant
List of Companies are:Google

*******Traversing ArrayList using simple for loop*******
List of Companies are:Infosys
List of Companies are:TCS
List of Companies are:Cognizant
List of Companies are:Google




Please go through the program properly.Enjoy Reading.

Difference between TreeSet and TressMap


TreeSet and TreeMap are two different Collection class in java.TreeSet implements Set interface whereas TressMap impements Map interface.


                                                     TreeSet Vs TreeMap




               TreeSet

              TreeMap
 Implementation

 It implements Set interface.
 It implements Map interface.
 Storing Objects 

 TressSet stores only one objects.
 TreeMap stores two objects i.e.key and value.

 Duplicates
 TreeSet does not allow duplicates value.
 TreeMap allows duplicate values.

 Specific Implementation
 It implements NavigableSet.
 It implements NavigableMap.






Study Well.Enjoy Reading.





Difference between ArrayList and HashMap


Both ArrayList and HashMap belongs to different Collection class in java.Both of them are used to store objects but their internal working is totally different


.                                              ArrayList Vs HashMap





       ArrayList

         HashMap
 Implementation
 ArrayList implements List interface.

 HashMap implements Map interface.
 Storage of Objects
ArrayList only stores one object. 

 HashMap stores tow object one is key and other is value.
Equals and HashCode 


 ArrayList does not have to implements these methods.
 HashMap must implement equals and hashcode method properly.
Order of Insertion 

 ArrayList maintains order of insertion.
 HashMap does not maintains order of insertion.





Happy Reading.Study Well.

Sunday 18 January 2015

Java Program to Find IP Address of the Machine


                    Java Program to find IP Address of the Machine



import java.net.InetAddress;
import java.net.UnknownHostException;


public class FindIpAddress {

  /**
   * @param args
   */
  public static void main(String[] args) {
    
     
              InetAddress myIp = null;
        try {
          myIp = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
          System.out.println("Exception caught ="+e.getMessage());
        }
              System.out.println("IP of my system is := "+myIp.getHostAddress());
       
  }

}













Enjoy Reading.

Java Program to Print Prime Numbers


                                  Java Program to Print Prime Numbers

This java program prints prime numbers between the input value provided by the user. The smallest prime number starts with 2.



1:  import java.util.Scanner;  
2:  public class Prime {  
3:       /**  
4:        * @param args  
5:        */  
6:       public static void main(String[] args) {  
7:            int iNumber,iRemainderValue;  
8:            //Scanner class used for I/O operations  
9:            Scanner scanner=new Scanner(System.in);  
10:            System.out.println("Enter the number till which you want to generate prime numbers:");  
11:            iNumber=scanner.nextInt();  
12:            //outer loop iterates till iNumber value   
13:            for(int i=2;i<iNumber;i++)  
14:            {  
15:                 iRemainderValue=0;  
16:                 //inner loop checks if value of i is divisible by j values and sets the   
17:                 //iRemainderValue value accordingly.  
18:                 for(int j=2;j<i;j++)  
19:                 {  
20:                      if(i%j==0)  
21:                           iRemainderValue=1;  
22:                 }  
23:                 if(iRemainderValue==0)  
24:                 {  
25:                      System.out.print(i +" ");  
26:                 }  
27:            }  
28:       }  
29:  }  


Enjoy Reading.

Difference between ArrayList and Vector


Difference between ArrayList and Vector comes under Core Java interview questions.It is the most basic questions asked to check the interviewee basic knowledge of Collection before going deep in Collection.


                                                         ArrayList Vs Vector




              ArrayList
                Vector
 Synchronization
 ArrayList in not synchronized means it is not suitable for multi-threaded environment.

 Vector is synchronized.
 Performance
 Arraylist are faster as it is not synchronized.

 Vector performance is slow as they are thread safe.
 Resize
 ArrayList grow half of its size when resized.

 Vector grow double of its size when resized.
 Iteration method
 ArrayList does not uses enumerator for iteration.

 Vector uses enumerator for  iteration.



Read carefully and understand the difference.
Enjoy Reading