Stack and Queue in Java with examples

Stack and Queue in Java with examples

Consider a table with a stack of plates. After the first one is placed on the table, the next one is placed on top of it; the third one is placed on top of the second, and so on, until the desired number is reached. To take the dishes off the table one by one, start with the last one placed on top; then move on to the last-but-one; then the one next to the top; and so on.

StringBuilder in Java explained with examples

StringBuilder in Java explained with examples

StringBuilder is a Java class that allows you to generate a mutable, or changeable, sequence of characters. The StringBuilder class, like StringBuffer, is an alternative to the Java Strings class, which provides an immutable sequence of characters. However, there is a critical distinction between StringBuffer and StringBuilder: the latter is non-synchronized.

How to use scanner in Java

How to use scanner in Java

The Scanner is a class in java.util package. This class is responsible for reading data from a string, the keyboard, a file, or a network socket. This article focuses solely on reading the keyboard input and displaying the output in a terminal window. Reading data from a file or a network channel is done similarly.

How to use Math.random() method in Java

How to use Math.random() method in Java

The Math.random() method is one of Java’s most commonly used methods for generating a random numeric number. The java.lang class is a built-in class in Java.Math with several mathematical procedures, one of which is Math.random(). Because the Math.random() method is a static method, it can be invoked/called without first constructing a math class instance—the java.lang.Math class offers several methods for performing basic numeric operations such as the exponential, logarithm, square root, and trigonometric functions.

How to format a Date to String in Java

How to format a Date to String in Java

Changing the date format of a given Date or String is a typical Java programming task. For example, suppose you have “2022-05-22 12:00:00” and want to convert it to “2022-05-22,” or convert from dd-MM-YY to MM-dd-YY, or any other format you like, as long as it is a valid date format as defined by Java. How will you accomplish this? It’s not that difficult, after all. It’s a simple two-step procedure.

TreeMap in Java

TreeMap in Java

Besides the AbstractMap Class, Java’s TreeMap discreetly implements the Map interface and NavigableMap. Depending on which constructor is used, the map is sorted either by the natural ordering of its keys or by a Comparator specified at map creation time.

Encapsulation in Java

Encapsulation in Java

In Java, encapsulation combines data (variables) and code that acts on the data (methods) into a single unit. Encapsulation means that a class’s variables are concealed from other classes and can only be accessed through its current class’s methods. Consequently, it’s also known as data concealment.

HashMap in Java with examples

HashMap in Java with examples

Since Java 1.2, HashMap has been part of the Java collection. The java.util package contains this class. It implements the Java Map interface in its most basic form. It holds data as (Key, Value) pairs that can be accessed using a different index type (e.g., an Integer). A key (index) refers to another object (value). Trying to insert the duplicate key overwrites the associated key’s element.