How-to articles, tricks, and solutions about JAVA

How to evaluate a math expression given in string form?

There are a few different ways to evaluate a math expression given in string form:

How to execute a java .class from the command line

To execute a Java class from the command line, you will need to use the java command and specify the name of the class that you want to run.

How to extract a substring using regex

To extract a substring from a string using a regular expression in Java, you can use the following steps:

How to filter a Java Collection (based on predicate)?

To filter a Java collection based on a predicate, you can use the removeIf method of the Collection interface. This method removes all elements from the collection that match the specified predicate.

How to find Java Heap Size and Memory Used (Linux)?

To find the Java heap size and the amount of memory used on a Linux system, you can use the jstat command and specify the process ID (PID) of the Java process.

How to find the index of an element in an int array?

To find the index of an element in an int array in Java, you can use the indexOf() method of the Arrays class.

How to find the length of an array list?

To find the length (number of elements) of an ArrayList in Java, you can use the size() method of the ArrayList class.

How to fix java.lang.UnsupportedClassVersionError: Unsupported major.minor version

The java.lang.UnsupportedClassVersionError error is usually caused by trying to run a Java class that was compiled with a newer version of the Java compiler than the version of the JRE (Java Runtime Environment) that you are using.

How to fix org.hibernate.LazyInitializationException - could not initialize proxy - no Session

The LazyInitializationException in Hibernate is thrown when an object that has been loaded with a "lazy" fetch type is accessed outside of a valid session.

How to for each the hashmap?

To iterate over the key-value pairs in a HashMap in Java, you can use the forEach() method and provide a lambda expression as an argument. Here's an example:

How to format decimals in a currency format?

You can use the NumberFormat class to format decimals in a currency format.

How to generate a random alpha-numeric string

To generate a random alpha-numeric string in Java, you can use the Random class and the nextInt and nextBoolean methods to generate random characters and append them to a StringBuilder. Here's an example of how you might do this:

How to get a file's Media Type (MIME type)?

To get the media type (also known as MIME type) of a file in Java, you can use the Files.probeContentType method of the java.nio.file.Files class.

How to get a thread and heap dump of a Java process on Windows that's not running in a console

To get a thread and heap dump of a Java process on Windows that is not running in a console, you can use the jstack and jmap tools that are included with the Java Development Kit (JDK).

How to get an enum value from a string value in Java

To get an enum value from a string value in Java, you can use the valueOf method of the enum type. Here's an example of how you might do this:

How to get current location in Android

To get the current location in Android, you can use the LocationManager class and the LocationProvider interface.

How to get current moment in ISO 8601 format with date, hour, and minute?

You can use the Instant class from the Java 8 java.time package to get the current moment in ISO 8601 format with date, hour, and minute.

How to get current timestamp in string format in Java? "yyyy.MM.dd.HH.mm.ss"

To get the current timestamp in string format in Java, you can use the SimpleDateFormat class and specify the desired format string.

How to get maximum value from the Collection (for example ArrayList)?

In Java, default methods are methods that are defined in an interface and have a default implementation. They were introduced in Java 8 as a way to add new functionality to interfaces without breaking backward compatibility.

How to get milliseconds from LocalDateTime in Java 8

To get the milliseconds from a LocalDateTime object in Java 8, you can use the toInstant method and the toEpochMilli method.

How to get row count using ResultSet in Java?

To get the row count using a ResultSet object in Java, you can use the last() method to move the cursor to the last row, and then use the getRow() method to get the row number:

How to get the current date/time in Java

To get the current date and time in Java, you can use the <code>java.time</code> package introduced in Java 8.

How to get the current time in YYYY-MM-DD HH:MI:Sec.Millisecond format in Java?

To get the current time in the YYYY-MM-DD HH:MI:Sec.Millisecond format in Java, you can use the SimpleDateFormat class and specify the desired format string:

How to get the current working directory in Java?

To get the current working directory in Java, you can use the System.getProperty("user.dir") method. This method returns the current working directory of the Java process as a string. Here's an example:

How to get the first element of the List or Set?

To get the first element of a List or Set in Java, you can use the following methods: