
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 33676 Articles for Programming

858 Views
Let’s say the following is our string with integer and characters −String str = "(29, 12; 29, ) (45, 67; 78, 80)";Now, to extract integers, we will be using the following pattern −\dWe have set it with Pattern class −Matcher matcher = Pattern.compile("\d+").matcher(str);Example Live Demoimport java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo { public static void main(String[] args) { String str = "(29, 12; 29, ) (45, 67; 78, 80)"; Matcher matcher = Pattern.compile("\d+").matcher(str); Listlist = new ArrayList(); while(matcher.find()) { list.add(Integer.parseInt(matcher.group())); } System.out.println("Integers = "+list); } }OutputIntegers = [29, 12, 29, 45, 67, 78, 80]

625 Views
In this article, we will learn how to calculate the frequency of words in a text using Java and Lambda Expressions. By leveraging concise syntax and functional programming techniques, we can efficiently process text data and determine word occurrences. This approach is particularly useful for applications like text analysis and natural language processing. What is Lambda Expression? A lambda expression in Java is a concise way to represent an anonymous function(a function without a name). Introduced in Java 8, it simplifies the implementation of functional interfaces (interfaces with a single abstract method), enabling cleaner and more readable code. Syntax of ... Read More

5K+ Views
To display number in scientific notation, create NumberFormat object first −NumberFormat numFormat = newDecimalFormat();Now, let’s say you need to format the minimum value of Integer −int i = Integer.MIN_VALUE; System.out.println(i); numFormat = newDecimalFormat("0.######E0"); System.out.println(numFormat.format(i)); numFormat = newDecimalFormat("0.#####E0"); System.out.println(numFormat.format(i));Above, we have used format() method of the NumberFormat class.Example Live Demoimport java.text.DecimalFormat; import java.text.NumberFormat; public class Demo { public static void main(String args[]) { NumberFormat numFormat = new DecimalFormat(); int i = Integer.MIN_VALUE; System.out.println(i); numFormat = new DecimalFormat("0.######E0"); System.out.println(numFormat.format(i)); numFormat = new DecimalFormat("0.#####E0"); ... Read More

1K+ Views
Create a Calendar instance and Date object −Calendar cal = Calendar.getInstance(); Date date = new Date(); cal.setTime(date);Now, create a HashMap and store Date value −LinkedHashMaphashMap = new LinkedHashMap(); hashMap.put("year", cal.get(Calendar.YEAR)); hashMap.put("month", cal.get(Calendar.MONTH)); hashMap.put("day", cal.get(Calendar.DAY_OF_MONTH));Example Live Demoimport java.util.Calendar; import java.util.Date; import java.util.LinkedHashMap; public class Demo { public static void main(String[] argv) { Calendar cal = Calendar.getInstance(); Date date = new Date(); System.out.println("Date = "+date); cal.setTime(date); LinkedHashMaphashMap = new LinkedHashMap(); hashMap.put("year", cal.get(Calendar.YEAR)); hashMap.put("month", cal.get(Calendar.MONTH)); hashMap.put("day", cal.get(Calendar.DAY_OF_MONTH)); ... Read More

189 Views
To get the duplicate numbers for random numbers in a range, loop through and create two Random class objects −Use nextInt() to get the next number −intrandVal1 = new Random().nextInt(50); intrandVal2 = new Random().nextInt(50);Now, compare both the above numbers −if (randVal1 == randVal2) { System.out.println("Duplicate number = "+randVal1); }All the above is to be done in a loop −for (int i = 1; i

2K+ Views
To check if a Float is isInfinite, use the isInfinite() method and to check for NAN, use the isNaN() method.Example Live Demopublic class Demo { public static void main(String[] args) { float value1 = (float) 1 / 0; boolean res1 = Float.isInfinite(value1); System.out.println("Checking for isInfinite? = "+res1); float value2 = (float) Math.sqrt(9); boolean res2 = Float.isNaN(value2); System.out.println("Checking for isNan? = "+res2); } }OutputChecking for isInfinite? = true Checking for isNan? = false

905 Views
In this article, we will calculate the number of minutes between two-time instants using Java. This will be done by using the Instant and Duration classes from the java.time package. We'll create two instances of time, add specific hours and minutes to one of them, and then compute the difference in minutes between the two.Steps to get minutes between two-time instantsFollowing are the steps to get minutes between two-time instants −First, import the necessary classes: Duration, Instant, and ChronoUnit from the java.time package.Create an instance of the current time using Instant.now().Add 5 hours and 10 minutes to the first time instance to ... Read More

3K+ Views
In this article, we will demonstrate how to calculate the difference in milliseconds between two Instant objects using the ChronoUnit class. It shows how to work with time-based classes in the java.time package, specifically focusing on creating and manipulating time instants and calculating durations between them. ChronoUnit: A standard set of date and time units lets you manipulate dates, times, and date-times. These units, such as years, months, and days, are designed to work across various calendar systems, even though the specific rules might differ slightly. You can also extend these units by implementing the TemporalUnit interface. Problem Statement Calculate the ... Read More

273 Views
At first set the date with java.util.Date −java.util.Date date = new Date();Now, convert the date to LocalDate −Instant instant = Instant.ofEpochMilli(date.getTime()); System.out.println("LocalDate = "+LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalDate());Example Live Demoimport java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Date; public class Demo { public static void main(String[] args) { java.util.Date date = new Date(); System.out.println("Date = "+date); Instant instant = Instant.ofEpochMilli(date.getTime()); System.out.println("LocalDate = "+LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalDate()); } }OutputDate = Thu Apr 18 23:51:06 IST 2019 LocalDate = 2019-04-18