How-to articles, tricks, and solutions about JAVA
To split a string in Android, you can use the split() method of the String class.
Yes, it is possible to find all classes in a package using reflection in Java.
You can use the map and collect methods of the Stream class to achieve this.
To get a file's MD5 checksum in Java, you can use the MessageDigest class from the java.security package.
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.
To get the current index/key in a "for each" loop, you can use the for loop instead.
Here is an example of how you can split an ArrayList into multiple smaller ArrayLists in Java:
Here is an example of how you can convert a JSON string to a HashMap in Java:
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.
To print a Hibernate query string with parameter values, you can use the toString() method of the Query interface.
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.
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.
In Java, the equivalent of the C++ Pair class is the AbstractMap.SimpleEntry class or the AbstractMap.SimpleImmutableEntry class.
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.
To pass a method as a parameter in Java, you can use a functional interface and a lambda expression.
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.
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.
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.
To read a file into a String in Java, you can use the readAllBytes() method of the Files class and the new String() constructor:
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.
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.
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.
To allow the Java Virtual Machine (JVM) to be remotely debugged, you can use the following command line options:
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.
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.