How-to articles, tricks, and solutions about JAVA
The ifPresent() and ifPresentOrElse() methods of the Optional class in Java 8 provide a functional style way to perform different actions depending on whether the Optional object is empty or contains a value.
To generate all permutations of a given string in Java, you can use a recursive approach.
To get a JSON object from a HTTP response in Java, you can use the JSONObject class from the org.json library.
To get an OutputStream into a String, you can use a ByteArrayOutputStream as the OutputStream and then use the toString() method of the ByteArrayOutputStream to get the contents of the stream as a string.
If you want to get the generic type of a class at runtime, you can use the Type class in the System namespace.
To get the integer value of the current year in Java, you can use the Calendar class and the get method.
To get a list of JSON objects using the Spring RestTemplate, you can use the exchange() method to send an HTTP GET request to the server, and then use the getBody() method of the ResponseEntity object to retrieve the list of objects.
To get only a part of an array in Java, you can use the Arrays.copyOfRange() method, or you can use the System.arraycopy() method.
To get a specific item from an ArrayList in Java, you can use the get() method of the ArrayList class.
In Java, you can get a character at a specific index in a string using the charAt() method of the String class. This method takes an index as an argument and returns the character at that index.
To get the string value from a HashMap depending on the key name in Java, you can use the get() method of the Map interface.
To get the POST request body from an HttpServletRequest object in Java, you can use the getReader method of the ServletRequest interface to read the request body as a BufferedReader and then use the readLine method to read the data as a string.
To get a file's MD5 checksum in Java, you can use the MessageDigest class from the java.security package.
A Set is a collection of elements in which each element can only occur once. A Set does not have a specific order and does not provide a way to access its elements by their position.
The java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory exception is thrown when a Java application is unable to find the LogFactory class from the Apache Commons Logging library.
The java.net.SocketTimeoutException: Connection timed out error usually occurs when a client is trying to connect to a server, but the connection request is taking too long to complete.
To get keyboard input in Java, you can use the Scanner class from the java.util package.
To get the length of a 2D array in Java, you can use the length field of the array.
To get the filenames of all files in a folder in Java, you can use the File class and its list method.
To get the IP address of the current machine using Java, you can use the InetAddress.getLocalHost method of the java.net package.
To get the name of the currently executing method in Java, you can use the Thread.currentThread().getStackTrace() method.
In Java, a global variable is a variable that is accessible from any part of the program. In Java, there is no such thing as a true global variable, as all variables must be declared within a class and are only accessible within the scope of that class.
Here are some examples of how to use the java.util.logging package to log messages in Java:
A good hash function for strings should have the following properties:
To build a Gradle project without running the tests, you can use the assemble task instead of the build task.