Thursday, September 6, 2018

Java 8 Lambda Expressions with examples and Rules

Introduction:


Java 8 is first released in 2014 and introduced lot of new features. Lambda expressions are the most significant change in java 8 version.
As of java 7, Oracle/Sun people have given importance to the object oriented programming languages but in java 8, they have introduced functional programming to compete with other programming languages such as Scala, C# etc.




Java 8 Lambda Expressions with examples and Rules


What is Lambda Expression?


Any function which is having no name is called as Lambda expression. Which is also called as anonymous function.


Rules:


1) function should not have access modifier
2) Should not have any return type (even void also not allowed)
3) Should not have name for function
4) Should use arrow symbol "->"

We will see now a few examples how to convert normal java fucntions to lambda expression.

Wednesday, September 5, 2018

How to List All Tables, Describe in Oracle, MySQL, DB2 and PostgreSQL

 In this tutorial, We will learn how to connect, list all tables in Oracle, MySQL, DB2, PostgreSQL and describe tables in Oracle,  MySQL, DB2 and PostgreSQL.

oracle and mysql all_tables examples

show all tables MySQL, show all tables in oracle, show all tables Postgres, show all tables, show all tables in database SQL, show all tables SQLite, show all tables in a database, show all tables in Cassandra, show all tables in hive, show all tables in schema Postgres, show all tables SQL, show all tables and columns MySQL

 You often want to list all tables in a database or list columns in a table. Obviously, every database has its own syntax to list the tables and columns. Well, this post placed all most popular databases.

Friday, July 20, 2018

How To Sort ArrayList In Java – Collections.Sort() Examples

How To Sort ArrayList In Java :

In this post, we will learn how to sort ArrayList in java. This is very easy to understand and remember the methods.


1) Sorting ArrayList in ascending order
2) Sorting ArrayList in descending order



How To Sort ArrayList In Java – Collections.Sort



Sorting List in Ascending order:
package com.java.w3schools;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class FruitsArrayList {
      public static void main(String[] args) {
            List<String> fruits = new ArrayList<>();
            fruits.add("Mango");
            fruits.add("Orange");
            fruits.add("Apple");
            fruits.add("Banana");
            System.out.println("Printing fruits before sorting : " + fruits);
            Collections.sort(fruits);
            System.out.println("Printing fruits after sorting : " + fruits);
      }
}


Output:

Printing fruits before sorting : [Mango, Orange, Apple, Banana]
Printing fruits after sorting : [Apple, Banana, Mango, Orange]
Collections class has a method sort() which takes List as argument. This method does sorting in ascending order. Observer the output of the above program.

Sorting List in Descending order:

Just replace the Collection.sort with the following code snippet.
Collections.sort(fruits, Collections.reverseOrder());

Output:

Printing fruits before sorting : [Mango, Orange, Apple, Banana]
Printing fruits after sorting : [Orange, Mango, Banana, Apple]
Collections class has a method for sorting the list elements in reverse order that is descending order.
In the next post, we will learn how to sort a list of students where Student is a user defined class with properties of id, name, age.

Wednesday, December 6, 2017

Java ArrayList remove Example: How to remove by index, by Value/Object, for a specific range


How to remove a value from ArrayList in java with example programs.

In this post, we will learn how to program to remove elements from a ArrayList in java. Removing value can be done in three ways.

1) By index
2) By value or Object
3) For a given specific range

ArrayList api provides various methods to do remove operations.

java-arraylist-remove




Sunday, November 19, 2017

Encapsulation in Java with Examples

What is Encapsulation in Java 

Encapsulation is one of the principle of OOPs.

Encapsulation describes the ability of hiding data and methods of object.
Encapsulation is a process of arranging information about data and behavior into a single component like as object in Java.

Keeping all members and methods are within bounds of class. In Java, all class are under encapsulated.

If we set all fields of class as private, so no other code outside of this class can not access private variables. To access these private variables, we have to declare public methods in class.


So these private members are not accessible by outside objects or classes.

A class can have authority to modify the values using setter and getter methods of class. By using getter methods, we can make read-only and wise versa for write-only.

Wednesday, November 15, 2017

3 Ways to Find Sum of First n numbers program in Java

In this post, You will learn today today how to find sum of first n numbers in java using while loop, for loop and mathematics formula(Optimized).

For this program, we have to input the natural number n value which you want to get the sum. So that Our program will get sum of first n numbers. This is basic program for freshers or who are studying engineering in their lab programs.



Example 1:
number = 10
sum = 55

Example 2:
number = 100
sum = 5050

Friday, November 10, 2017

Java Supports Pass By Value only, not Pass By Ref

Does Java supports Pass By Value or Pass By Reference:

 

Very common question in java interviews for freshers and experienced. But, this is very tricky and confusion for most of the people whether java is pass by value or pass by reference.

First, we will make clear what is call by value and call by reference. These two comes into play when we pass parameters in invoking a method.

java-pass-by-value




Call By Value: Values only passed as copy of original. But not original memory location. It creates a new copy in the memory and passes to the method that we invoked.

Call By Reference: Passes reference or alias of the object which is said to be Call By Reference. Unfortunately, Java inventors decided to call the location of an object as a "reference". When we pass the value of an object, we are passing the reference to it. This is confusing to beginners.