Found 33676 Articles for Programming

Non-generic Vs Generic Collection in Java

Alshifa Hasnain
Updated on 19-Mar-2025 18:53:35

3K+ Views

In this article, we will learn about the collections in Java. The collections are used to store and manipulate groups of objects. Java collections can be broadly classified into two types − Non-generic Collections (Before Java 5) Generic Collections (Introduced in Java 5) Non-generic collections allow storing objects of different types leading to runtime errors. On the other hand generic collections enforce type safety at compile-time, reducing the risk of type-related errors.  Non - Generic Collection When the data structure is non-generic, it causes issues when the data is tried ... Read More

Method Overloading and null error in Java

AmitDiwan
Updated on 14-Sep-2020 08:55:40

869 Views

When method is overloaded in Java, the functions have the same name, and the number of parameters to the function is same. In such cases, if the parameters are non-primitive and have the ability to accept null values, the compiler gets confused when the function is called with a null value, since it can’t choose either of them, because both of them have the ability to accept null values. This results in a compile time error.ExampleBelow is an example showing the same − Live Demopublic class Demo {    public void my_function(Integer i) {       System.out.println("The function with integer ... Read More

Multiset Interface – Java

AmitDiwan
Updated on 14-Sep-2020 08:53:29

1K+ Views

Multiset is a collection in Java, that helps in order-independent equality, similar to Set structure. But the only difference is that a multiset can also contain duplicate elements.If multiset is visualized as a list, then this wouldn’t be the case since lists can’t hold duplicate values, and list elements are always in a specific order.Multiset can be thought of as a collection that lies somewhere between a list and a set structure. In multiset, the duplicates values are allowed, and there is no guarantee that the elements in a multiset would occur in a specific order. Multiset is also known ... Read More

MultiMap in Java

AmitDiwan
Updated on 14-Sep-2020 08:51:49

308 Views

Multimap is a general method to bind the keys with random multiple values. The Multimap framework in Guava has methods that help in dealing with mapping keys to multiple values. Multimap can be visualized as a framework that −Is a collection of mapping from one key to one specific valueIs a collection of mapping from unique key to multiple values, i.e. collection of values.It can be implemented in places that use Map.Advantages of MultimapAn empty collection need not be populated before added a key value pair with the help of the function ‘put’.The ‘get’ method doesn’t return a null, except ... Read More

Multidimensional Collections in Java

Alshifa Hasnain
Updated on 19-Mar-2025 18:52:59

3K+ Views

In this article, we will learn about multidimensional collections in Java. These collections offer dynamic resizing, flexible data storage, and better memory management compared to traditional arrays. What are Multidimensional collections? Multidimensional collections are also known as Nested collections. It is a group of objects wherein every group has any number of objects that can be created dynamically. They can be stored in any position as well. In the case of arrays, the user would be bound to a specific number of rows and columns, hence multidimensional structure helps create and add elements dynamically. Different Approaches The following are the ... Read More

Searching characters and substring in a String in Java

AmitDiwan
Updated on 14-Sep-2020 08:44:41

645 Views

Following is the Java program to search characters and substring in a string −Example Live Demoimport java.io.*; import java.lang.*; public class Demo {    public static void main (String[] args) {       String test_str = "Hello";       CharSequence seq = "He";       boolean bool_1 = test_str.contains(seq);       System.out.println("Was the substring found? " + bool_1);       boolean bool_2 = test_str.contains("Lo");       System.out.println("Was the substring found? " + bool_2);    } }OutputWas the substring found? true Was the substring found? FalseA class named Demo contains the main function. Here, a string ... Read More

Local Variable Type Inference or LVTI in Java 10

AmitDiwan
Updated on 14-Sep-2020 08:36:17

433 Views

Type inference in Java refers to the automatic detection of the variable’s datatype. This automatic detection usually happens at compile time. It is a feature of Java 10 and it allows the developer to skip declaring the type that is associated with the local variables. Local variables are those which are defined inside a method, initialization block, in for-loops, etc. The type would be usually identified by JDK.Upto Java 9, the following syntax was used to define local variable of a class type −class_name variable_name = new class_name(Arguments);This way, the type of the object would be specified on the right ... Read More

Java program to swap first and last characters of words in a sentence

AmitDiwan
Updated on 31-Jul-2024 17:51:50

3K+ Views

Problem Statement Given a sentence, create a  program in Java that swaps the first and last characters of each word efficiently as given below − Input That is a sample Output The string after swapping the last characters of every word is : thaT si a eampls Steps to swap first and last characters of words in a sentence Below are the steps to swap first and last characters of words in a sentence − Convert string to character array. Iterate through the character array using while loop to identify the ... Read More

Java program to merge two files into a third file

Alshifa Hasnain
Updated on 20-Mar-2025 18:18:43

4K+ Views

In this article, we will learn to merge two files into a third file in Java. Combining the contents of two files into a third file is a typical operation in file handling, frequently needed to aggregate data. Java offers efficient facilities such as BufferedReader and PrintWriter to accomplish this easily. ApproachThe following are the steps to merge the contents of two files into a third file in Java− Read the contents of the first file line by line using a BufferedReader. Write each line to the third file using a ... Read More

Java program for ShellSort

AmitDiwan
Updated on 15-Nov-2024 18:47:54

706 Views

In this article, we will learn to write a program for shell sort using Java. In the program, we’ll apply this technique to sort an array and observe how the algorithm optimizes the sorting process by reducing the interval between elements. Shell sort Shell sort is a sorting technique that is similar to insertion sort, wherein elements are sorted which are at far ends (either end) of the array. This way, the interval size between the next and the second to last element reduces. This happens for all the elements in the array until the interval distance is reduced to ... Read More

Advertisements