Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles by Alshifa Hasnain
221 articles
What is the order of execution of non-static blocks with respect to a constructor in Java?
In Java, non-static blocks run before the constructor whenever an object is created. If multiple non-static blocks exist, they execute in the order they appear in the class, followed by the constructor. In this article, we will understand this order of execution with the help of examples. What Are Non-Static Blocks in Java? The Non-static blocks are class-level blocks which don't contain a prototype. A non-static block must perform any logic whenever an object is instantiated, regardless of the constructor. The Non-static blocks are automatically called by the JVM for every object creation in the Java stack area. We can ...
Read MoreJava program to split the array and add the first part to the end
Our task is to write a Java program to split the array and add the first part to the end. Here, the value of k=2, which means we need to keep 2 elements at the end of the array. Input: 67, 45, 78, 90, 12, 102, 34 Output: 78, 90, 12, 102, 34, 67, 45 Split the Java Array, Add the First Part to the End The following are the approaches to split the array and add the first part to the end: By shifting elements Using the reversal method ...
Read MoreJava program to shift array elements to the left
What Is Left Shifting in an Array? Shifting the array elements to the left by the given number of positions is also known as left rotation of the array. The element at the beginning gets pushed out of the front during the shift, but instead of being deleted, they are re-attached at the end of the array. Let us consider an example for a better understanding: Original Array: After 1 Left Shift: We can see in the above figure that, after the first shift, the element "1" is pushed out and then reattached at the end of the ...
Read MoreCount the number of columns in a MySQL table with Java
In this article, we will learn how to count the number of columns in a MySQL table using JDBC. We will be using the ResultSetMetaData to get details of the table by using simple examples. What is ResultSetMetaData? The ResultSetMetaData is an interface that is present in the java.sql package. Using ResultSetMetaData, we can get information about the table, for example, what are the column names of each and every table, and how many columns are there?. To create the object for ResultSet: ResultSet rs=st.executeQuery("Select * from Student"); The executeQuery method writes the records, which are then stored in the ...
Read MoreAdd a value to Pair Tuple in Java
By default, Java does not have built-in tuple support; to use the tuple, we use the third-party library called Javatuples. Using this library, you can create a tuple of different sizes, starting from a single-element tuple, which is the Unit class, up to a ten-element tuple (Decade class). The following is the list of JavaTuples library classes: Unit: 1 element tuple Pair: 2 element tuple Triplet: 3 element tuple Quartet: 4 element tuple Quintet: 5 element tuple ...
Read MoreAdd a value in Ennead Tuple in Java
By default, Java does not have built-in tuple support; to use the tuple, we use the third-party library called Javatuples. Using this library, you can create a tuple of different sizes, starting from a single-element tuple, which is the Unit class, up to a ten-element tuple (Decade class). The following is the list of JavaTuples library classes: Unit: 1 element tuple. Pair: 2 element tuple. Triplet: 3 element tuple Quartet: 4 element tuple Quintet: 5 element tuple ...
Read MoreDifference between HashTable and ConcurrentHashMap in Java
In this article, we will learn about the ConcurrentHashMap and HashTable in Java. First, we will know about ConcurrentHashMap, the syntax of ConcurrentHashMap, and an example after that will learn about HashTable, the syntax of HashTable, and an example. At the end, we will see a table showing the difference between ConcurrentHashMap and HashTable. What is ConcurrentHashMap in Java? ConcurrentHashMap is a class that was introduced in JDK 1.5. The ConcurrentHashMap is threadsafe as it allows multiple threads to read and write the data without locking the entire map. ConcurrentHashMap applies locks only at the bucket level, called a fragment, while ...
Read MoreHow to get the date and time in JShell in Java 9?
In this article, we will learn to get the date and time in JShell in Java 9. First, we will learn about the date and time class in Java with an example, and after that will learn about date and time in JShell with multiple examples. JShell JShell is an interactive command-line tool that allows us to learn, investigate, and explore the Java language and its API. We can type any valid Java code into the console and get immediate results without the need to write a verbose class with the main() method. C:\Users\User>jshell | Welcome to JShell -- Version ...
Read MoreHow to use the collect() method in Stream API in Java 9?
In this article, we will learn to use the collect() method in the Stream API in Java 9. Java Stream API collect() Method In Java, the collect() method in the Stream API collects all objects from a stream object and stores them in a type of collection. The user has to provide what type of collection the results can be stored. We specify the collection type using the Collectors Enum. There are different types, and different operations can be present in the Collectors Enum. Syntax The following is the syntax for the collect() method declaration: R collect(Collector
Read MoreDifference between EnumMap and HashMap in Java
In this article, we will learn about the EnumMap and HashMap in Java. First, we will know about EnumMap, the syntax of EnumMap, and an example after that will learn about HashMap, the syntax of HashMap, and an example. At the end, we will see a table showing the difference between EnumMap and HashMap. What is EnumMap? EnumMap is introduced in JDK 5. It is designed to use an Enum as a key in the Map. It is an implementation of the Map interface. All of the keys in the EnumMap should be of the same enum type. Keys cannot be ...
Read More