HashSet with Example


If  our requirement is frequent insert then we go with HashSet.
For efficiency objects added to a HashSet need to implement the hashCode() method in a manner that properly distributes the hash codes. While most system classes override the default hashCode() implementation of the Object.
It is generally faster to add elements to the HasSet then convert the collection to a TreeeSet for sorted traversal.
To optimize HashSet space usage , you can tune initial capacity and load factor.
Example:
public class HashSetExample {

    public static void main(String[] args) {

        HashSet<String> hs=new HashSet<String>();

        // duplicate element is not permitted
        hs.add("b");
        hs.add("a");
        hs.add("c");
        hs.add("d");
        hs.add("d");

        Iterator it=hs.iterator();

        while(it.hasNext())
        {
            String value =(String)it.next();

            System.out.println("Value :"+value);
        }

        System.out.println("Size :"+hs.size());

        hs.remove("d");

        hs.clear();
    }
}

What are the differences between interface and abstract class?


The key difference between an interface and the corresponding abstract class is that a class or interface can have unlimited number of immediate super interface but it can only have one super class.
There are two primary axes of “inheritance” an object-oriented language like java. Implementation inheritance is where the sub-class inherits the actual code implementation from the parent. Interface inhyeritance is where the sub-class adheres to the public interface of the parent.
Also java actally mixes the two notions together a bi…. java interfaces are nice and clean. When you implement an interface, you are stipulating that your class adheres to the “contract” of the interface that you spefified. Java class inheritance isn’t so clean when you sub-class in java you are getting both the code inheritance but you are also stipulating that you sub-class adheres to the contract of the interface of the parent class.

Abstrct class in java are just like regular java classes but with the added constraint that you cannot instance then directly. In terms of that added constraint, they are basically classes which don’t actually implement all lof the code specified by their “contract”.

What is the difference between callable statements, prepare statements , createstatements?


  • CallableStatement: This is used to retrieve the stored procedures from the database. ie., these statements are used when two or more SQL statements are retrieved over and over.
  • PreparedStatement: These are pre-compiled statements which are stored in database. We are using these, if the same query is to be executed over and over.
  • CreateStatement: This is used to execute single SQL statement.

Difference between Hashtable & HashMap?


Hash table is a Collection .To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method.  Hashtable  is Synchronized and permit not null values only.
Hashtable maps keys to values. Any non-null object can be used as a key or as a value.
The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.

What is a singleton class?How do you writer it? Tell examples?


A class which must create only one object (ie. One instance) irrespective of any number of object creations based on that class.

Steps to develop single ton class:-

1.Create a Private construcor, so that outside class can not access this constructor. And declare a private static reference of same class

2.Write a public Factory method which creates an object. Assign this object to private static Reference and return the object

Sample Code:-

class SingleTon

{

private static SingleTon st=null;

private SingleTon()

{

System.out.println(“\nIn constructor”);

}

public static SingleTon stfactory()

{

if(st==null) st=new SingleTon();

return st;

}

}

 

 

For complete Example of Singleton design pattern click here