How-to articles, tricks, and solutions about JAVA
There are a few different ways to evaluate a math expression given in string form:
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.
To extract a substring from a string using a regular expression in Java, you can use the following steps:
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.
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.
To find the index of an element in an int array in Java, you can use the indexOf() method of the Arrays class.
To find the length (number of elements) of an ArrayList in Java, you can use the size() method of the ArrayList class.
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.
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.
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:
You can use the NumberFormat class to format decimals in a currency format.
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:
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.
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).
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:
To get the current location in Android, you can use the LocationManager class and the LocationProvider interface.
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.
To get the current timestamp in string format in Java, you can use the SimpleDateFormat class and specify the desired format string.
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.
To get the milliseconds from a LocalDateTime object in Java 8, you can use the toInstant method and the toEpochMilli method.
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:
To get the current date and time in Java, you can use the <code>java.time</code> package introduced in Java 8.
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:
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:
To get the first element of a List or Set in Java, you can use the following methods: