Difference between EnumMap and HashMap in Java

HashMap vs EnumMap in Java
What is difference between EnumMap and HashMap in Java is the latest Java collection interview question which has been asked to couple of my friends. This is one of the tricky Java question, specially if you are not very much familiar with EnumMap in Java, which is not uncommon, given you can use it with only Enum keys. Main difference between EnumMap and HashMap is that EnumMap is a specialized Map implementation exclusively for Enum as key. Using Enum as key, allows to do some implementation level optimization for high performance which is generally not possible with other object's as key. We have seen lot of interview questions on HashMap in our article How HashMap works in Java but what we missed there is this question which is recently asked to some of my friend. Unlike HashMap, EnumMap is not applicable for every case but its best suited when you have Enum as key. We have already covered basics of EnumMap and some EnumMap example in my last article What is EnumMap in Java and In this post we will focus on key differences between HashMap and EnumMap in Java.

Difference between trustStore and keyStore in Java - SSL

trustStore vs keyStore in Java
trustStore and keyStore are used in context of setting up SSL connection in Java application between client and server. TrustStore and keyStore are very much similar in terms of construct and structure as both are managed by keytoolcommand and represented by KeyStore programatically but they often confused Java programmer both beginners and intermediate alike. Only difference between trustStore and keyStore is what they store and there purpose. In SSL handshake purpose of trustStore is to verify credentials and purpose of keyStore is to provide credential. keyStore in Java stores private key and certificates corresponding to there public keys and require if you are SSL Server or SSL requires client authentication. TrustStore stores certificates from third party, your Java application communicate or certificates signed by CA(certificate authorities like Verisign, Thawte, Geotrust or GoDaddy) which can be used to identify third party. This is second article on setting up SSL on Java program, In last post we have seen How to import SSL certificates into trustStore and keyStore and In this Java article we will some differences between keystore and truststore in Java, which will help to understand this concept better.

10 Tips to override toString() method in Java - ToStringBuilder Netbeans Eclipse

Java toString method
toString method in Java is used to provide clear and concise information about Object in human readable format. Correctly overridden toString method can help in logging and debugging of Java program by providing valuable and meaningful information. Since toString() is defined in java.lang.Object class and its default implementation doesn't provide much information, its always a best practice to override toString method in sub class. In fact if you are creating value class or domain class e.g. Order, Trade or Employee,  always override equals, ,hashCode, compareTo and toString method in Java.  By default toString implementation produce output in the form package.class@hashCode e.g. for our toString() example, Country class’ toString() method will print test.Country@18e2b22 where 18e2b22 is hashCode of object in hex format, if you call hashCode method it will return 26094370, which is decimal equivalent of 18e2b22. This information is not very useful while troubleshooting any problem. Let’s see a real life example where you are troubleshooting network connectivity issues, in case of this you want to know which host and port your system is trying to connect and if Socket or ServerSocket class only print default toString information than its impossible to figure out the actual problem, but with a decent toString implementation they can print useful information like hostname and port. In this Java  tutorial we will see some tips to override toString method with code examples.

How to determine Type of object at runtime in Java - Runtime Type Identification

Runtime Type identification in Java
Determining Type of object at runtime in Java means finding what kind of object it is. For those who are not familiar with What is a Type in Java,  Type is the name of class e..g for “abc” which is a String object, Type is String. Finding Type of any object at runtime is  also known as Runtime Type Identification in Java. Determining Type becomes increasingly important for method which accept parameter of type java.lang.Object like compareTo method of Comparable class. Since two objects of different type can not be equal to each other if we know how to determine type of Object from object itself than we can, not only avoid ClassCastExcpetion but also optimized the equals method. Java provides three different ways to find type of object at runtime e.g. instanceof keyword, getClass() and isInstance() method of java.lang.Class. Out of all three only getClass() is the one which exactly find Type of object while others also return true if Type of object is super type. As we all know that Java  throws ClassCastException if you try to cast object into wrong type, which makes finding type of Object at runtime even more important for robust Java programs, Though type casting can be minimized by using Generics in Java but you still have some places where you need to cast object. In this Java tutorial we will see examples three ways of determining type of Object from Java program itself. On same note Java programming language does not support RTTI(Runtime type Identification) which was C++ capability to find Object type at runtime but as I said Java has its own way of facilitating this.

What is EnumMap in Java – Example Tutorial

What is EnumMap in Java
EnumMap in Java is added on JDK  5 release along with other important features like  Autoboxing, varargs and Generics. EnumMap is specialized Map implementation designed and optimized for using Java Enum as key. Since enum can represent a type (like class or interface)  in Java and it can also override equals() and hashCode() , It can be used inside HashMap or any other collection but using  EnumMap brings implementation specific benefits which is done for enum keys, In short EnumMap is optimized Map implementation exclusively for enum keys . As per javadoc Enum is implemented using Arrays and common operations result in constant time. So if you are thinking of an high performance Map, EnumMap could be decent choice for enumeration data. We have already seen many examples of Java enum in our article 10 Examples of enum in Java  and using Enum as thread-safe Singleton. In this Java tutorial we will see simple examples of using EnumMap in Java. On related note Difference between EnumMap and HashMap is also getting popular as one of advanced Java collection interviews questions and most of Java IDE like Eclipse and Netbeans will also suggest to use EnumMap if you use Enum keys along with HashMap as part of there code improvement suggestion.

Difference between save vs persist and saveOrUpdate in Hibernate

Save vs saveOrUpdate vs persist in Hibernate
What is difference between save and saveOrUpdate or Difference between save and persist are common interview question in any Hibernate interview, much like Difference between get and load method in Hibernate. Hibernate Session class provides couple of ways to save object into database by methods like save, saveOrUpdate and persist.  You can use either save(),  saveOrUpdate() or persist() based upon your requirement for persisting object into Database. Along with Spring framework Interview questions, Hibernate questions are also quite popular on J2EE interviews because of its status as leading ORM. It’s good to prepare some questions from Hibernate before appearing in any J2EE interviews. One of them is Difference between save , saveOrUpdate and persist, which we will see in this Hibernate article.

How to replace escape XML special characters in Java String

How to replace XML special Characters in Java String
There are two approaches to replace XML or HTML special characters from Java String, First,  Write your own function to replace XML special characters or use any open source library which has already implemented it. Luckily there is one very common open source library which provides function to replace special characters from XML String is Apache commons lang’s StringEscapeUtils class which provide escaping for several  languages like XML, SQL and HTML. you can use StringEscapeUtils to convert XML special character in String to there escaped equivalent. I personally like to use open source code instead of reinventing the wheel to avoid any testing efforts. Even Joshua Bloach as advocated use of Open source library to leverage experience and work of other programers. If you are reading from XML file and after doing some transformation writing to another XML file , you need to take care of XML special characters present in source file. If you don’t escape XML special characters while creating XML document than various XML parsers like DOM and SAX parser will consider those XML meta consider them as XML tag in case of < or >. Even if you try to transform XML with special character using  XSLT transformation, it will complain and fail. So while generating XML documents its very important to escape XML special characters to avoid any parsing or transformation issues. In this Java XML tutorial we will see What is special characters in XML and how to escape XML characters from Java String.

Java program to find IP Address of localhost - Example Tutorial

How to find IP address of local host from Java program
Java networking API provides method to find IP address of localhost from Java program by using java.net. InetAddress class. It’s rare when you need IP address for localhost in Java program. Mostly I used Unix command to find IP address of localhost. For all practical purpose where program doesn’t need IP address but you need to troubleshoot any networking issues, Use DOS or windows command or Use Linux commands. Recently one of my friend faced this question in a core Java interview, where they are expecting Java developer with some socket programming experience, But until you know or you have done it before its hard to answer this fact based question, which motivates me to write this post. In this Java tutorial we will see How to find IP address of localhost from Java program. By the way it’s also good to remember list of Unix networking commands to troubleshoot any networking issues related to Java application in Unix environment.