How-to articles, tricks, and solutions about JAVA

Java - get the current class name?

To get the current class name in Java, you can use the getClass() method of the Object class, which returns a Class object that represents the runtime class of the object.

Java - How to create new Entry (key, value)

To create a new key-value pair in a Map in Java, you can use the put method.

Java - removing first character of a string

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

Java - sending HTTP parameters via POST method easily

To send HTTP parameters via the POST method in Java, you can use the java.net.URL and java.net.HttpURLConnection classes. Here is an example of how you can do this:

Java - What does "\n" mean?

In Java, the string "\n" is a newline character.

Java 11 package javax.xml.bind does not exist

If you are seeing the error "package javax.xml.bind does not exist" in your Java 11 project, it means that the Java XML Bind (JAXB) API is not included in the classpath.

Java 256-bit AES Password-Based Encryption

In Java, you can use the javax.crypto package to perform 256-bit AES (Advanced Encryption Standard) encryption.

Java 8 Distinct by property

To get a list of distinct elements by a property in Java 8, you can use the distinct() method of the Stream interface and the map() method to extract the property from each element.

Java 8 Iterable.forEach() vs foreach loop

In Java 8, the Iterable.forEach() method is a default method that allows you to iterate over the elements of an Iterable (such as a List or Set) and perform a specific action on each element.

Java 8 Lambda function that throws exception?

In Java 8, you can use a lambda expression to create a functional interface that throws an exception.

Java 8 List<V> into Map<K, V>

To convert a List<V> into a Map<K, V> in Java 8, you can use the toMap() method of the Collectors class from the java.util.stream package.

Java 8: Difference between two LocalDateTime in multiple units

To find the difference between two LocalDateTime objects in multiple units, you can use the Duration class.

Java Array Sort descending?

To sort an array in descending order in Java, you can use the Arrays.sort method and pass it a comparator that compares the elements in reverse order.

Java ArrayList copy

There are several ways to create a copy of an ArrayList in Java:

Java Byte Array to String to Byte Array

To convert a byte array to a string and back to a byte array in Java, you can use the getBytes method of the String class and the getBytes method of the Charset class.

Java code for getting current time

To get the current time in Java, you can use the Instant class from the java.time package.

Java Compare Two Lists

To compare two lists in Java, you can use the equals() method of the List interface.

Java default constructor

In Java, a default constructor is a constructor that is automatically generated by the compiler if no other constructors are defined in a class.

Java Does Not Equal (!=) Not Working? [duplicate]

It is possible that the != operator is not working as expected in your Java code because you are using it to compare object references, rather than the contents of the objects themselves.

Java dynamic array sizes?

In Java, you can implement a dynamic array (i.e., an array that can grow and shrink as needed) by using an ArrayList or an ArrayDeque.

Java Error: "Your security settings have blocked a local application from running"

If you are getting the error "Your security settings have blocked a local application from running" when trying to run a Java application, it means that your security settings are preventing the application from running.

Java FileOutputStream Create File if not exists

To create a file using FileOutputStream in Java if it does not exist, you can use the following code:

Java format yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss

To convert a date in the format yyyy-MM-dd'T'HH:mm:ss.SSSz to the format yyyy-mm-dd HH:mm:ss, you can use the SimpleDateFormat class in Java.

Java Generate Random Number Between Two Given Values

To generate a random number between two given values in Java, you can use the nextInt method of the java.util.Random class. For example: