How to Convert Array to Set and List in Java with Example

In this example we will learn how to convert an String array to Collection, Set or List in Java. This knowledge of converting array to Collection can be really useful to any Java developer, as most of legacy code tend to use array, which means you either need to pass them your input as array or they return result as array. Since newer Java code prefer Collection over array, which they should, because of flexibility offered by Collection classes, we often need to convert Array into different Collection classes e.g. List, Set or simply Collection. I have shown couple of techniques for converting array to arraylist, which equally applicable, when it comes to convert Array to List in Java. In this article, we will go couple of steps further and not only learn converting array to List, but also array to Set and array to Collection in Java. Well, it's only one method, which you need to know, Arrays.asList(), which accepts an array and return a List, later you can convert this List into any other Collection, by using copy constructor provided by Collection classes.

Difference between Deep and Shallow Copy in Java Object Cloning

Shallow copy and deep copy is related with cloning process so before go into the deep of shallow and deep copy we need to understand what is clone in java. Clone is nothing but the process of copying one object to produce the exact object, which is not guaranteed. We all know in Java object is referred by reference we can not copy one object directly to another object. So we have cloning process to achieve this objective. Now one question arises in mind why we need this process so the answer is whenever we need a local copy of the object to modify the object in some method but not in method caller.  So we can define Cloning as “create a copy of object  .I think now we are somehow clear about the cloning but there  is more to it depending on how we are doing this copy, we can divide cloning into two types.
  • Shallow Copy
  • Deep Copy
Before going into the deep of shallow and deep copy we need to understand how we achieve cloning in java.

Reading input and password in Java using - Example tutorial

Java 6 added a new utility class for reading input data from character based devices including command line. can be used to read input from command line, but unfortunately, it doesn't work on most of the IDE like Eclipse and Netbeans. As per Javadoc call to System.Console() will return attached console to JVM if it has been started interactive command prompt or it will return null if JVM has been started using a background process or scheduler job. Anyway, not only provides a way to read input from command prompt or Console but also reading passwords from the console without echoing it. Console.readPassword() method reads password and returns a character array and password is masked during entering so that any peeping tom can not see your password while you are entering it.