Java Articles

Page 121 of 450

Difference between HashMap and HashSet in Java.

Nitin Sharma
Nitin Sharma
Updated on 11-Mar-2026 27K+ Views

HashMap and HashSet both are one of the most important classes of Java Collection framework.Following are the important differences between HashMap and HashSet.Sr. No.KeyHashMapHashSet1ImplementationHashmap is the implementation of Map interface.Hashset on other hand is the implementation of set interface.2Internal implementationHashmap internally do not implements hashset or any set for its implementation.Hashset internally uses Hashmap for its implementation.3Storage of elementsHashMap Stores elements in form of key-value pair i.e each element has its corresponding key which is required for its retrieval during iteration.HashSet stores only objects no such key value pairs maintained.4Method to add elementPut method of hash map is used to ...

Read More

Java Program to count letters in a String

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 21K+ Views

Let’s say we have the following string, that has some letters and numbers.String str = "9as78";Now loop through the length of this string and use the Character.isLetter() method. Within that, use the charAt() method to check for each character/ number in the string.for (int i = 0; i < str.length(); i++) {    if (Character.isLetter(str.charAt(i)))    count++; }We have set a count variable above to get the length of the letters in the string.Here’s the complete example.Examplepublic class Demo {    public static void main(String []args) {       String str = "9as78";       int count = ...

Read More

How to create date object in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 34K+ Views

Using the Date classYou can create a Date object using the Date() constructor of java.util.Date constructor as shown in the following example. The object created using this constructor represents the current time.Exampleimport java.util.Date; public class CreateDate {    public static void main(String args[]) {             Date date = new Date();       System.out.print(date);    } }OutputThu Nov 02 15:43:01 IST 2018Using the SimpleDateFormat classUsing the SimpleDateFormat class and the parse() method of this you can parse a date string in the required format and create a Date object representing the specified date.Exampleimport java.text.ParseException; import java.text.SimpleDateFormat; ...

Read More

Multiple inheritance by Interface in Java

Arushi
Arushi
Updated on 11-Mar-2026 86K+ Views

An interface contains variables and methods like a class but the methods in an interface are abstract by default unlike a class. Multiple inheritance by interface occurs if a class implements multiple interfaces or also if an interface itself extends multiple interfaces.A program that demonstrates multiple inheritance by interface in Java is given as follows:Exampleinterface AnimalEat {    void eat(); } interface AnimalTravel {    void travel(); } class Animal implements AnimalEat, AnimalTravel {    public void eat() {       System.out.println("Animal is eating");    }    public void travel() {       System.out.println("Animal is travelling");    } ...

Read More

Java program for Multiplication of Array elements

Venkata Sai
Venkata Sai
Updated on 11-Mar-2026 20K+ Views

To find the product of elements of an array.create an empty variable. (product)Initialize it with 1.In a loop traverse through each element (or get each element from user) multiply each element to product.Print the product.Exampleimport java.util.Arrays; import java.util.Scanner; public class ProductOfArrayOfElements {    public static void main(String args[]){       System.out.println("Enter the required size of the array :: ");       Scanner s = new Scanner(System.in);       int size = s.nextInt();       int myArray[] = new int [size];       int product = 1;       System.out.println("Enter the elements of the array one by one ");       for(int i=0; i

Read More

Split String with Comma (,) in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 34K+ Views

Let’s say the following is our string.String str = " This is demo text, and demo line!";To split a string with comma, use the split() method in Java.str.split("[,]", 0);The following is the complete example.Examplepublic class Demo {     public static void main(String[] args) {        String str = "This is demo text, and demo line!";        String[] res = str.split("[,]", 0);        for(String myStr: res) {           System.out.println(myStr);        }     } }OutputThis is demo text and demo line!

Read More

Concatenate string to an int value in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 30K+ Views

To concatenate a string to an int value, use the concatenation operator.Here is our int.int val = 3;Now, to concatenate a string, you need to declare a string and use the + operator.String str = "Demo" + val;Let us now see another example.Exampleimport java.util.Random; public class Demo {    public static void main( String args[] ) {       int val = 3;       String str = "" + val;       System.out.println(str + " = Rank ");    } }Output3 = Rank

Read More

Java Program to convert ASCII code to String

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 18K+ Views

To convert ASCII to string, use the toString() method. Using this method will return the associated character.Let’s say we have the following int value, which works as ASCII for us.int asciiVal = 89;Now, use the toString() method.String str = new Character((char) asciiVal).toString();Examplepublic class Demo {    public static void main(String []args) {       int asciiVal = 87;       String str = new Character((char) asciiVal).toString();       System.out.println(str);    } }OutputW

Read More

Generate a random array of integers in Java

Anvi Jain
Anvi Jain
Updated on 11-Mar-2026 40K+ Views

In order to generate random array of integers in Java, we use the nextInt() method of the java.util.Random class. This returns the next random integer value from this random number generator sequence.Declaration − The java.util.Random.nextInt() method is declared as follows −public int nextInt()Let us see a program to generate a random array of integers in Java −Exampleimport java.util.Random; public class Example {    public static void main(String[] args) {       Random rd = new Random(); // creating Random object       int[] arr = new int[5];       for (int i = 0; i

Read More

Print a 2D Array or Matrix in Java

George John
George John
Updated on 11-Mar-2026 32K+ Views

In this post we will try to print an array or matrix of numbers at console in same manner as we generally write on paper.For this the logic is to access each element of array one by one and make them print separated by a space and when row get to end in matrix then we will also change the row.Examplepublic class Print2DArray {    public static void main(String[] args) {       final int[][] matrix = {          { 1, 2, 3 },          { 4, 5, 6 },          { 7, 8, 9 }       };       for (int i = 0; i

Read More
Showing 1201–1210 of 4,498 articles
« Prev 1 119 120 121 122 123 450 Next »
Advertisements