Tuesday, December 31, 2019

Java Servlet Session Timeout and Examples

1. Overview


In this tutorial, how to set up and configure a session timeout in a java Servlet based web application.

Configure Session Timeout in the web.xml of a Java Servlet web application, and globally for a Tomcat or Jetty Server.

This can be done in many ways as follows.

A) web.xml
B) Programmatically
C) Tomcat or any server configuration


Java Servlet Session Timeout


Monday, December 30, 2019

Java 8 Method References With Examples

1. Overview


In this tutorial, We'll learn how to use Method References in Java 8 and where we can use Method References in the programs. To understand this article, you should have a minimal understanding of Lambda Expressions.

Read: Introduction to Lambda Expressions in Java 8

Method References are part of the new Java 8 concept and used to refer to the functional method of Functional Interface. Syntax is ClassName::methodName. Method ref is indicated with "::" - double colon operator. This is a compact and simple form of the lambda expression. If you are using a lambda expression to refer only functional method and not any additional logic then it is best to use method references in these kinds of scenarios.

We'll learn types of method references and their example programs.

Java 8 Method References With Examples


Sunday, December 29, 2019

Java 8 Stream mapToInt(ToIntFunction) Method Examples | Convert Stream to IntStream

1. Overview


In this java 8 Streams series, We'll learn today what is Stream API mapToInt() method and when to use this. This method is used to convert the Stream to IntStream. Let us see it's syntax and few examples on mapToInt() method. This is also part of intermediate operations such as map() and filter() methods.

Java 8 Stream mapToInt(ToIntFunction) Method Examples | Convert Stream to IntStream




Saturday, December 28, 2019

Java List or Traverse All Files in Folder Recursively (Java 8 Files.walk() Example)

1. Overview


In this programming tutorial, we are showing an example program on how to list or traverse all files and folders including subfolders in a directory using the following methods.

A) Using the classic approach
B) Using Java 8 methods

Traverse All Files in Folder Recursively


Java 12 Files mismatch Method Example to Compare two Files

Wednesday, December 25, 2019

What is the need for Default Methods inside Interfaces in Java 8

1. Overview


In this java 8 tutorial series, We'll be learning today why default methods are introduced in java 8 and why default methods are needed?


need for Default Methods inside Interfaces in Java 8

2. Interfaces Before Java 8 


Prior to Java 8, Every method present inside interface is always public and abstract whether we are declaring it or not.

package com.java.w3schools.blog.java.program.to.java8.defaultmethod;

public interface InterfacePriorJava8 {

 int method1();

 int method2();

}


Thursday, December 19, 2019

Java Program To Copy(Add) All List Values To A new ArrayList

1. Overview


In this tutorial, We'll be learning how to copy all list values to another new List or ArrayList in Java. Here our context copy means adding all values to a new List. how to copy list values to another list in java or Copy a List to Another List in Java can be done in many ways in java.
And We will keep tracking and show all the possible errors that encounter in our practice programs. Please read the entire article to get a better deeper understanding.

We have already written an article on ArrayList API Methods and its examples.

Java Program To Copy(Add) All List Values To A new ArrayList


We will be showing the example programs using constructor, addAll method, Using Java 8 and Java 10 concepts finally using Collections.copy() method. Hope this article will be interesting for you.

Note: ArrayList is not an Immutable object.

Tuesday, December 17, 2019

Java Program to Convert Int to String (Best Way)

1. Overview


In this conversion series articles, we will learn today how to convert int to String in java. Previously, I explained converting String to Int in java.

We do not talk much about unnecessary things. Directly jumping into our topic.

int conversion into String can be done using following java builtin methods.

String.valueOf()
Integer.toString()
String.format() 
Using + operator

Java Program to Convert Int to String


Monday, December 16, 2019

Java 8 Program To Boxed Stream Operations

1. Overview


In this tutorial, We'll learn how to convert primitives to wrapper objects in java 8. Java programs to convert a stream of int, long, double values into Collection using boxed() method.

This boxed() method is present in IntStream, LongStream, and DoubleStream primitive specialization streams.

boxed() method returns a Stream consisting of the elements of this stream, each boxed to a Wrapper.


Java 8 Program To Boxed Stream Operations

Friday, December 13, 2019

Variables Dynamic Initialization in Java

1. Overview


In this tutorial, We will be discussing variables dynamic initialization topic with example programs in java. Most of the developers do this but do not realize this is said to be dynamic initialization. In the previous tutorial, we discussed in-depth on variables in java.  If you are an experienced developer, continue reading this article. Otherwise, please go through this to get a complete understanding. Accessing Local variables from lambda is quite different and that will cause compile-time errors if you are using variables inside lambda expressions.


If any variable is not assigned with value at compile-time and assigned at run time is called dynamic initialization of a variable. Basically, this is achieved through constructors, setter methods, normal methods and builtin api methods which returns a value or object.

Variables Dynamic Initialization in Java

Thursday, December 12, 2019

Java Program To Add All Numbers In A String (Consecutive Digits Considered As One Number)

1. Introduction


In this tutorial, We will learn how to write a java program to add all numbers in a string. Here the ∫. Already shown a program on how to sum all digits in the string (in this case each digit considered as one number. In other words, taking into consideration each individual digit).


Examples of samples:

Exmaple 1:

Input 1: java10program2
Output 1: 12

Exmaple 2:

Input 2: hello100world200
Output 2: 300

Exmaple 3:

Input 3: nodigits
Output 3: 0


Java Program To Add All Numbers In A String

Wednesday, December 11, 2019

Java Program to "Run Length Encoding (RLE)" - String Compression

1. Introduction


In today's article, You will learn What is the "Run Length Encoding" technique which is heavily used in string data compression. This is an experienced java interview question for 4-6 years developers. Earlier days this is used to compress the black and white photos.


Run Length Encoding is abbreviated as "RLE".

Run-length encoding (RLE) is a form of lossless data compression in which runs/flows of data are stored as a single data value and count, rather than as the original run.
Java Program to "Run Length Encoding (RLE)" - String Compression



This works for only sequences in which the same data value occurs in many consecutive data elements.

Examples:

Input: aaabbccccd
Output:a3b2c4d1

Input: xxxyyziiii
Output: x3y2z1i4

Input: kkkkkkiiiiiiuuuuasdfgllll
Output: k6i6u4a1s1d1f1l4

All of the above examples are valid. If the string "aaabba" is invalid because character 'a' is repeated in non-consecutive which is appeared after character 'b'. If the string is invalid then it may produce unexpected results. Please keep in mind this note before implementing it in real-time applications.

Thursday, December 5, 2019

Java Program to Send An Email with Plain Text

1. Overview

In this email sending programming series, You will be learning today how to send an email to a specified email address from a default mail address. Content can be anything in the email. Basically, sending emails is a very common feature in the IT industry. After performing a successful case completion or if an error occurred an email should be triggered. And also summarizing the stats for the day and triggering the email at the end of the day. Any scenario is good choice to send an email. It is absolutely based on the need.

Mostly in all banking companies will be running thousands of control-m jobs that need a notification after completion of each job. control-m triggers a shell script or java code and executes all business logic and in the end, it will send the status of the job in the email. If it is not sending the job status to the right person then no action is taken if the job is failed.

Java Program to Send An Email with Plain Text

Java Program to Print or Display Floyd’s Triangle With Example Output

1. Overview


In this programming series tutorial, You'll learn how to create and print Floyd's triangle in java today.  Printing the consecutive numbers in order and right-angled triangle as below is said to be Floyd's Triangle.


1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Formula to find the total number of elements in the Floyds triangle is n * (n+1) / 2 where n is the input that how many lines should be printed in the output. The same formula can be used to calculate the sum of n natural numbers also.

Java Program to Print or Display Floyd’s Triangle With Example Output


Refer: Find Sum of N Natural Numbers Using Arthimetic Formulae.

Wednesday, December 4, 2019

Java Program To Convert String to ArrayList Using Arrays.asList()

1. Overview


In this ArrayList articles series, You'll be learning today how to convert a String into an ArrayList using java built-in methods. There are many scenarios in the real-time applications where we need to do transform the string into a list of objects. Typically string should be multiple values separated by a delimiter. For example take a String like "Started,Approved,In Progress,Completed". Here we are seeing the statues in string format and all these statuses are for processing the tickets raised by the users. This is the status flow. We need to now convert this string into a List<String>.


Java Program To Convert String to ArrayList Using Arrays.asList()


Note: String can have integers, double and strings values with a delimiter. The same below shown program works for any type of values present in the input string.

Java Program to Add Two Complex Numbers

1. Overview


In this w3schools programming tutorial, You'll learn a java program to add two complex numbers. Complex number is an A + iB. Every complex equation will have two parts - real and imaginary portions. When adding complex number we need to add separately both real numbers and separately both imaginary numbers.

Here are the two complex number equations.

a1 + ib1, a2 + ib2

(a1,b1) and (a2, b2)

This is also a fresher or 1-2 years of experience java developer interview question.