How-to articles, tricks, and solutions about JAVA
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.
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.
In JUnit, you can use the getClass().getClassLoader().getResource("").getPath() method to get the path of the src/test/resources directory.
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.
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:
To get the values and keys from a HashMap in Java, you can use the values and keySet methods.
To hash a string using SHA-256 in Java, you can use the MessageDigest class from the java.security package.
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.
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.
To import a JAR file into Eclipse, follow these steps:
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).
You can initialize an ArrayList in one line in Java using the following syntax:
There are several ways to initialize an array in Java. Here are some examples: Using the new operator:
Here is an example of how you can initialize an array of objects in Java:
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.
To initialize a List<String> object in Java, you can use one of the following approaches: Using the new keyword:
To install Java 8 on macOS, follow these steps:
To install JDK 11 under Ubuntu, follow these steps:
To install the JDK (Java Development Kit) on Ubuntu Linux, follow these steps:
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.
To launch an activity from another application in Android, you can use an Intent with the FLAG_ACTIVITY_NEW_TASK flag set.
In Java, you can create a new list using the java.util.ArrayList class or the java.util.LinkedList class.
To match "any character" in a regular expression, you can use the . (dot) character. The . character matches any single character, except for a newline.
To mock a final class with Mockito, you can use the PowerMockito library.
To mock a void method with Mockito, you can use the doAnswer method. Here is an example of how you can use it: