How-to articles, tricks, and solutions about JAVA

Where is Java Installed on Mac OS X?

On Mac OS X, the default location for the JDK (Java Development Kit) is /Library/Java/JavaVirtualMachines/jdk<version>.jdk/Contents/Home/, where <version> is the version number of the JDK you have installed.

Examples of GoF Design Patterns in Java's core libraries

There are many examples of the GoF (Gang of Four) design patterns in the core libraries of Java. Here are some examples:

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.

Remove HTML tags from a String

To remove HTML tags from a string, you can use a regular expression to match and replace the tags with an empty string.

C# Java HashMap equivalent

In Java, the HashMap class is the equivalent of the Dictionary class in C#. It is a map data structure that stores (key, value) pairs and provides fast lookup and insertion of elements.

How do I get a Date without time in Java?

To get a java.util.Date object with the time set to 00:00:00 (midnight), you can use the toInstant() method to convert a LocalDate object to an Instant, and then use the atZone() method to convert the Instant to a ZonedDateTime object, and finally use the

Difference between DTO, VO, POJO, JavaBeans?

In Java, DTO stands for Data Transfer Object, and it is a simple object that is used to carry data between processes. DTOs are often used when transferring data over a network, or between layers of an application. They typically have private fields, and g

Find first element by predicate

To find the first element in a list that matches a certain condition, you can use the stream() method to create a stream from the list, and then use the filter() method to specify the condition that the element should satisfy. Finally, you can use the fin

How does the "final" keyword in Java work? (I can still modify an object.)

In Java, the final keyword can be used in several contexts to declare that something cannot be changed.

Removing an element from an Array in Java

There are a few different ways you can remove an element from an array in Java. Here are a few options:

How can I initialise a static Map?

To initialize a static Map in Java, you can use the Map interface's of method, which was added in Java 9. The of method creates an immutable Map with a fixed set of key-value pairs.

How to configure port for a Spring Boot application

To configure the port for a Spring Boot application, you can use the server.port property in the application's configuration file. The configuration file can be a application.properties file in the classpath, or a application.yml file in the classpath.

What is reflection and why is it useful?

Reflection is a feature of the Java language that allows you to inspect and manipulate classes, fields, and methods at runtime.

Static Classes In Java

In Java, a static class is a class that can be accessed without an instance of the class. A static class is defined by adding the static keyword to the class declaration.

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 do I invoke a Java method when given the method name as a string?

To invoke a Java method when given the method name as a string, you can use reflection. Reflection is a feature of the Java language that allows you to inspect and manipulate classes, fields, and methods at runtime.

Why am I getting a NoClassDefFoundError in Java?

A NoClassDefFoundError in Java indicates that the Java Virtual Machine (JVM) or a ClassLoader was not able to find the definition of a class that was referenced in your code. This can occur for a variety of reasons, including:

Convert Set to List without creating new List

To convert a Set to a List in Java without creating a new List object, you can use the List constructor that takes a Collection as an argument.

Sending HTTP POST Request In Java

To send an HTTP POST request in Java, you can use the java.net.URL and java.net.HttpURLConnection classes. Here is an example of how you can use these classes to send a POST request:

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:

What's the syntax for mod in java

The syntax for mod in Java is the percent sign %. For example, a % b returns the remainder of a divided by b.

How to create JSON Object using String?

To create a JSON object from a string in Java, you can use the org.json library. Here's an example of how to do this:

How to convert List to Map?

To convert a List to a Map in Java, you can use the stream() method and the collect() method along with a Collectors.toMap() call. Here's an example of how to do this:

JAX-RS — How to return JSON and HTTP status code together?

In JAX-RS, you can use the Response class from the javax.ws.rs.core package to return a JSON response and an HTTP status code together. Here's an example of how to do this: