Friday, 11 July 2014

Differences between Hashtable and HashMap

0 comments
Before we actually see differences,let me give you brief introduction of both.

HashMap
HashMap implements Map interface which maps key to value.It is not synchronized and is not thread safe.Duplicate keys are not allowed and null keys as well as value are allowed. 
HashMap employeeHashmap=new HashMap();  
employeeHashmap.put(1,"Harit");  
employeeHashmap.put(2,null);  // will work fine 
Hashtable Hashtable implements Map interface which maps key to value.It is synchronized and thread safe.Duplicate keys are not allowed and null key is not allowed.
Hashtable employeeHashmap=new Hashtable();  
employeeHashmap.put(1,"Harit");  
employeeHashmap.put(2,null);  //not allowed and will throw NullPointer exception at run time


Hashtable vs HashMap:

Parameter
Hashtable
HashMap
ThreadSafe
Yes
No
Synchronized
Yes
No
Performance
Due to theadSafe and Synchronized,it is often slower than HashMap
In single threaded environment, it is much faster than Hashtable.So if you do not work in multi thread environment ,then hashMap is recommended
Null key
Do not allow
Allows null key as well as values
Fail fast
enumeration in hashtable is not fail fast
Iterator in hashMap is fail fast
Extends
It extends Dictionary class which is quite old
It extends AbstractMap class
Alternative
No alternative
You can use ConcurrentHashMap for multi thread environment

Some important points need to be discussed. 
  • Synchonized meaning only one thread can modify one table  at one point of time.When any thread perform update operation on hashtable then it acquires lock on it and other threads have to wait for lock to be released.
  • Fail-fast iterator means if one thread is iterating over hashmap and other thread trying to modify hashmap structurally it will throw ConcurrentModification Exception and fail immediately.Structurally modification means inserting or deleting elements that can change structure of map.

Can we synchronize HashMap?

Yes,We can synchonized a HashMap also with the help of Collections.synchonizedMap(hashmap) so HashMap can be synchronized by

Map map=Collections.synchonizedMap(hashmap)  

Thursday, 12 June 2014

Differences among Java EE and Java SE and Java ME

0 comments
Java technology is both a programming language and a platform. The Java programming language is a high-level object-oriented language that has a particular syntax and style. A Java platform is a particular environment in which Java programming language applications run.
There are several Java platforms. Many developers, even long-time Java programming language developers, do not understand how the different platforms relate to each other.

2.1.1 The Java Programming Language Platforms

There are three platforms of the Java programming language:
  • Java Platform, Standard Edition ( Java SE)
  • Java Platform, Enterprise Edition ( Java EE)
  • Java Platform, Micro Edition (Java ME)
All Java platforms consist of a Java Virtual Machine (VM) and an application programming interface (API). The Java Virtual Machine is a program, for a particular hardware and software platform, that runs Java technology applications. An API is a collection of software components that you can use to create other software components or applications. Each Java platform provides a virtual machine and an API, and this allows applications written for that platform to run on any compatible system with all the advantages of the Java programming language: platform-independence, power, stability, ease-of-development, and security.

2.1.1.1 Java SE

When most people think of the Java programming language, they think of the Java SE API. Java SE's API provides the core functionality of the Java programming language. It defines everything from the basic types and objects of the Java programming language to high-level classes that are used for networking, security, database access, graphical user interface (GUI) development, and XML parsing.
In addition to the core API, the Java SE platform consists of a virtual machine, development tools, deployment technologies, and other class libraries and toolkits commonly used in Java technology applications.
JavaFX technology, a part of the Java SE platform, is a client technology for creating rich internet applications using a lightweight user-interface API. JavaFX applications use hardware-accelerated graphics and media engines to take advantage of higher-performance clients and a modern look-and-feel as well as high-level APIs for connecting to networked data sources. JavaFX applications may be clients of Java EE platform services.

2.1.1.2 Java EE

The Java EE platform is built on top of the Java SE platform. The Java EE platform provides an API and runtime environment for developing and running large-scale, multi-tiered, scalable, reliable, and secure network applications.

2.1.1.3 Java ME

The Java ME platform provides an API and a small-footprint virtual machine for running Java programming language applications on small devices, like mobile phones. The API is a subset of the Java SE API, along with special class libraries useful for small device application development. Java ME applications are often clients of Java EE platform services.

Source: Oracle Doc

Monday, 19 May 2014

Ant Build OutOfMemory issue

0 comments
If 'javac' task is used with fork option then , javac will run in his own process and memory should be directly assigned to the javac using 'memoryinitialsize' and 'memorymaximumsize':
javac   srcdir="${src}"
            destdir="${classes}"
            source="1.6"
            debug="true"
            fork="true"
            deprecation="off"
            memoryinitialsize="512m"
            memorymaximumsize="1024m"

Related Posts Plugin for WordPress, Blogger...