How-to articles, tricks, and solutions about JAVA

Good Hash Function for Strings

A good hash function for strings should have the following properties:

Java Compare Two Lists

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

Android changing Floating Action Button color

To change the color of a Floating Action Button (FAB) in Android, you can use the setBackgroundTintList() method and pass it a color state list.

Number of lines in a file in Java

To get the number of lines in a file in Java, you can use the BufferedReader class and read the file line by line until the end of the file is reached.

Deleting an object in java?

In Java, objects are automatically eligible for garbage collection when they are no longer reachable.

Explanation of 'String args[]' and static in 'public static void main(String[] args)'

In the main() method in Java, String args[] is an array of strings that holds the command-line arguments passed to the program.

Why are interface variables static and final by default?

In Java, interface variables are static and final by default because that is how they are defined in the Java Language Specification (JLS).

How to get row count using ResultSet in Java?

To get the row count using a ResultSet object in Java, you can use the last() method to move the cursor to the last row, and then use the getRow() method to get the row number:

Tomcat Server Error - Port 8080 already in use

If you see the error "Port 8080 already in use" when starting the Tomcat server, it means that another process is already using port 8080 on your machine.

Java HTTPS client certificate authentication

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

Installing Java 7 on Ubuntu

To install Java 7 on Ubuntu, you can follow these steps:

POST request via RestTemplate in JSON

To make a POST request with the RestTemplate in JSON, you can use the postForObject() method and pass it the URL of the request, the request body, the response type, and the HttpEntity object that represents the request headers and body.

Type List vs type ArrayList in Java

In Java, List is an interface that defines a list data structure, while ArrayList is a class that implements the List interface.

Remote debugging a Java application

To remotely debug a Java application, you need to start the application with the java command and the -agentlib:jdwp option.

How do I concatenate two strings in Java?

To concatenate two strings in Java, you can use the + operator.

:: (double colon) operator in Java 8

In Java 8, the :: (double colon) operator is used to refer to a method or a constructor.

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.

Understanding Spring @Autowired usage

The @Autowired annotation in Spring is used to inject dependencies into a Java object.

Simple way to count character occurrences in a string

To count the occurrences of a character in a string in Java, you can use the charAt() method of the String class to iterate over the characters in the string and check if each character is equal to the target character.

Convert an integer to an array of digits

To convert an integer to an array of digits in Java, you can use the toCharArray() method of the String class to convert the integer to a string, and then use the toCharArray() method to convert the string to an array of characters.

How do I parse command line arguments in Java?

To parse command line arguments in Java, you can use the main() method of your application's entry point class.

How to check String in response body with mockMvc

To check a string in the response body with MockMvc, you can use the andExpect() method of the MockMvcResultMatchers class.

Data access object (DAO) in Java

In Java, a data access object (DAO) is a design pattern that provides an abstract interface for accessing data from a database.

How to parse a date?

To parse a date in Java, you can use the SimpleDateFormat class.

How to run Unix shell script from Java code?

To run a Unix shell script from Java code, you can use the Runtime.getRuntime().exec() method to execute the script.