Monday, October 30, 2017

Java Program to Bubble Sort(Sinking Sort)

Bubble Sort in Java | Sinking Sort Technique

Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The algorithm, which is a comparison sort, is named for the way smaller or larger elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort. It can be practical if the input is usually in sorted order but may occasionally have some out-of-order elements nearly in position.

1) Easy to understand.
2) Easy to implement.
3) In-place, no external memory is needed.
4) Performs greatly when the array is almost sorted (This is part of Optimized Bubble Sort).
5) Widely used by students to clear practical exams or freshers interview process as the end result is the same as that of any sorting technique


Bubble Sort in Java

Sunday, October 29, 2017

4 Best Ways To Swap Two Numbers in Java without using temporary/Third variable?

In this tutorial, We will discuss how to swap two numbers in Java without using temporary variable.


This is very common interview question for freshers or 1-2 experience.

First, we will see how to swap using two numbers with using temporary variable then next without third variable.

Sum of  Two Numbers in Java

4 Best Ways To Swap Two Numbers in Java without using temporary variable?

Thursday, October 26, 2017

4 Different ways of Reading a text file in Java (CSV Files)

In this post, We will learn very basic program How to read a file in Java. Very useful scenario which is prominently used in our real time projects.
How to read csv file in java. This is a common use case.

java.io package has utility classes for reading and writing files.

We can do reading a file in java 4 ways.

1) Using BufferedReader
2) Using JDK 7 try-with-resources
3) Using Files.readAllBytes
4) Using Files.readAllLines

4 Ways How to read a file in Java, CSV Example Programs

First, Create a csv file with the following data in your computer(File name : employee.csv).

100,Jhon,20
101,Cena,24
102,Mike,19

Wednesday, October 25, 2017

Best Ways to Java Generic Naming Convention, Rules, Examples

In this post, We will learn what are the naming conventions rules for Generic type parameters. Many people might have seen that Generic types are heavily used in Java Collection api such as ArrayList, HashSet, HashMap.

Best Ways to Java Generic Naming Convention, Rules, Examples

Generics types are should be enclosed in Diamond operator.

Wednesday, October 18, 2017

Java Type Inference in Generics

Type Inference in Generic

In Java 7, Type Inference is introduced in generics declaration which is used to reduce the coding as well to minimize the compile errors.

Made compiler smart enough to infer the type of generic instance. This is done by using "<>" operator and it is called as Diamond Operator. Not required to mention the type in between diamond operator and can be replaced declaring the type as left blank.

Java Type Inference in Generics

Monday, October 16, 2017

How to convert a String to an int in Java?

We will learn today, How to convert String to Int in java. This is very popular written test question in Java interviews.

For example, We have a String "12345" and now I want to represent it in integer.

We can do conversion in 4 ways.

1) Integer.parseInt method.
2) Integer.valueOf
3) Integer constructor
4) DecimalFormat class

string to int

Add ArrayList to another ArrayList in Java | addAll method

In this post, We will learn How to add ArrayList into a another ArrayList in java.

We can accumulate this in two ways.

1) While creating ArrayList object.
2) Using addAll method.


Add ArrayList to another ArrayList in Java


1) Adding existing ArrayList into new list:

ArrayList has a constructor which takes list as input. Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

ArrayList(Collection c)