Difference between EnumMap and HashMap in Java

HashMap vs EnumMap in Java
What is the difference between EnumMap and HashMap in Java is the latest Java collection interview question which has been asked to a couple of my friends? This is one of the tricky Java questions, especially if you are not very much familiar with EnumMap in Java, which is not uncommon, given you can use it with only Enum keys. The main difference between EnumMap and HashMap is that EnumMap is a specialized Map implementation exclusively for Enum as key. Using Enum as key allows doing some implementation level optimization for high performance which is generally not possible with other objects as key. We have seen a 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 friends. Unlike HashMap, EnumMap is not applicable for every case but it's 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. A 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 don't provide much information, it's always a best practice to override the 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 produces 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 an 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. 

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 a method which accepts parameter of type java.lang.An object like compareTo method of the Comparable class. Since two objects of different type can not be equal to each other if we know how to determine the type of Object from the object itself then we can, not only avoid ClassCastExcpetion but also optimized the equals method. Java provides three different ways to find the 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 the super type. 

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 are 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 a 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. 

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 a 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 their escaped equivalent. I personally like to use open source code instead of reinventing the wheel to avoid any testing efforts. Even Joshua Bloch as advocated use of Open source library to leverage the experience and work of other programmers. 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 the 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 >. 

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.