How-to articles, tricks, and solutions about JAVA

How to get the last value of an ArrayList

To get the last value of an ArrayList in Java, you can use the size() method to get the size of the list and then access the value at the index size() - 1.

How to get the path of a running JAR file?

To get the path of a running JAR file, you can use the java.lang.Class class's getProtectionDomain method and then the getCodeSource method.

How to get the path of src/test/resources directory in JUnit?

In JUnit, you can use the getClass().getClassLoader().getResource("").getPath() method to get the path of the src/test/resources directory.

How to get the separate digits of an int number?

To get the separate digits of an integer number, you can convert the number to a string, and then use the list() function to convert the string to a list of characters.

How to get the user input in Java?

To get user input in Java, you can use the Scanner class. Here's an example of how to get a string input from the user:

How to get values and keys from HashMap?

To get the values and keys from a HashMap in Java, you can use the values and keySet methods.

How to hash some String with SHA-256 in Java?

To hash a string using SHA-256 in Java, you can use the MessageDigest class from the java.security package.

How to implement a tree data-structure in Java?

A tree is a data structure that consists of nodes arranged in a hierarchy. Each node has a value and may have zero or more child nodes. The top node in a tree is called the root node.

How to import a .cer certificate into a java keystore?

To import a .cer certificate into a Java keystore, you can use the keytool utility that comes with the Java Development Kit (JDK). The keytool utility is a command-line tool that allows you to manage certificates and keystores.

How to import a jar in Eclipse?

To import a JAR file into Eclipse, follow these steps:

How to import an existing X.509 certificate and private key in Java keystore to use in SSL?

To import an existing X.509 certificate and private key into a Java keystore, you can use the keytool utility that is included with the Java Development Kit (JDK).

How to Initialization of an ArrayList in one line in Java

You can initialize an ArrayList in one line in Java using the following syntax:

How to initialize an array in Java?

There are several ways to initialize an array in Java. Here are some examples: Using the new operator:

How to initialize an array of objects in Java

Here is an example of how you can initialize an array of objects in Java:

How to initialize HashSet values by construction?

To initialize the values of a HashSet when constructing the set, you can use one of the HashSet constructors that takes a Collection as an argument.

How to initialize List<String> object in Java?

To initialize a List<String> object in Java, you can use one of the following approaches: Using the new keyword:

How to install Java 8 on Mac

To install Java 8 on macOS, follow these steps:

How to install JDK 11 under Ubuntu?

To install JDK 11 under Ubuntu, follow these steps:

How to install the JDK on Ubuntu Linux

To install the JDK (Java Development Kit) on Ubuntu Linux, follow these steps:

How to iterate over a JSONObject?

To iterate over the key/value pairs in a JSONObject, you can use the keys method to get an iterator over the keys in the object, and then use the get method to get the value for each key.

How to launch an Activity from another Application in Android

To launch an activity from another application in Android, you can use an Intent with the FLAG_ACTIVITY_NEW_TASK flag set.

How to make a new List in Java

In Java, you can create a new list using the java.util.ArrayList class or the java.util.LinkedList class.

How to match "any character" in regular expression?

To match "any character" in a regular expression, you can use the . (dot) character. The . character matches any single character, except for a newline.

How to mock a final class with mockito

To mock a final class with Mockito, you can use the PowerMockito library.

How to mock void methods with Mockito

To mock a void method with Mockito, you can use the doAnswer method. Here is an example of how you can use it: