How-to articles, tricks, and solutions about JAVA

Why do I get an UnsupportedOperationException when trying to remove an element from a List?

The java.lang.UnsupportedOperationException is thrown when an operation is not supported by a class.

What's the difference between JPA and Hibernate?

Java Persistence API (JPA) is a specification for object-relational mapping (ORM) in Java.

How do I set environment variables from Java?

To set an environment variable from Java, you can use the System.setProperty() method.

JPA JoinColumn vs mappedBy

In a JPA entity mapping, the @JoinColumn annotation is used to specify the foreign key column for a many-to-one or one-to-one relationship.

Best way to create enum of strings?

The best way to create an enum of strings in Java is to define the enum type with a String field, and then specify the possible values for the enum as string literals.

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 check if my string is equal to null?

To check if a string is equal to null in Java, you can use the == operator.

How to create correct JSONArray in Java using JSONObject

To create a JSON array using JSONObject in Java, you can use the put() method of the JSONObject class to add elements to the array.

Import a custom class in Java

To use a custom class in your Java code, you will need to import it at the beginning of your source file.

I need to convert an int variable to double

To convert an int to a double, you can use the doubleValue() method of the Integer class.

Date format Mapping to JSON Jackson

You can use the @JsonFormat annotation to specify the format of date fields in a JSON payload when serializing or deserializing using Jackson.

Spring cron expression for every day 1:01:am

To create a cron expression that triggers an event every day at 1:01 AM, you can use the following cron expression:

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.

How to check internet access on Android? InetAddress never times out

To check for internet access on Android, you can use the isReachable() method of the InetAddress class.

How do I print my Java object without getting "SomeType@2f92e0f4"?

To print a Java object in a more readable format, you can override the toString() method in your class.

Integer division: How do you produce a double?

In Java, integer division always produces an integer result, even if the dividend is a floating-point number.

How to sum a list of integers with java streams?

You can use the reduce() operation in the Java Streams API to sum the elements of a list of integers.

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.

How do you return a JSON object from a Java Servlet

To return a JSON object from a Java Servlet, you can use the following steps:

How to capitalize the first character of each word in a string

There are a few different approaches you can take to capitalize the first character of each word in a string in Java.

Why would a "java.net.ConnectException: Connection timed out" exception occur when URL is up?

A java.net.ConnectException: Connection timed out error can occur when a Java application is unable to establish a network connection to the specified host.

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

A java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) error typically indicates that the MySQL server is unable to authenticate the user with the provided username and password.

java.net.SocketTimeoutException: Read timed out under Tomcat

A java.net.SocketTimeoutException: Read timed out error can occur when a connection to a server is blocked by a firewall or if the server is experiencing high load and is unable to process requests in a timely manner.

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.

Page scroll up or down in Selenium WebDriver (Selenium 2) using java

To scroll up or down in a web page using Selenium WebDriver in Java, you can use the JavascriptExecutor interface and the scrollBy method.