Ads Sponsored By Google :)

Showing posts with label Collection Frame Work. Show all posts
Showing posts with label Collection Frame Work. Show all posts

Wednesday, 14 August 2013

How many ways to Traverse HashMap in java With Example Code

how to traverse a hashmap in java,java code to traverse a hashmap,traverse a hashmap java ,ways of traverse the map in java, traversing hashmap using foreach loop, traversing hashmap using KeySet Iterator, traversing hashmap using entrySet, Java Code for

traversing Hashmap in java.


   In Java Collection Frame Work we have multiple ways of iterating the data from the Map, HashMap and TreeMap. As am java developer one of my colleagues attended the interview of experience of 4years on java. Then he got the question like this, write the java to iterate or retrieve the data from the hashmap or TreeMap or map in atleast four ways. So Candidate who going to attend for the java interview of experience of 2+, 3+, 4+,5+, 6+, 7+ etc..
You can find the this type of questions in the interview. Why because these candidates did so many projects in real time, so if the interviewer think that these guys must have command on retrieving the data through Collection Object  by using the HashMap. Map, TreeMap Objects.

In this tutorial we are going to retrieve the data from the Object of HashMap, Map, TreeMap. We are retrieving or traversing or iterating the data from HashMap, Map, TreeMap Object in five different ways.
HashMap Traversing Techinques

Consider an Example :

In real time most of the cases we are using generics with Collection classes and interface, so its better to write the code in Collections with Generics to impress the interviewer.

HashMap object creation along with Generics :

HashMap<String, String> demo=new HashMap<String,String>();
demo.put(”Michael”, “USA”);
demo.put(“Jaffar”,”Afghan”);

1) Iterating Map Object elements using the foreach loop :

From JDK 5.0 Version so many updating are included in it, in which one of the updation is foreach loop for iterating over map by using KeySet of Map interface to get the keys. This will used to iterate the values of Map and display the key and value in pair.

For Example:

HashMap<String, String> demo=new HashMap<String,String>();
demo.put(”Michael”, “USA”);
demo.put(“Jaffar”,”Afghan”);
for(String key : dmeo.keySet()){
System.out.println(key+”  ”+demo.get(key));
}

Output :

Iterating or Traversing the Map Using Java5 foreach loop
Michael USA
Jaffar Afghan

2) Iterating HashMap, Map, TreeMap in java using KeySet Iterator:

  To Retrieve the HashMap, Map, TreeMap by using Iterator 

For Example:
Set<String> keyset=demo.keySet();
Iterator<String> keysetIterator=keyset.iterator();
While(keysetIterator.hasNext()){
String  key=keysetIterator.hasNext();// stores Key “Michael”,”Jaffar”
System.out.println(key+”  “+demo.get(key));// USA, Afghan
}

Output :
Iterating or Traversing the Map Using Java5 Iterator
Michael USA
Jaffar Afghan

3) Retrieving or Traversing the HashMap in java using EntrySet along with forLoop :

From this Example we are retrieving the data using EntrySet along with foreachloop. EntrySet is collection of all Map Entries which contains Key and Value.

For Example :

Set<Map.Entry<String<String>> entryset-demo.entrySet();
For(Entry entry : entrySet()){
System.out.println(entry.getKey() +” “ +entry.getValue());
}

Output :

Iterating or Traversing the Map Using entrySet
Michael USA
Jaffar Afghan

4) Iterating HashMap in java using EntrySet and Iterator :

Retrieving the data from the HashMap object using EntrySet and Itertor.

For Example :

 Set<Map,Entry<String,String>> entrySet1=demo.entrySet();
 Itertor<Entyr<String, String>> entrySetIterator=entrySet1.iterator();
 While(entrySetIterator.hasNext()){
 Entry entry=entrySetIterator.next();
 System.out.println(entry.getKey()+”  ”+entry.getValue());
 }

Output:

Iterating or Traversing the Map Using Java5 Iterator & EntrySet
Michael USA
Jaffar Afghan

Difference Between ArrayList Vs LinkedList in Java|ArrayList and LinkedList Differences

ArrayList Vs LinkedList, LinkedList Vs ArrayList in java, Java ArrayList Linkedlist, Difference between ArrayList and LinkedList, difference between linkedlist and arraylist, Difference between Arraylist and Linkedlist with Example. arraylist and linkedlist difference in java,diff arraylist and linkedlist in java,compare arraylist and linkedlist in java,arraylist and linked list differences


ArrayList Vs LinkedList

In Java Collection Frame work Difference between LinkedList and ArrayList is one most Frequently Asked question in Java interview for Freshers,2+,3+,5+,6+ Experience also. Candidate’s who are going to attend for the Interview must be perfect in Core java and Collection Frame Work. In this Collection Frame Work we explained the Difference ArrayList  and  LinkedList is most frequently asked in the Interview, So Candidate must be Prefect in this Sections.


ArrayList Vs LinkedList


LinkedList Vs ArrayList

LinkedList and ArrayList both of them are classes that implements List Interface, But Most of them don’t how the LinkedList and ArrayList work internally in different manner. From Difference between ArrayList and LinkedList in which ArrayList is implemented by resizable array and LinkedList is implemented by doubleLinkedList. From this tutorial you can find the how LinkedList and ArrayList are use in realtime that shows in the difference between LinkedList and ArrayList.

1)    Array is data structure is used to store the data and delete the data is on the basis of the index. That makes faster in adding the element to the array. But Delete an element from the array makes to costly process, after deletion of the element from the array need’s to rearrange all elements. To Retrieve any element from the LinkedList does not iterate on the basis of the index and Random

2)    Performance of the ArrayList is provides O(1) performance for retrieving the element, For LinkedList retrieve Order of O(n).

3)    Insertion Operation are very easy and fast in LinkedList while comparing with ArrayList, there is a risk of resizing the array. ArrayList is need to update its index while adding any element to the ArrayList not at end of the Array. Removal of the element from the ArrayList is similar to Insertion in Arraylist, So LinkedList is better to use.

4)    LinkedList requires much more memory compared to ArrayList, because ArrayList holds the only index data, but in LinkedList it necessary to hold address  of both Previous node  data and next node data.

In Real Time Scenarios Where to Use LinkedList and ArrayList :

LinkedList is not popular when compared to ArrayList, But Linked list is the best option to use in java when Compared with ArrayList.

1)    In Web application or any application always requires to use Random access, if we want to access the nth element in the LinkedList. It’s easy to use.

2)    If Application has more insertion and deletion operations, then it’s  better to use ArrayList, if no Resizing of Array.

Sunday, 4 August 2013

HashMap Vs HashTable|Difference between HashMap and HashTable with Realtime Example

Difference between Hashmap and Hashtable, HashMap Vs HashTable, HashMap Realtime Example, HashTable Realtime Example, Difference between HashMap and Hashtable with Example, HashTable Vs HashMap, Difference HashMap and HashTable with Realtime Examples.



HashMap Vs HashTable ? Is it Possible to make HashMap synchronized ?

HashTable Vs HashMap
  In Java Collection Frame work Difference between HashMap and HashTable is one most  Frequently Asked question in Java interview for Freshers,2+,3+,5+,6+ Experience also.  Candidate’s who are going to attend for the Interview must be perfect in Core java and Collection Frame Work. In this Collection Frame Work we explained the Difference HashMap and  HashTable is most often asked in the Interview, So Candidate must be Prefect in this Sections. 
   
Difference Between HashMap and Hashtable in Java
 Hashtable and HashMap both of them implements Map Interface, But there are some difference between them, that helps to decide whether to Hashtable or HashMap in Java.
    1)    HashMap is Similar to Hashtable, but its not Sychronized and allows null values.             HashMap represented as key value pair in which both Key, Value allows null value.
    2)    One of the main Difference between HashMap and Hashtable, HashMap is not synchronized whereas Hashtable is synchronized. Hashtable is thread safe and             hashtable is not threadsafe(allows multiple threads to share a resource or object).
    3)    From Java 5 Version introduces the ConcurrentHashMap which is an alternative          for HashMap and has better performance than Hashtable in java.
    4)    Another Major Significant Difference is Iterator in HashMap is fail and fast safe         iterator, While enumerator is used for retrieving the data in thread safe manner, that throws ConcurrentModificationException, when any another is trying to modify the data in map.
    5)    Another final Difference between Hashtable and HashMap in which due to thread safe and synchronization Hashtable is very slow compare to HashMap because of single threaded approach.
Synchronized.
 Description for New Terms
 1)   Sychronization is process that allows one thread to access the Hashtable, It means first of all it holds the lock on the object of Hashtable while any thread is going to access the Hashtable.
2)   Another Term is Fail-safe is related to Iterators. If an Iterator or ListIterator is created on the Collection Object. If any thread tries to modify the that Collection Object then it throws ConcurrentModfificationException.
HashMap Sychronization Using Collections Method
In Java Collection HashMap Can be Sychronized by using Collections sychronizedMap(HashMap) method. 
3)   HashMap can be Sychronized by using Collections Class Method (i.e)  synchronizedMap(HashMap).
Ex:    Map m=Collections.synchronizedMap(HashMap);

LinkWithin