Rishi Raj has Published 127 Articles

Replace all the elements of a Vector with Collections.fill() in Java

Rishi Raj

Rishi Raj

Updated on 30-Jul-2019 22:30:24

453 Views

All the elements of a vector can be replaced by a specific element using java.util.Collections.fill() method. This method requires two parameters i.e. the Vector and the element that replaces all the elements in the Vector. No value is returned by the Collections.fill() method.A program that demonstrates this is given as ... Read More

Why Protected access modifier used in Java?

Rishi Raj

Rishi Raj

Updated on 30-Jul-2019 22:30:24

172 Views

The data members and methods of a class can be accessed from the same package or sub-classes in a different package if they are specified with the protected access modifier. The keyword protected is used to specify this modifier.A program that demonstrates the protected access modifier in Java is given ... Read More

Why Abstract Class is used in Java?

Rishi Raj

Rishi Raj

Updated on 30-Jul-2019 22:30:24

710 Views

A class is an abstract class if it contains at least one abstract method. It can contain other non-abstract methods as well. A class can be declared as abstract by using the abstract keyword. Also, an abstract class cannot be instantiated.A program that demonstrates an abstract class in Java is ... Read More

Sort a List in reverse order in Java

Rishi Raj

Rishi Raj

Updated on 30-Jul-2019 22:30:24

8K+ Views

A list can be sorted in reverse order i.e. descending order using the java.util.Collections.sort() method. This method requires two parameters i.e. the list to be sorted and the Collections.reverseOrder() that reverses the order of an element collection using a Comparator.The ClassCastException is thrown by the Collections.sort() method if there are ... Read More

Send email using Java Program

Rishi Raj

Rishi Raj

Updated on 30-Jul-2019 22:30:23

463 Views

To send an e-mail using your Java Application is simple enough but to start with you should have JavaMail API and Java Activation Framework (JAF) installed on your machine. You can download latest version of JavaMail (Version 1.2) from Java's standard website. You can download latest version ... Read More

Sorting a HashMap according to values in Java

Rishi Raj

Rishi Raj

Updated on 30-Jul-2019 22:30:23

518 Views

As we know that Hash map in Java does not maintain insertion order either by key or by order.Also it does not maintain any other order while adding entries to it. Now in order to sort a hash map according to the values mapped to its corresponding keys we first ... Read More

How to create immutable class in Java?

Rishi Raj

Rishi Raj

Updated on 30-Jul-2019 22:30:23

499 Views

An immutable class object's properties cannot be modified after initialization. For example String is an immutable class in Java. We can create a immutable class by following the given rules below − Make class final − class should be final so that it cannot be extended. Make each field ... Read More

How to prevent Cloning to break a Singleton Class Pattern in Java?

Rishi Raj

Rishi Raj

Updated on 30-Jul-2019 22:30:23

242 Views

A Singleton pattern states that a class can have a single instance and multiple instances are not permitted to be created. For this purpose, we make the constructor of the class a private and return a instance via a static method. But using cloning, we can still create multiple instance ... Read More

PriorityQueue Class in Java Programming

Rishi Raj

Rishi Raj

Updated on 30-Jul-2019 22:30:23

82 Views

The java.util.PriorityQueue class is an unbounded priority queue based on a priority heap.Following are the important points about PriorityQueue − The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used. A ... Read More

Private Constructors and Singleton Classes in Java Programming

Rishi Raj

Rishi Raj

Updated on 30-Jul-2019 22:30:23

1K+ Views

As we know the primary role of the constructor is to instantiate a class object now if we made the constructor as private then we restrict its calling to be only in defining a class and not in some other class. Now the singleton class in Java is defined as ... Read More

Advertisements