java 8

The Java 8 lambda Thread and Runnable syntax and examples alvin August 19, 2017 - 12:55pm

As a quick note, here are some examples of the Java 8 lambda Thread and Runnable syntax.

Java 8 Thread/Runnable lambda syntax

First, here’s the Java 8 lambda syntax for a Runnable, where I create a Runnable and pass it to a Thread:

Android Room, database I/O, and Java 8 threads

I just started working with the Android Room database persistence library, and since you’re not supposed to run things like database queries on the main thread (the UI thread), I was looking at other ways to run them.

In general, you probably won’t want to run database queries using a Thread, but just to see how Room works, I wrote this Java Thread code, and confirmed that it works as expected:

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));

Blurry text in a Mac OS X Java Swing application (solution)

I noticed recently that a Scala/Java Swing application I am developing on Mac OS X 10.9 has blurry text when it’s run as a Mac application. The text looks fine when I run the application through SBT, but looks blurry when I package it and run it as a Mac OS X application.

A solution is to put this text in the application’s Info.plist file:

Java architect's thoughts on Java 7 and Java 8

I just ran across this link in my email about the Chief Architect of the Java Platform Group at Oracle, with his thoughts about how to proceed on the Java 7 and Java 8 platforms releases. I haven't paid attention to the "New Java Features" world in a while, but it's a short, interesting read about how they're thinking about proceeding with the Java 7 and Java 8 platform releases.

Java 5 for loop syntax example (Java 5 Generics)

Java FAQ: Can you show me an example of the Java 5 for loop syntax? (Java 5 and newer)

Answer: Sure. I've created a sample Java program to demonstrate the Java 5 for-each loop syntax, using both a List with pre-Java5 syntax, and a second example using Java 5 generics syntax.

Java 5 for loop syntax example

Without any further ado, here is the example code: