10 Example of find command in Unix and Linux

The find command is one of the versatile commands in UNIX and Linux  and I used it a lot in my day to day work. I believe having good knowledge of find command in UNIX and understanding of its different options and usage will increase your productivity a lot in UNIX based operating system e.g. Redhat Linux or Solaris. If you are a QA, support personnel, and your works involve lots of searching text on Linux machine or if you are a Java or C++ programmer and your code resides in UNIX, find command can greatly help you to look for any word inside your source file in the absence of an IDE. It is the alternative way of searching things in UNIX, grep is another Linux command which provides similar functionality like find but in my opinion later is much more powerful than grep in UNIX.

9 difference between Array vs ArrayList in Java

Both array and ArrayList are two important data structure in Java and frequently used in Java programs. Even though ArrayList is internally backed by an array, knowing the difference between an array and an ArrayList in Java is critical for becoming a good Java developer. If you know the similarity and differences, you can judiciously decide when to use an array over an AraryList or vice-versa. In this article, I'll help you understand the difference between ArrayList and array in Java. If you are coming from C or C++ background then you already know that array is one of the most useful data structure in the programming world. It offers O(1) performance for index-based search and one of the fundamental way to store data.

How to declare and initialize a List (ArrayList and LinkedList) with values in Java

Initializing a list while declaring is very convenient for quick use, but unfortunately, Java doesn't provide any programming constructs e.g. collection literals, but there is a trick which allows you to declare and initialize a List at the same time. This trick is also known as initializing List with values. If you have been using Java programming language for quite some time then you must be familiar with syntax of array in Java and how to initialize an array in the same line while declaring it as shown below:

String[] oldValues = new String[] {"list" , "set" , "map"};

or even shorter :

String[] values = {"abc","bcd", "def"};

Similarly, we can also create List  and initialize it at the same line, popularly known as initializing List in one line example. Arrays.asList() is used for that purpose which returns a fixed size List backed by Array. By the way don’t confuse between Immutable or read only List which doesn’t allow any modification operation including set(index) which is permitted in fixed length List.Here is an example of creating and initializing List in one line:

3 ways to loop over Set or HashSet in Java? Examples

Since Set interface or HashSet class doesn't provide a get() method to retrieve elements, the only way to take out elements from a Set is to iterate over it by using Iterator, or loop over Set using advanced for loop of Java 5. You can get the iterator by calling the iterator() method of Set interface. This method returns an iterator over the elements in the sets but they are returned in no particular order, as Set doesn't guarantee any order. Though individual Set implementations e.g. LinkedHashSet or TreeSet can impose ordering and in such iterator will return elements on that order. You can only traverse in one direction using iterator i.e. from first to last elements, there is no backward traversing allowed as was the case with List interface and ListIterator. Similarly, if you use advanced for loop, then also you can traverse in only one direction.

Top 5 Hibernate Books for Java Developers - Best, Must read

Hibernate is one of the most popular, open source ORM (Object Relational Mapping) framework, which has now become a standard for developing persistence layer on Java enterprise application, along with JPA (Java Persistence API). I often receive  requests to suggest which book is best to learn to hibernate or recommendation about some good books on Spring and Hibernate. This motivates me to write this article about some of the best book on Hibernate, available in the market. Earlier I have shared some of the must-read books on Spring framework for Java developer, which is quite helpful for picking a book on Spring. Similar to Spring framework, experience in Hibernate is most sought after thing in Java JEE development roles.

10 examples of grep command in UNIX and Linux

grep command is one of the most frequently used UNIX command stands for "Global Regular Expression Print" like find, chmod or tar command in Unix. grep command in Unix operating system e.g. Linux, Solaris, BSD, Ubuntu or IBM AIX is used to search files for matching patterns, by using grep command in Unix you can search a file which contains a particular word or particular pattern. UNIX grep command also provides several useful command line option which can be used to enhance the functionality of grep command e.g. by using grep -v you can list down all files which don't contain a word i.e. excluding files which matches a pattern, grep -c will print count of matching pattern in a file etc. One of the popular examples of grep command is to find empty files and directories in Unix.

5 books to learn Spring framework and Spring MVC for Java Programmers

Spring and Spring MVC is one of the most popular Java frameworks and most of new Java projects uses Spring these days. Java programmer often asks questions like which books is good to learn Spring MVC or What is the best book to learn Spring framework etc. Actually, there are many books to learn Spring and Spring MVC but only certain books can be considered good because of  their content, examples or the way they explained concept involved in Spring framework. Similar to  Top 5 books on Java programming we will some good books on Spring in this article, which not only help beginners to start with Spring but also teaches some best practices.

Solving java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet in Java Spring MVC Application

The  java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet error comes when you deploy a Spring MVC application into Tomcat or Jetty and Servlet container not able to found this class, which usually found in the spring-webmvc.jar file. The DispatcherServlet is the core of Spring MVC framework, it implements the FrontController pattern and intercept all HTTP requests sent to your Web application and subsequently route it to correct controller based upon URL mapping. You need to declare the DispatcherServlet in your web.xml and specify the URL pattern such that Servlet container like Tomcat should send all HTTP request to DispatcherServlet. We also set load-on-startup tag as 1 for this Servlet, so that it should be loaded at deployment time. This Servlet is the link between Servlet Container and Spring MVC framework.

Why JPA Entity or Hibernate Persistence Class Should Not be Final?

One of the interesting hibernate interview questions is, why you should not make a Hibernate persistent class final? I'll try to answer this question in this short blog post. Use of proxies is the core feature of Hibernate (one of the most popular ORM framework for Java Projects) for implementing key performance features e.g. lazy loading and lazy associations fetching. In order to use a proxy in place of real class, your hibernate persistence class must be either non-final or the implementation of an interface that declares all of the public methods. Why? because you cannot extend a final class in Java, and to stand up as a proxy, proxy class must satisfy the IS-A relation, which comes either by extending a class using "extends", or implementing an interface using "implements".

4 Ways to find Nth highest salary in SQL - Oracle, MSSQL and MySQL

One of the most common SQL interview questions is to find the Nth highest salary of employee, where N could be 2, 3, 4 or anything e.g. find the second highest salary in SQL. Sometimes this question is also twisted as to find the nth minimum salary in SQL. Since many Programmers only know the easy way to solve this problem e.g. by using SQL IN clause, which doesn't scale well, they struggle to write the SQL query when Interviewer keep asking about 4th highest, 5th highest and so on. In order to solve this problem effectively, you need to know about some key concepts like correlated subquery, window functions like ROW_NUMER(), RANK() and DENSE_RANK() etc. Once you know the generic logic to solve this problem, you can tackle all those variations by yourself.

Top 20 Amazon and Google Programming Interview Questions

Hello, In this article I am going to sharing some frequently asked programming job interviews from technical giants and startups. If you are going for a programming job interview with Microsoft, Google or Amazon, you better be prepared for  all kinds of questions. These companies are known to ask tough questions, tricky puzzles and lots of data structure and algorithm questions. Since it's hard to prepare all these in a short time, it makes sense to at least have a good idea of frequently asked programming questions in Microsoft, Google or Amazon. There are a lot of questions spread across the internet and if you are good in googling (not to mention, an important surviving skill in programming job), you can get tons of questions asked in these companies, or you can read Cracking the coding interview book, which contains 150+ programming questions and their solutions.

How to fix java.net.SocketException: Software caused connection abort: recv failed

Out of many client servers related socket errors here is one more interesting socket related error from Java program, "java.net.SocketException: Software caused connection abort: recv failed". The key point in this error message is "abort" and "recv", which means is someone (client or server) is trying to read from a closed connection.This error usually comes at the client socket end, when server closed the connection before the client has read the response, but, in general, it can come to any end of TCP socket, so you must check the log files for both client and server to find out who is complaining. If Server is complaining then it's fine and the client has closed the TCP connection may be due to timeout or any RuntimeException at the client end.

Reading/Writing to/from Files using FileChannel and ByteBuffer in Java

In the past, I have talked about RandomAccessFile and how it can be used for doing faster IO in Java, and  in this Java NIO tutorial, we are going to see how to use read/write data from using FileChannel and ByteBuffer. Channel provides an alternate way to read data from a file, it provides better performance than InputStream or OutputStream. It can also be opened in blocking and non-blocking mode. Though FileChannles are read/write channels and they are always blocking, they cannot be put into non-blocking mode. The RandomAccessFile class treats a file as an array of Bytes. You can write your data in any position of the Array and you can read from any position. To do that, it uses a pointer that holds the current position and provides several methods like seek() to move that pointer.

How does Java HashMap or LinkedHahsMap handles collisions?

Prior to Java 8, HashMap and all other hash table based Map implementation classes in Java handle collision by chaining, i.e. they use linked list to store map entries which ended in the same bucket due to a collision. If a key end up in same bucket location where an entry is already stored then this entry is just added at the head of the linked list there. In the worst case this degrades the performance of the get() method of HashMap to O(n) from O(1). In order to address this issue in the case of frequent HashMap collisions, Java8 has started using a balanced tree instead of linked list for storing collided entries. This also means that in the worst case you will get a performance boost from O(n) to O(log n).

10 Examples of using ArrayList in Java - Tutorial

ArrayList in Java is most frequently used collection class after HashMap in Java. Java ArrayList represents an automatic re-sizeable array and used in place of the array. Since we can not modify the size of an array after creating it, we prefer to use ArrayList in Java which re-size itself automatically once it gets full. ArrayList in Java implements List interface and allow null. Java ArrayList also maintains insertion order of elements and allows duplicates opposite to any Set implementation which doesn't allow duplicates. ArrayList supports both Iterator and ListIterator for iteration but it’s recommended to use ListIterator as it allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the Iterator's current position in the list. But while using ListIterator you need to be little careful because ListIterator has no current element; its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next(). In this Java ArrayList tutorial, we will see how to create Java ArrayList and perform various operations on Java ArrayList. This collection class is also favorited on many core Java interviews with questions like Difference between ArrayList and Vector  or LinkedList vs ArrayList.

How to check if String is not null and empty in Java?

In Java, since null and empty are two different concept, it's a little bit tricky for beginners to check if a String is both not null and not empty. A String reference variable points to null if it has not been initialized and an empty String is a String without any character or a String of zero length. Remember, a String with just whitespace may not be considered as empty String by one program but considered as empty String by others, so, depending upon your situation, you can include the logic to check for that as well. A String with just white space is also referred as blank String in java. In this tutorial, I will teach you a couple of right ways to test if a String is not null and not empty in Java.

5 Resolutions for Java Programmers - 2016

First of all wish you very happy new year guys. It's that time of year when we start afresh, make plans, set goals and make resolutions for the new year. Being a Java developer and author of a Java blog, I frequently receive a request from Java programmers from all over the world about how they can improve themselves? How they can become a better Java developer etc. In the past, I have shared my 10 tips to become a better programmer and every tip on that article still holds true, but given it's a new year. I have decided to jot down 5 new resolutions for Java programmers to improve themselves, enhance their knowledge on Java Programming and increase their value for future career growth. So without wasting anymore time, let's see what are 5 resolutions for Java Programmers in the new year, 2016.

SQL EXISTS Example - Customers Who Never Order LeetCode Solution

Good morning Guys, Today we will learn how to use the EXISTS clause in SQL by picking up a nice SQL exercise from LeetCode. Suppose that a website contains two tables, the Customers table and the Orders table. Write an SQL query to find all customers who have never ordered anything. This problem can be solved from different way but it is also a very good example of how you can use the SQL EXISTS clause. If any customer has ordered anything then their CustomerId will exist in Order table, we will take advantage of this knowledge to solve this problem. This question is not as popular as the second largest salary query, but it is also one of the frequently asked SQL queries from programming interview, you can see the full list here.

java.net.SocketException: Failed to read from SocketChannel: Connection reset by peer

You might have seen the java.net.SocketException: Failed to read from SocketChannel: Connection reset by peer error while working with Java NIO based server which is using SocketChannel instead of InputStream for reading data from the network. In general, this can come at both client and server end of a client-server Java application which is using TCP/IP to connect each other. Though, this exception usually comes at the server end and client was complaining that they are not able to connect. From the error message, it's quite clear that before client or server could read the data from SocketChannel, another party has disconnected the session. Let's see the root cause of the problem and how to solve java.net.SocketException: Failed to read from SocketChannel: Connection reset by a peer in Java application.