java

recent posts related to java, jdbc, spring, etc.

A Java method to round a float value to the nearest one-half value

As a quick note, here’s a Java method that will round a float to the nearest half value, such as 1.0, 1.5, 2.0, 2.5, etc.:

/**
 * converts as follows:
 * 1.1  -> 1.0
 * 1.3  -> 1.5
 * 2.1  -> 2.0
 * 2.25 -> 2.5
 */
public static float roundToHalf(float f) {
    return Math.round(f * 2) / 2.0f;
}

The comments show how this function converts the example float values to their nearest half value, so I won’t add any more comments here.

How to iterate (loop) over the elements in a Map in Java 8

If you need to iterate over the elements in a Map in Java 8, this source code shows how to do it:

Map<String, String> map = new HashMap<String, String>();
map.put("first_name", "Alvin");
map.put("last_name",  "Alexander");

// java 8
map.forEach((k,v)->System.out.println("key: " + k + ", value: " + v));
Modern garbage collection alvin January 28, 2017 - 10:52am

From the article: “The reality is that Go’s GC does not really implement any new ideas or research. As their announcement admits, it is a straightforward concurrent mark/sweep collector based on ideas from the 1970s. It is notable only because it has been designed to optimise for pause times at the cost of absolutely every other desirable characteristic in a GC.

A Java tuple class (Tuple2 or Pair, if you prefer)

After working with Scala for a long time, I had to come back to Java for a while to work on an Android app. Right away I missed a lot of things from the Scala world, including all of the built-in Scala collection methods, and other things as simple as the Scala Tuple classes.

If you haven’t used them before, a Scala Tuple class lets you write code like this:

Tuple<String, Integer> t = new Tuple<>("age", 41);

If you’re comfortable with generics, the Java implementation of a Tuple class like this is simple:

How to populate a Java int array with a range of values

I just learned an easy way to populate/initialize a Java int array with data, such as a range of numbers. The key is to use the rangeClosed method on the Java 8 IntStream class. Here’s an example using the Scala REPL:

scala> val n = java.util.stream.IntStream.rangeClosed(0, 10).toArray()
n: Array[Int] = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

I show that in Scala to show the output, and here’s what it looks like with Java:

You may not need to use StringBuilder in Java any more alvin December 23, 2016 - 12:13pm

Here’s a short article on dzone.com that explains why you might not need to use StringBuilder in Java any more.

Java int, double, float, and mixed-type arithmetic rules

Table of Contents1 - The answer2 - More Java division and arithmetic rules3 - Summary

Java FAQ: What are the rules about Java arithmetic (multiplication, division) involving mixed data types?

While working on a math problem in Java just a little while ago, I realized that I wasn’t comfortable with the Java mixed-type division rules. That is, I wondered if the result of this equation:

3 / 2

the same as the result of this equation:

3 / 2.0

or this equation:

3.0 / 2.0

How to use multiple regex patterns with replaceAll (Java String class)

Table of Contents1 - 1) A simple string2 - 2) Replace multiple patterns in that string3 - 3) More explanation4 - Multiple search patterns5 - Summary

Java FAQ: How can I use multiple regular expression patterns with the replaceAll method in the Java String class?

Here’s a little example that shows how to replace many regular expression (regex) patterns with one replacement string in Scala and Java. I’ll show all of this code in Scala’s interactive interpreter environment, but in this case Scala is very similar to Java, so the initial solution can easily be converted to Java.