Found 33676 Articles for Programming

Java program to compare two sets

Shriansh Kumar
Updated on 12-Apr-2025 16:31:57

2K+ Views

The given task is to write Java programs that compare two sets and check if they are equal or not. Here, Set is an interface of the Java Collection Framework that extends the Collection interface and stores unique elements. Set depicts the features of a mathematical set. Hence, it allows all those operations that we can perform on a mathematical set, such as union, comparison, intersection, etc. Since Set is an interface, we can't use its functionalities directly. For this purpose, we need a TreeSet class that implements the Set Interface and can access its methods. How to Compare Two Sets in ... Read More

Java program to adjust LocalDate to last day of month with TemporalAdjusters class

Krantik Chavan
Updated on 08-Aug-2024 17:38:19

3K+ Views

In this article, we will learn the LocalDate, Month, and TemporalAdjusters classes from java.time package. These classes are essential for handling date and time operations in Java. LocalDate represents a date without time, Month is an enumeration of the twelve months, and TemporalAdjusters provides utility methods for common date adjustments. We will learn how to set a specific date and adjust it to find the first and last days of the month and the first day of the next month. Steps to adjust LocalDate to last day of month with TemporalAdjusters class Following are the steps ... Read More

Java program to get date for all the days of the current week

Krantik Chavan
Updated on 18-Oct-2024 11:57:36

1K+ Views

In this article, we will learn to get date for all the days of the current week using Java. We will use the java.time package to get the current date and calculate the dates for the rest of the week based on the current day.  Steps to get date for all the days of the current week Following are the steps to get date for all the days of the current week − First we will import the DayOfWeek and LocalDate from java.time and Arrays class from java.util. We will get the current ... Read More

Java Program to parse a mathematical expression and operators

Nancy Den
Updated on 30-Jul-2019 22:30:25

2K+ Views

At first, we have set the mathematical expressions:String one = "10+15*20-5/5"; String two = "3+5-6"; String three = "9+2*(6-3+7)";To parse mathematical expression, use Nashorn JavaScript in Java i.e. scripting. Nashorn invoke dynamics feature, introduced in Java 7 to improve performance.For scripting, use the ScriptEngineManager class for the engine:ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("Nashorn");Now for JavaScript code from string, use eval i.e. execute the script. Here, we are parsing mathematical expressions set above:Object expResult1 = scriptEngine.eval(one); Object expResult2 = scriptEngine.eval(two); Object expResult3 = scriptEngine.eval(three);Exampleimport javax.script.ScriptEngine; import javax.script.ScriptEngineManager; public class Demo {    public static void main(String[] args) throws ... Read More

Java program to get the prime numbers with BigInteger type

Nancy Den
Updated on 04-Nov-2024 18:44:41

604 Views

In this article, we will learn how to generate prime numbers using the BigInteger class in Java. A prime number is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. Here, we have the BigInteger type, which has operations for modular arithmetic, GCD calculation, primality testing, prime generation, etc. Steps to get the prime numbers with BigInteger type Following are the steps to get the prime numbers with BigInteger type − First, we will import the BigInteger from the java.math package. ... Read More

Java program to generate random number with restrictions

Nancy Den
Updated on 15-Oct-2024 10:40:36

1K+ Views

In this article, we will learn to generate random numbers with restrictions in Java. We will be using the Java Random class from java.util package. Random class Random class is imported from java.util package. The instance of this class is used to generate the random numbers and provides different numbers of different types integer, double, long, float, etc. Steps to generate random numbers with restrictions Following are the steps to generate random numbers with restrictions − Import the Random class from the java.util package. Initialize the Random object to generate random values. ... Read More

Java Program to convert mathematical string to int

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

340 Views

To evaluate mathematical string to int, use Nashorn JavaScript in Java i.e. scripting. Nashorn invoke dynamics feature, introduced in Java 7 to improve performance.For scripting, use the ScriptEngineManager class for the engine:ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("nashorn");Now, use put() to set a key/value pair in the state of the ScriptEngine:scriptEngine.put("one", 10); scriptEngine.put("two", 50); scriptEngine.put("three", 40);Now, here is the mathematical string. Use eval to evaluate:String strExp = "(one + two - three) == 20"; Object evalExp = scriptEngine.eval(strExp);Exampleimport javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; public class Demo {    public static void main(String[] args) {       ScriptEngineManager ... Read More

How to convert Long array list to long array in Java?

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

4K+ Views

Firstly, declare a Long array list and add some elements to it:ArrayList < Long > arrList = new ArrayList < Long > (); arrList.add(100000 L); arrList.add(200000 L); arrList.add(300000 L); arrList.add(400000 L); arrList.add(500000 L);Now, set the same size for the newly created long array:final long[] arr = new long[arrList.size()]; int index = 0;Each and every element of the Long array list is assigned to the long array:for (final Long value : arrList) {    arr[index++] = value; }Exampleimport java.util.ArrayList; public class Demo {    public static void main(String[] args) {       ArrayList

Java program to get the beginning and end date of the week

Krantik Chavan
Updated on 30-Aug-2024 19:34:32

2K+ Views

In this article, we'll explore how to determine the start and end dates of a week using Java. Specifically, we'll write a program that takes a given date and calculates the Monday and Sunday of that week.  Problem Statement Write a Java program to find the beginning (Monday) and end (Sunday) dates of the week for a given date. Input Date = 2019-04-16 Output Start of the Week = 2019-04-15End of the Week = 2019-04-21 Steps to calculate week's start and end dates Following are the steps to get the beginning and end date of the week − ... Read More

How to count days between two dates in Java

Nancy Den
Updated on 30-Jul-2019 22:30:25

1K+ Views

Let us first set two dates:LocalDate date1 = LocalDate.of(2019, 4, 16); LocalDate date2 = date1.with(Month.MAY).withDayOfMonth(04);Now, count the dates between both the above dates using between():int numDays = Period.between(date1, date2).getDays();Exampleimport java.time.LocalDate; import java.time.Month; import java.time.Period; public class Demo {    public static void main(String[] argv) {       LocalDate date1 = LocalDate.of(2019, 4, 16);       LocalDate date2 = date1.with(Month.MAY).withDayOfMonth(04);       int numDays = Period.between(date1, date2).getDays();       System.out.println("Number of days between two dates = "+numDays);    } }OutputNumber of days between two dates = 18

Advertisements