Pages

Showing posts with label Core Java. Show all posts
Showing posts with label Core Java. Show all posts

Thursday, September 5, 2024

Anonymous Comparator In Java For Sorting

1. Overview

In this tutorial, We'll learn how to sort arrays or lists using an anonymous comparator in java.

A complete guide on how to use comparator in java?

Anonymous class means creating the class and providing the implementation at the same without a class name.

For example, we have an interface Job and it has the method post() to post the new job. here, a new job1 instance is created for the implementation class of Job interface without a class name.

Example 1

interface Job {
	void post();
}

Job job1 = new Job() {

	@Override
	public void post() {
		System.out.println("Posting job 1 now");

}


This is similar to the anonymous implementation of the abstract class or interface. This is the actual implementation of the Comparator without creating a class and implementing the Comparator interface.

Sunday, September 1, 2024

Java Thread.join() Examples to Wait for Another Thread

1. Introduction


In this tutorial, We'll learn how to use Thread.join() method in java. And also how to join the multiple threads at one place after completing the execution of all threads or one thread or any other threads.

join() method is part of the Thread class and it is part of the java.lang package. All the classes in java.lang package is not needed to add import statements. So, directly you can use it in the programs. join() is mainly used to sleep or wait the current thread until completion of another thread execution.

Similar to the wait() and notify(), join() also widely used in the thread intercommunication.

2. Thread.join() Syntax


below is the syntax from Thread API. join() method is an overloaded method so we should be careful which method should be used.

public final void join() throws InterruptedException

public final void join(long millis) throws InterruptedException

public final void join(long millis, int nanos) throws InterruptedException


Java 8 String API also has join() method.

Wednesday, December 22, 2021

Java List or ArrayList Max Size (With Examples)

1. Overview

In this tutorial, We'll understand what is the limit of ArrayList or List implementations that can hold the maximum size.

But this is completely different from finding maximum value from ArrayList.

Before finding the max size of ArrayList, we need to understand that ArrayList collection API is implemented based on the ordinary arrays of java.

If the new ArrayList<Integer>(5) then a new ArrayList with the size of 5 is created and it can store 5 integer values.

Java List or ArrayList Max Size

Tuesday, December 21, 2021

Java Array Sort Descending Order or Reverse Order

1. Overview

In this tutorial, We'll learn how to sort the arrays in revere order in java.
Sorting arrays is done with the help of Comparator interface which works very well for the custom objects.

Array of ints or any primitive/wrapper types  can be sorted in descending order using Arrays.sort() method.

Arrays.sort(T[], Collections.reverseOrder());
Arrays.sort(ints, Collections.reverseOrder());
Arrays.sort(stringArray, Collections.reverseOrder());
Arrays.sort(empArray, Collections.reverseOrder());

Let us write the few examples on arrays reverse order sorting.

Java Array Sort Descending Order

Monday, December 20, 2021

Java Difference Between Float and Double Data Types

1. Overview

In this tutorial, We'll learn what are the differences between float and double data types in java.

Float and double data types are used to represent the floating-point values but there are few differences and you must know all of these when using them. 

First, let us understand float vs double and then next when to use which double and float?

Any fixed value is assigned to a variable is called literal in java. double and floating data storage variables are called Floating literals.

All examples shown are placed in GitHub and a link is given at the end of the article.


Java Difference Between Float and Double Data Types

Sunday, December 19, 2021

Java Extends Keyword in Depth for Beginners

1. Overview

In this tutorial, we'll learn how to use extends keyword in java with examples.

Extends keyword is used only in the inheritance chain in java and it cannot be used in any other places.

In Java, Interfaces and classes can use extends keyword.

All examples are shown in this article are available on GitHub at the end of the article.

Java Extends Keyword


Saturday, December 18, 2021

Java Finally Block: Does Finally Execute After Return?

1. Introduction


In this tutorial, You'll be learning core java concepts as part of exception handling "Java finally block when return statement is encountered"

This is a famous interview question "will finally block is executed after return statement".
Answer is Yes, The finally block is executed even after a return statement in the method. So, finally block will always be executed even whether an exception is raised or not in java.

Finally Block: Will a finally block execute after a return statement in a method in Java?


We will look into the following in this article.
  • Finally block is executed right after try or catch blocks.
  • Scenarios where finally() block not executed
  • Does finally block Override the values returned by the try-catch block?
  • When happens to finally block execution if System.exit(1) is invoked?

Friday, December 17, 2021

How to Create Constants In Java?

1. Overview


In this core java tutorial series, You'll learn today how to create a constant in java and what is the best way to create a java constant.
A constant is a variable whose value cannot be changed once it has been assigned. In fact, java does not have any built-in keyword to create constantly. But, Java provides a set of modifiers final and static. These can be applied to the variables to make them effectively constant.

Constants can make your program more easily read and understood by others. In addition, a constant is cached by the JVM as well as your application, so using a constant can improve performance.

First, let us go through on final and static keywords in java.

Java ArrayList Insert/Replace At Index

1. Overview

In this tutorial, We'll learn how to insert or replace an element at a specified index into ArrayList java.

Use the ArrayList.add(int index, Object value) method to add any object or element at the specific index of ArrayList and use ArrayList.set(int index, E  value) to replace the value at the specific index of ArrayList in java.

Let us explore the examples

All examples shown in this article are on GitHub and a link is given at the end of the post.

Java ArrayList Insert/Replace At Index

Java Array Insert - Add Values At The Specific Index

1. Overview

In this tutorial, We'll learn how to insert an element at the specific index for the given array in java.

if you are new to java, Please read the below articles on arrays.



This can be done in two ways. Let us write the example code to insert the value at any given position.

All examples shown in this article are present in GitHub and it is provided at the end of the article.

Java Array Insert - Add Values At The Specific Index


Thursday, December 16, 2021

How To Check If int is null in Java

1. Overview

In this tutorial, We'll learn how to check if primitive int is null or not in java.

First, let us write the simple example program and see what is the output if we check int is not null using != operator.

How To Check If int is null in Java


Monday, December 13, 2021

Java Insert Dimensions To Complete Referencetype [Fixed]

1. Overview

In this tutorial, We'll learn how to fix the common compile time error "Syntax error, insert "Dimensions" to complete ReferenceType" in java.

This error occurs when you are working with the java generic types.

It is suggested to follow the generic naming conventions and rules with collection api.

Compile time error

Syntax error, insert "Dimensions" to complete ReferenceType.

At the end of the article, we've given GitHub link for the examples shown in this post.

Java Insert Dimensions To Complete Referencetype [Fixed]

Java StringBuilder Insert - At Any Index and Any type of data

1. Overview

In this article, we will learn how to insert value or string at a particular index into StringBuilder in java.

StringBuilder is a mutable sequence of characters and it is recommended to use the non-synchronization applications.

StringBuilder class has a useful method insert() which takes the index and object as arguments.
When the data is added to the string builder at the given index then the remaining characters next to the given index are shifted to the right side. This is a costly operation in case of you have millions of characters in it.


Java StringBuilder Insert


Saturday, December 11, 2021

Java TreeMap Comparator

1. Overview

In this tutorial, We'll learn how to add a custom comparator to the TreeMap in java to sort by key and also sort by values.

If you are new to java comparators, please read an in-depth article on Java comparators with java 8 stream api. 

Java TreeMap Comparator

Thursday, December 9, 2021

Java Arrays Sort Comparator

1. Overview

In this tutorial, we'll learn how to sort arrays in java with the comparator.

Comparator is used to write the custom sorting with the Collections or over Arrays.

If you are new arrays, read articles on Arrays.



At the end of the article, we have given the GitHub link where you can see all code at once and you can run the programs along with me.

It is easy to sort the arrays when the array is single-dimensional. Use Arrays.sort() method.

But if you have the user defined objects or two dimensional arrays then it is not possible to sort by using just the Arrays sort() method.

In this case, we need to use the Comparator interface to provide the custom logic for it.

First, we will start with the custom objects and then next show the two or more dimensional arrays.

You will see the different ways to create the comparator in jdk8, java 7 and before versions. All will produce the same output.

Java Arrays Sort Comparator

Wednesday, December 8, 2021

Java IS-A and HAS-A Relationship With Examples

Overview


Nowadays, Many programmers get confused with these IS-A and HAS-A relationships in java. It is a common interview question for freshers and experienced programmers to check their basic knowledge of oops principles.


We will discuss the following

1) IS-A Relationship (Inheritance)
2) HAS-A Relationship (Association)
3) Examples for each type
4) Interview Questions

Java IS-A and HAS-A Relationship With Examples




The main idea of these two principles is code reusability and easy code maintenance. You will see now how the code can be easily reusable in many places and used by many other programmers.

java lang ClassCastException [Fixed]

1. Overview

In this article, we will learn what is java.lang.ClassCastException in java and how to fix it.

We will look at the meaning of ClassCastException and a few examples of it.

java lang ClassCastException

Tuesday, December 7, 2021

Java 8 Comparator Comparing Reverse Order

 

1. Overview

In this tutorial, We'll learn how to use a comparator to sort the collection in reverse order.

In Java 8, Comparator interface is added with the reverse() method to reverse the collection using the existing comparator instead of creating another custom comparator.

First, let us learn how to reverse the comparator for comparing the objects from the collection before java 8 and next using Java 8 Comparator new methods.


Java 8 Comparator Comparing Reverse Order

Monday, December 6, 2021

Java String substring() Method Example

1. Introduction


In this tutorial, You'll learn how to get the portion of a string using the substring() method of String API.

The name of this method indicates that it will fetch substring from an original string for a given index.

Java String substring() Method Example

String program to find the regionmatches() method.

Saturday, December 4, 2021

Java Comparator

1. Overview

In this tutorial, We'll learn how to use the Comparator interface in java.

A comparator interface is used to order the user-defined object in sorted order. This comparator interface has the capability to compare the same type of objects.

Comparator is used to sort the object in ascending or descending order.

We will write the example programs on custom objects with a single field or property.
And also how to sort the collection such as a list or set by multiple properties.

Java Comparator