Saturday, November 16, 2013

OOPS explanation (concepts)..10/10

OOPS : (Object Oriented Programming System)

Below are the main OOPS concepts...

Object:
Class:
Abstraction:
Encapsulation:
Inheritance:
Polymorphism:
MessagePassing:

Object:
   - An object represents anything that is really existing in the world. Object exists physically. JVM will allocate separate memory for object. because object is physically exist.

Class:
  - A class is model/idea/blue print for creating Objects. JVM can't allocate memory for class, because class doesn't exist physically.

Abstraction:
  - Hiding unnecessary data from user. [ view is an example for abstraction in oracle].
   Hiding implementation details is called Abstraction.
  advantages:  - It increases security
  - Enhancement is easy
  - Improves maintainability

Encapsulation:
  - Binding of data and methods as a single unit.
  example : class
   advantages:  - we can use the same variables or names in different classes.
   ----- If any class contains DataHiding + Abstraction such type of class is called Encapsulation.
  example: java bean
  advantages:  - It increases security
   - Enhancement is easy
   - Improves maintainability

Inheritance: 
  - Producing a new class from existing class.
  advantages : Re-usability of the code.

Polymorphism:
  - If something exists in several forms is called polymorphism. If same method is performing different tasks it is called polymorphism.

Message passing: 
  - Calling a method in OOPS is called message passing.



Friday, November 15, 2013

Main important usable Terms in Collection classes / implementations...10/10


Below are most important terms that we forget regularly....

  • Synchronized - Vector, Hashtable
  • NonSynchronized - All remaining
  • Ordered - ArrayList, Vector, LinkedList, LinkedHashSet, LinkedHashMap
  • Sorted - TreeSet, TreeMap
  • Homogeneous elements - TreeSet, TreeMap
  • Heterogeneous elements - remaining all
  • Allow nulls - ArrayList, Vector, LinkedList, HashSet, LinkedHashSet, 
  • No nulls - TreeSet
  • Allow nullkeys - HashMap
  • No nullkeys - HashTable
  • Allow nullvalues - HashMap
  • No nullvalues - HashTable
  • Iterator - All List and Set implements, All Map implements keySet's
  • ListIterator - only List implements, 
  • Enumeration - legacy Vector, HashTable
  • toArray - All List, Set implements
*** Since List, Set implementations are the instance of Iterable interface, we can iterate them using for-each loop at any time. ***

Monday, November 11, 2013

Count number of duplicate elements in ArrayList objects using Collections API?


Usage of Collections:

Suppose we have an ArrayList of characters / words which are repeated multiple times, And we want to count number of occurrences of each character / word..

We use only collections API.

Take an Example class

Test.java
-----------
import java.util.*;
public class Test{

        public static void main(String ar[]){

                ArrayList al=new ArrayList(); //It allows duplicates
                        al.add("a");
                        al.add("b");
                        al.add("c");
                        al.add("a");
                        al.add("b");
                        al.add("a");

                Set alSet=new HashSet(al); // no duplicates allowed in set, we get uniques

                        for(String element:alSet){ //using for-each loop
                                  int count=Collections.frequency(al,element); // this method counts the occurrences
                                 System.out.println(element+" repeated "+count+" times");
                        }//end for
        }//end main
}//end class



This is one of many methods,,

And similarly we can use Map object to count repeated characters/words..... Will be continued 

Sunday, November 10, 2013

How to print / iterate key and values of any map object?


Displaying key, values of any Map object - simplest way :

     We have many ways to display / iterate key, values of a map object. Here I am following advanced for loop and keySet.

Let us take a sample map object..

Map m=new HashMap();
                               m.put("1","one");
                               m.put("2","two");
                               m.put("3","three");
                               m.put("4","four");
                               m.put("5","five");
                               m.put("6","six");

// now we want to display all the keys with their respected values.

//We have a method called keySet() in Map interface. And this HashMap class implements that method.

//This method returns a Set object of  String elements ( we mentioned keys are strings in map object above).

 Set keySet=m.keySet();

//now we can iterate this Set without the help of iterator, because as per java guidelines from here. we can directly iterate set object using for-each loop with out the help of iterator object. So

 for(String key:keySet){
     System.out.println(key+":"+m.get(key)); // will print both key and value, and iterates..till last key.
 }//end for loop

// instead we can also write in a simple step using below line

for(String key:m.keySet()){
   System.out.println(key+":"+m.get(key));
}//end for loop

This is the benefit of for-each loop. Here we use for-each instead of using iterator object, and also we are no need to check for next object existence. 


code snippet:

Test.java
import java.util.*;
public class Test{

public static void main(String nag[]){

Map mp=new HashMap();
mp.put("i","one");
mp.put("2","two");
mp.put("3","three");

System.out.println("iterating....");
Set keySet=mp.keySet();
for(String key:keySet){
                       System.out.println(key+":"+mp.get(key));
}//end loop

            System.out.println("trying new one..:");
         for(String key:mp.keySet()){
                 System.out.println(key+"-"+mp.get(key));
}//end loop

System.out.println("done with new tech..");
}//end main
}//end class




Please send your valuable feedback to :
javaojavablog@googlegroups.com (or) nagarjuna.lingala@gmail.com