Enum to Integer and Integer to Enum in Java

November 5, 2015 by

Need to convert an integer to an enum in Java? The bad news is that it is not as easy as it should be, but the good news is that it is still easy! Consider the enum below. public enum PageType { ABOUT(1), CODING(2), DATABASES(3); private int value; private static Map map = new HashMap();… read more

Replacing Newline Characters in Java and JSTL

October 26, 2015 by

In this article, we will take a look at how we can replace newline characters in Java. Additionally, we will implement a custom JSTL tag so that we can replace newline characters with corresponding <br /> break lines within JSP files. Unlike PHP’s nl2br function, for instance, this is not as easy in JSTL. Replacing… read more

Generating a UUID in Java

October 13, 2015 by

Generating a UUID in Java is extremely easy. In fact, it can be done with a single line of code! To generate a type 4 UUID in Java, simply run the following line of code. String uuid = UUID.randomUUID().toString(); // Example: e03913fb-5951-4964-a88b-47371641fd17 Because you will likely generate UUIDs in many different contexts, here is a… read more

Generate CRC32 Checksum in Java

September 26, 2015 by

Generating a CRC32 checksum in Java is – as you would expect – extremely simple. All it takes is a few lines of code. Below is an example on how to generate a CRC32 checksum from a string. public final class AppUtils { private AppUtils() { } public static long crc32(String input) { byte[] bytes… read more

Updating Entities with Update Query in Spring Data JPA

March 25, 2015 by

With object-relational mapping (ORM) tools such as Hibernate, one usually modifies entities by first fetching them from the database, modifying some fields, and then persisting the entities again. This is a good approach in many cases, but in some scenarios this may not be desirable. For instance, if you want to update a collection of entities (i.e.… read more