How-to articles, tricks, and solutions about JAVA

Functional style of Java 8's Optional.ifPresent and if-not-Present?

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.

Generating all permutations of a given string

To generate all permutations of a given string in Java, you can use a recursive approach.

Get a JSON object from a HTTP response

To get a JSON object from a HTTP response in Java, you can use the JSONObject class from the org.json library.

Get an OutputStream into a String

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.

Get generic type of class at runtime

If you want to get the generic type of a class at runtime, you can use the Type class in the System namespace.

Get integer value of the current year in Java

To get the integer value of the current year in Java, you can use the Calendar class and the get method.

Get list of JSON objects with Spring RestTemplate

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.

Get only part of an Array in Java?

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.

Get specific ArrayList item

To get a specific item from an ArrayList in Java, you can use the get() method of the ArrayList class.

Get string character by index

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.

get string value from HashMap depending on key name

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.

Get the POST request body from HttpServletRequest

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.

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.

Getting an element from a Set

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.

Getting java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory exception

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.

Getting java.net.SocketTimeoutException: Connection timed out in android

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.

Getting Keyboard Input

To get keyboard input in Java, you can use the Scanner class from the java.util package.

Getting the array length of a 2D array in Java

To get the length of a 2D array in Java, you can use the length field of the array.

Getting the filenames of all files in a folder

To get the filenames of all files in a folder in Java, you can use the File class and its list method.

Getting the IP address of the current machine using Java

To get the IP address of the current machine using Java, you can use the InetAddress.getLocalHost method of the java.net package.

Getting the name of the currently executing method

To get the name of the currently executing method in Java, you can use the Thread.currentThread().getStackTrace() method.

Global variables in Java

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.

Good examples using java.util.logging

Here are some examples of how to use the java.util.logging package to log messages in Java:

Good Hash Function for Strings

A good hash function for strings should have the following properties:

Gradle build without tests

To build a Gradle project without running the tests, you can use the assemble task instead of the build task.