How-to articles, tricks, and solutions about JAVA

Java Hashmap: How to get key from value?

To get the key for a given value in a java.util.HashMap, you can use the entrySet() method of the java.util.HashMap class to get a set of the mappings in the map, and then iterate over the set and check the value of each mapping.

Java HTTPS client certificate authentication

To perform HTTPS client certificate authentication in Java, you can use the HttpsURLConnection class and the SSLSocketFactory class.

Java Initialize an int array in a constructor

To initialize an int array in a constructor in Java, you can use an initializer list as follows:

Java inner class and static nested class

In Java, an inner class is a class that is defined within another class. An inner class has access to the members (fields and methods) of the outer class, and can be used to encapsulate the implementation of a component of the outer class.

Java int to String - Integer.toString(i) vs new Integer(i).toString()

In Java, there are several ways to convert an int value to a String:

Java List.contains(Object with field value equal to x)

To check if a Java List contains an object with a specific field value, you can use the List.stream().anyMatch() method along with a lambda expression to filter the elements in the list based on the field value.

Java Pass Method as Parameter

To pass a method as a parameter in Java, you can use a functional interface and a lambda expression.

Java recursive Fibonacci sequence

To implement a recursive Fibonacci sequence in Java, you can define a recursive method that returns the nth number in the sequence.

Java ResultSet how to check if there are any results

To check if a ResultSet object contains any results in Java, you can use the next method of the ResultSet class.

Java Security: Illegal key size or default parameters?

If you receive the error "Illegal key size or default parameters" in Java, it means that you are trying to use a cryptographic algorithm with a key size that is not allowed by the jurisdiction policy files.

Java String array: is there a size of method?

In Java, you can use the length field of an array to determine the size of the array. For example, given an array arr, you can get the size of the array using arr.length.

Java String new line

In JavaScript, you can use the following special characters to add a new line in a string:

Java string split with "." (dot)

To split a string in Java using a dot (.) as the delimiter, you can use the split method of the String class.

Java string to date conversion

To convert a string to a date in Java, you can use the parse method of the SimpleDateFormat class.

Java URL encoding of query string parameters

To URL encode the query string parameters of a URL in Java, you can use the URLEncoder class from the java.net package.

Java, How do I get current index/key in "for each" loop

To get the current index/key in a "for each" loop, you can use the for loop instead.

Java; String replace (school project)?

To replace a substring in a string in Java, you can use the replace() method of the String class.

Java: convert List<String> to a join()d String

To convert a List&lt;String&gt; to a single string by joining the elements of the list with a separator, you can use the join() method of the java.util.StringJoiner class.

Java: Get first item from a collection

To get the first item from a collection in Java, you can use the iterator() method to get an iterator for the collection, and then call the next() method on the iterator to get the first element.

Java: Get month Integer from Date

To get the month integer from a java.util.Date object in Java, you can use the getMonth() method of the java.util.Calendar class.

Java: how can I split an ArrayList in multiple small ArrayLists?

Here is an example of how you can split an ArrayList into multiple smaller ArrayLists in Java:

Java: How to get input from System.console()

To get input from the System.console() in Java, you can use the readLine() method of the Console class.

Java: how to initialize String[]?

There are several ways to initialize a String array in Java. Here are a few examples:

Java: How to read a text file

To read a text file in Java, you can use the BufferedReader class from the java.io package.

Java: method to get position of a match in a String?

To get the position of a match in a String in Java, you can use the indexOf() method of the String class.