Sunday, 15 October 2017

Does java support pointer?

Java doesn't support pointer directly.

Reason: Improper usage and handling of pointer may lead to memory leakage along with reliability issue.

Apart from this pointer can be used to access the memory area directly and thus could help users with malicious intent in altering sensitive data stored at the memory location in illegal way. This could in turn pose serious security issue. But java is secured language because it does not using pointer.

And most probably this is the reason why java makers decided to omit pointer from java.

Saturday, 16 September 2017

TreeSet in java


TreeSet 
is a class that implements Set interface and extends AbstractSet class. By default, elements in TreeSet class is stored in ascending order (see example given below).


Key feature of TreeSet

1.     TreeSet contains only unique elements.
2.     Maintains ascending order (by default). 
3.     Access and retrival time is fast in TreeSet.


Now let’s look at the ways in which you can create TreeSet in java,
  • ·        TreeSet <E> mySet = new TreeSet<E>();     //<E> denotes generics
  • ·        TreeSet <E> mySet = new TreeSet<E>( Collection c);     //<E> denotes generics


Simple Example:

package javaRadarTreeSet;

import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetExample {

      public static void main(String[] args) {
            //Create treeset
            TreeSet<String> treeSet = new TreeSet<String>();
            //Add elements to treeset
            treeSet.add("Omjit");
            treeSet.add("Krishna");
            treeSet.add("Mithilesh");
            treeSet.add("Manish");
            treeSet.add("Puja");
            treeSet.add("Shweta");
            treeSet.add("Rita");

            //Traverse the elements of treeset
            Iterator<String> setIterator = treeSet.iterator();
            while(setIterator.hasNext()){
                  String element = setIterator.next();
                  System.out.println(element);
            }

      }

}
OUTPUT:
Krishna
Manish
Mithilesh
Omjit
Puja
Rita
Shweta

Now let’s look at different ways in which you can use the TreeSet that you created. 
Click on each link to read about them.

TreeSet operations

·         Sort the elements in TreeSet  --> (by default its sorted in ascending order)
·         Search element in TreeSet
·         Remove element from a particular index in TreeSet

Traverse TreeSet

·         Traverse TreeSet using Iterator
·         Traverse TreeSet using ListIterator
·         Traverse TreeSet using Enumeration

Thursday, 17 August 2017

What is the advantage of iterating Collection using iterator?

We can use for loop, advanced for loop or iterator for iterating over Collection.

But using iterator over other competitors gives some advantages like:

  • By using for loop you cannot update(add/remove) the Collection whereas with iterator we can easily update Collection.
Example for updating list by iterator:

package javaRadarArrayList;

import java.util.ArrayList;
import java.util.Iterator;

public class UpdateList {

      public static void main(String[] args) {
            //create Array List
            ArrayList<String> javaRadarList = new ArrayList<String>();
            //Add elements to ArrayList
            javaRadarList.add("Java");
            javaRadarList.add("Spring");
            javaRadarList.add("Hibernate");
            javaRadarList.add("EJB");

            System.out.println("Original list:" +javaRadarList);
           
            Iterator<String> itr = javaRadarList.iterator();
            while(itr.hasNext()){
                  String token = itr.next();
                  itr.remove();
                  System.out.println("list afer modification:" +javaRadarList);
            }
                 
      }

}

OUTPUT:
Original list:[Java, Spring, Hibernate, EJB]
list afer modification:[Spring, Hibernate, EJB]
list afer modification:[Hibernate, EJB]
list afer modification:[EJB]
list afer modification:[]


NOTE: Try to remove object from Collection using for loop and see if you can or not.

  • In situations where there is no idea of what type of Collection will be used we can use iterator to traverse over the collection as all Collection implements iterator interface.

Sunday, 13 August 2017

Difference: HashMap vs HashTable


HashMap and HashTable differ based on the following parameters:

  • Nature
           HashMap is not synchronized in nature.
           HashTable is synchronized in nature

  • Traversed by
           HashMap can be traversed by using an iterator
           HashTable can be traversed by using an iterator or enumerator

  • Null value/key
          HashMap allows only one null key and any number of null values.
          HashTable doesn't allow key or value as null.

  • fail-fast nature of iterator
          Iterator in HashMap is fail-fast.
          Enumerator in HashTable is not fail-fast.

  • Performance
          HashMap is faster performance wise.
          HashTable is slower in comparision to HashMap performance wise.
          
          Reason: Since HashTable is synchronized in nature so it doesn't allow multi-threading                                       operation on a particular resource at a time thus turning out to be slower than                                       HashMap which is non-synchronized by nature.



You may also like to read:



Saturday, 12 August 2017

Ten common java interview question


Today I am going to share with my readers the ten commonly asked interview questions in java interview. These questions are based on survey that I conducted on my friends who were attending interview of different companies.

This survey was conducted majorly on candidates having 1-4 years of experience in java.

Iterator is considered fail-fast as it will fail immediately if it feels that the Collection on which it is iterating currently is about to get modified by self/another thread. Here modification means addition, deletion or updating of element. And this modification may result in non-deterministic behavior at an undetermined time in future. In such cases, iterator fails by throwing  ConcurrentModificationException. Click to learn more with example

String is immutable. A detailed explanation citing the reason behind this immutability is already written in this article




We know String is immutable in java. So to perform modification on String we use either StringBuffer or StringBuilder. Though they perform same task but still they differ on certain parameters. Lets check out the difference here



This is one of the most frequently asked java interview question. You will use either of them while coding number of times.
So lets take a walk through to the difference between the two.

First thing that you should be aware of is that both are used to compare two objects. But this question is generally asked in relation to String because each of them when used to compare String Objects behaves differently. Lets see the detailed explanation here

Array list and Linked list both implements list interface. Now if you have to use one then which one will you prefer. For this we have to think about the number of elements we have to insert or delete from the list, memory consumption, which will help in searching the element faster, etc. Click to read more...

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.