How-to articles, tricks, and solutions about JAVA

Android Split string

To split a string in Android, you can use the split() method of the String class.

Can you find all classes in a package using reflection?

Yes, it is possible to find all classes in a package using reflection in Java.

Getting a File's MD5 Checksum in Java

To get a file's MD5 checksum in Java, you can use the MessageDigest class from the java.security package.

How do I measure time elapsed in Java?

To measure time elapsed in Java, you can use the System.currentTimeMillis() method to get the current time in milliseconds, then subtract the start time from the end time to get the elapsed time.

Java, How do I get current index/key in "for each" loop

To get the current index/key in a "for each" loop, you can use the for loop instead.

Java: how can I split an ArrayList in multiple small ArrayLists?

Here is an example of how you can split an ArrayList into multiple smaller ArrayLists in Java:

Convert a JSON String to a HashMap

Here is an example of how you can convert a JSON string to a HashMap in Java:

Java dynamic array sizes?

In Java, you can implement a dynamic array (i.e., an array that can grow and shrink as needed) by using an ArrayList or an ArrayDeque.

How to print a query string with parameter values when using Hibernate

To print a Hibernate query string with parameter values, you can use the toString() method of the Query interface.

How to respond with an HTTP 400 error in a Spring MVC @ResponseBody method returning String

To respond with an HTTP 400 error in a Spring MVC controller method that returns a String, you can throw a ResponseStatusException with a status of BAD_REQUEST.

Convert java.time.LocalDate into java.util.Date type

To convert a java.time.LocalDate into a java.util.Date type in Java, you can use the atStartOfDay() method of the LocalDate class to get a LocalDateTime object and then use the toInstant() method to convert it to an Instant object.

What is the equivalent of the C++ Pair<L,R> in Java?

In Java, the equivalent of the C++ Pair class is the AbstractMap.SimpleEntry class or the AbstractMap.SimpleImmutableEntry class.

Java 8 Distinct by property

To get a list of distinct elements by a property in Java 8, you can use the distinct() method of the Stream interface and the map() method to extract the property from each element.

Java Pass Method as Parameter

To pass a method as a parameter in Java, you can use a functional interface and a lambda expression.

How can I check if an element exists with Selenium WebDriver?

To check if an element exists with Selenium WebDriver, you can use the findElements() method of the WebDriver interface and check the size of the returned list.

What causes javac to issue the "uses unchecked or unsafe operations" warning

The javac compiler may issue the "uses unchecked or unsafe operations" warning if your code uses operations that may result in an Unchecked warning at runtime.

HashMap - getting First Key value

To get the first key-value pair from a HashMap in Java, you can use the entrySet() method to get a set of the map's entries and then use the iterator() method to get an iterator for the set.

What is simplest way to read a file into String?

To read a file into a String in Java, you can use the readAllBytes() method of the Files class and the new String() constructor:

Eclipse java debugging: source not found

If you are trying to debug a Java application in Eclipse and you see the error "Source not found", it means that the source code for the class you are trying to debug is not available in the current project or the project build path.

Difference between @Mock and @InjectMocks

In the context of testing with the Mockito framework, the @Mock annotation is used to create a mock object of a class or interface, and the @InjectMocks annotation is used to inject the mock objects into a test class.

How to check if a String contains another String in a case insensitive manner in Java?

To check if a String contains another String in a case-insensitive manner in Java, you can use the toLowerCase() method of the String class and the contains() method.

What are Java command line options to set to allow JVM to be remotely debugged?

To allow the Java Virtual Machine (JVM) to be remotely debugged, you can use the following command line options:

How to convert object array to string array in Java

To convert an object array to a string array in Java, you can use the toString() method of the Object class and the map() method of the Stream class.

Calling Non-Static Method In Static Method In Java

To call a non-static method from a static method in Java, you need to create an instance of the class and call the non-static method on that instance.