Tuesday, May 3, 2011

What is ConcurrentHashMap-identityHashMap-WeakHashMap ?

Q) What is ConcurrentHashMap?
Ans) A concurrentHashMap is thread-safe implementation of Map interface. In this class put and remove method are synchronized but not get method. This class is different from Hashtable in terms of locking; it means that hashtable use object level lock but this class uses bucket level lock thus having better performance.

Q) What is identityHashMap?
Ans) The IdentityHashMap uses == for equality checking instead of equals(). This can be used for both performance reasons, if you know that two different elements will never be equals and for preventing spoofing, where an object tries to imitate another.
Q) What is WeakHashMap?
Ans) A hashtable-based Map implementation with weak keys. An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use. More precisely, the presence of a mapping for a given key will not prevent the key from being discarded by the garbage collector, that is, made finalizable, finalized, and then reclaimed. When a key has been discarded its entry is effectively removed from the map, so this class behaves somewhat differently than other Map implementations.

Soft references act like a data cache. When system memory is low, the garbage collector can arbitrarily free an object whose only reference is a soft reference. In other words, if there are no strong references to an object, that object is a candidate for release. The garbage collector is required to release any soft references before throwing an OutOfMemoryException.

Weak references are weaker than soft references. If the only references to an object are weak references, the garbage collector can reclaim the memory used by an object at any time. There is no requirement for a low memory situation. Typically, memory used by the object is reclaimed in the next pass of the garbage collector.
 

Difference between ArrayList, LinkedList and Vector



  • ArrayList is in most cases what you want to use. It is a list backed by an array, which means it has fast access to each element via the get method.




  • Vector is a leftover from the early days of Java, retrofitted with the List interface. The chief difference from ArrayList is that its methods are synchronized (ArrayList's are not). That means it is easier to use in multi-threaded environments, but it does incur the synchronization overhead.




  • LinkedList is backed by a doubly-linked list, not an array. That means it's fast to access elements at the start and the end of the list, but less so in the middle. On the other hand, inserting and deleting elements is fast compared to ArrayList






  • When to use ArrayList or LinkedList ?
    Ans)  Adding new elements is pretty fast for either type of list. For the ArrayList, doing  random lookup using "get" is fast, but for LinkedList, it's slow. It's slow because there's no efficient way to index into the middle of a linked list. When removing elements, using ArrayList is slow. This is because all remaining elements in the underlying array of Object instances must be shifted down for each remove operation. But here LinkedList is fast, because deletion can be done simply by changing a couple of links. So an ArrayList works best for cases where you're doing random access on the list, and a LinkedList works better if you're doing a lot of editing in the middle of the list. 
    Source : Read More - from java.sun

    ArrayList

    Now for some implementation notes. The ArrayList is actually encapsulating an actualy Array, an Object[]. When you instanciate ArrayList, an array is created, and when you add values into it, the array changes its size accordingly. This gives you strengths and weaknesses:
    • Fast Random Access
    • You can perform random access without fearing for performence. Calling get(int) will just access the underlying array.
    • Adding values might be slow When you don’t know the amount of values the array will contain when you create it, a lot of shifting is going to be done in the memory space when the ArrayList manipulates its internal array.
    • Slow manipulation When you’ll want to add a value randomly inside the array, between two already existing values, the array will have to start moving all the values one spot to the right in order to let that happen.

    LinkedList

    The LinkedList is implemented using nodes linked to each other. Each node contains a previous node link, next node link, and value, which contains the actual data. When new data is inserted, a node is inserted and the links of the surrounding nodes are updated accordingly. When one is removed, the same happens – The surrounding nodes are changing their links and the deleted node is garbage collected. This, as well, gives strengths and weaknesses:
    • Fast manipulation As you’d expect, adding and removing new data anywhere in the list is instantanious. Change two links, and you have a new value anywhere you want it.
    • No random access Even though the get(int) is still there, it now just iterates the list until it reaches the index you specified. It has some optimizations in order to do that, but that’s basically it.

    Some Conclusions

    ArrayList is very useful when a well defined set of data is needed in a List interface as opposed to an array. It can be dynamically changed, but try not to do so frequently throughout the life of the application. LinkedList is there for you to do just that: Manipulating it is very easy, and as long as its used for iteration purposes only and not for random accessing, it’s the best solution. Further, if you need random accessing from time to time, I suggest toArray for that specific moment.
    Another point I didn’t raise here is the Queue issue. LinkedList implements extended abilities to the normal List interface which allows it to add and remove elements from its beginning and end. This makes the LinkedList perfect for Queue and Stack purposes – Although in Java 5 they already added a Stack class.
    Hope this helped someone. Tell me if you want to differ.

     

    How to Print your Blogger Posts?

    How to Print your Blogger Posts

    To notify your readers that your posts can be printed effectively, you'll probably want to add a "Print" link beneath each of your Blogger posts.

    Follow steps

    1. Log in Blogger Dashboard

    2. Click Design --> edit HTML --> click "expand widget templates" and search for this tag </head>

    3. Immedietly before this line, add the following lines of code:
    <style media='print' type='text/css'>
    #header-wrapper, #header, .header, #sidebar-wrapper, .sidebar, #footer-wrapper, #footer, .date-header, .post-meta-data, .comment-link, .comment-footer, #blog-pager, #backlinks-container, #navbar-section, .subscribe_notice, .noprint {display: none;}
    #main-wrapper {width: 95%}
    </style>

    4. Now search for this code:
    <p><data:post.body/></p>

    5. Immediately after this line, paste following lines of code:
    <b:if cond='data:blog.pageType == &quot;item&quot;'>
    <a href='javascript:window.print()'>Print this post</a>
    </b:if>

    6. Save Template.

    How do you swap two variables in JAVA without using a third one bu using bitwise operator ?


    Answer:
    /*Program to swap 2 values without using the temporary variable and Arithmetic operators*/

    class Swap
    {
    public static void main(String args[])
    {
    int a=1;
    int b=2;
    System.out.println("Before swap: a="+a+"b="+b);
    a=a^b;
    b=a^b;
    a=a^b;
    System.out.println(" After swap: a="+a+"b="+b);
    }
    }

    Another Method

    class Swap
    {

    • public static void Swap()

    • {

    • int num1 = 10;

    • int num2 = 20;

    • System.out.println("Before Swapping");

    • System.out.println("Value of num1 is :" + num1);

    • System.out.println("Value of num2 is :" +num2);

    • //add both the numbers and assign it to first

    • num1 = num1 + num2;

    • num2 = num1 - num2;

    • num1 = num1 - num2;

    • System.out.println("Before Swapping");

    • System.out.println("Value of num1 is :" + num1);

    • System.out.println("Value of num2 is :" +num2);
    • }
    • }
    •