h1

Java 8 – Streams Basics to Intermediate – Part 1

September 3, 2020

This is a part 1 of a series of posts I am planning to publish of Java Streams API, Predicates and Optional. Assumption here is you are starting off with Java 8 and are new to all these features. So the posts will start off with covering very basic concepts and then build upon them as we go along. I will also try to cover some other features in between involving method references, default methods, please read along and provide your feedback.

Streams

  • Converting a list to a map
Assume we have a list of numbers and we want to return a map of number to boolean representing if the number is even or not. But this can be easily achieved using streams in a single line as well.

Using traditional forEach

private static Map<Integer, Boolean> convertWithoutUsingStreams(List<Integer> numbers) {

    Map<Integer, Boolean> map = new HashMap<>();

    numbers.forEach(e -> map.put(e, isEven(e)));

    return map;

}

Using streams instead

private static Map<Integer, Boolean> convertUsingStreams(List<Integer> numbers) {

    return numbers.stream().collect(Collectors.toMap(e -> e, e -> isEven(e)));
}
The benefits of using streams does not stop there. You can use parallel streams when dealing with large collections. Of course, you should make sure of using parallelism carefully.

Using parallel stream

private static Map<Integer, Boolean> convertUsingParallelStreams(List<Integer> numbers) {

    return numbers.parallelStream().collect(Collectors.toMap(e -> e, e -> isEven(e)));

}
To see parallelism in action, take a look at this implementation of isEven method –
private static Boolean isEven(Integer number) {

    System.out.println(String.format("Current threadId : %s", Thread.currentThread().getId()));

    return number % 2 ==0;

}

Output using stream –

As can be seen, the only thread being used is the main thread when using stream method on list.
Current threadId : 1

Current threadId : 1

Current threadId : 1

Current threadId : 1

Current threadId : 1

Current threadId : 1

Current threadId : 1

Current threadId : 1

Current threadId : 1

Current threadId : 1

Current threadId : 1

{100=true, 101=false, 102=true, 103=false, 104=true, 105=false, 106=true, 107=false, 108=true, 109=false, 110=true}

Output using parallelStreams –

In this case, the isEven method is getting invoked on various threads, including the main thread (threadId = 1).
Current threadId : 1

Current threadId : 9

Current threadId : 1

Current threadId : 9

Current threadId : 10

Current threadId : 9

Current threadId : 10

Current threadId : 9

Current threadId : 11

Current threadId : 10

Current threadId : 1

{100=true, 101=false, 102=true, 103=false, 104=true, 105=false, 106=true, 107=false, 108=true, 109=false, 110=true}
h1

Maven and GWT can live together

June 18, 2010

I recently started working on a requirement where the UI was to be developed using GWT. Being a maven fan for quite sometime now, I started using gwt-maven-plugin to create a GWT maven plugin. And ofcourse everything was fine till the point I started adding external maven dependencies. GWT compile started complaining about the source not found for tons of classes. I googled around and find out the only way to get around the issue was to use GWT modules for external dependencies and inherit them in main GWT UI application. This approach works flawlessly if you are working with project created in Eclipse using the GWT web project wizard and other dependent projects are standard Eclipse Java project. This approach wasn’t supposed to work, as most of our applications or artifacts are maven based project. On further googling and downloading the source code of gwt-maven-plugin it turns out there is another version of gwt-maven-plugin available on svn repository of GWT project itself. It’s version is 1.3.1.google. Also there is another configuration that needs to be added in gwt-maven-plugin configuration namely “configureSourcesArtifacts” for all the dependencies you have source or is downloadable from the maven repository. You can find more detailed explanation of this configuration on the gwt-maven-plugin site under gwt:compile goal. Once you setup this configuration running mvn compile gwt:compile will be able to compile the GWT project for all permutations with ease.

Please contact me in case you need a sample pom file which I am using for my project.

Happy mavenizing GWT 🙂

h1

Netbeans 6.8 UML support

January 30, 2010

Netbeans has been gaining a lot of popularity over last decade or so and definitely has become forerunner in becoming número UNO in open source development community specially for Java and JEE.

I have been following Netbeans release since 6.x and one of the feature that I really liked was the UML/UML2 diagrams, code generation and reverse engineering. This plugin stayed with the distribution for quite sometime but for some reason this plugin is not supported in the latest release 6.8.

What does that mean to you or me?
Well if you like traditional way of building apps where you first design and then use netbeans to generate the code for you, you are doomed. No longer would your existing uml projects open up, you would not be able to forward or reverse engineer code from/to model.

There is a plugin which Netbeans recommend for UML modelling but if you like the native UI of netebans UML then once again you are doomed.

I wanted to have same UML plugin to work with and the good news I was able to find a workaround which works like a charm. I had two installations of netbeans 6.8 and 6.7 which did suported the UML model projects. All I had to do was copy the “uml” folder from the netbeans 6.7 install directory to 6.8 install directory and voilà I could now create uml projects in 6.8 and open existing uml projects in 6.8 IDE.

Happy modelling.

h1

JPA2 Junit4 testing with HSQLDB as in-memory database

January 25, 2010

Followed the steps from this great article –

http://antoniogoncalves.org/xwiki/bin/view/Article/TestingJPA
Only issue is that this article uses EclipseLink instead of hibernate implementation of JPA.
If you are using Netbeans 6.8 and you generated the pom file for the project, please perform the following steps –
1. Comment out the following dependency
javax
javaee-api
6.0
provided
or
javax
javaee-web-api
6.0
provided
(depending upon the type of project you created, web or ejb)
2. Add the following exclusion from hibernate-entitymanager artifact
org.hibernate
ejb3-persistence
3. For latest bean validation jsr add the following artifact
org.glassfish
bean-validator
3.0-JBoss-4.0.2_02
and for the persistence apis add the following dependency
javax.persistence
persistence-api
1.0
4. Comment out ejb3-persistence api artifact
org.hibernate
ejb3-persistence
1.0.1.GA
But of course you would have to add the test level dependency for HSQLDB and update the Junit version from old 3.8.2 to latest 4.5
Also for DBunit and dates keep this note handy

DbUnit use the JDBC escape formats for string representation.

Type Format
DATE yyyy-mm-dd
TIME hh:mm:ss
TIMESTAMP yyyy-mm-dd hh:mm:ss.fffffffff
Happy Testing
h1

Too Express or not too express

December 18, 2009

I have been a fan of apple products since my first stint with the first edition of IPod way back in 2005. Recently I have been debating with myslef to setup wireless network for all my devices including two HP laptops and recently purchased two IPhones using either Airport Express/Extreme or a standard linksys or dlink router. Thinking that nothing could go wrong with any of the options I thought of going with Apple Express. The manual says you can setup a wireless network with ease. Belive me it WASN’T. And before I say anything bad about the product I must say this once you have it working you will not regret your descison. Depending upon what problem you have with the initial setup the solution can be different but here I am going to discuss the issue I had and I belive most of the people had (atleast from the reviews on Amazon) is the setting up your new wireless connection which is a pain in the butt. According to the manual you should connect all your wires to the Express before using the Airport Utility. Having done that I thought creating the network should not take long. Actually it shouldn’t have if Apple moved one line from there manual on page 32 to 12. Instead of keeping your DSL powered on while you connect your wires to Express you should power off your DSL modem connect wires to your Express, power on your Express, wait for around 10 seconds and power up your DSL modem. Instead of seeing cool flashing amber light you will see annoying solid green light indicating your Express was able to connect to internet and now you can setup your wireless in express mode. And you will be on board with your Express.

h1

Do people relate “out of the box” with java

December 16, 2009

I don’t why but I thought it will be a fun little excercise to develop a “Hello world” application using jsf2, code name Mojarra. Since most of my current development is on Jboss AS, I thought I would use it as my app container. Nothing to fancy. Since I was in the mood of experimentations I thouht I would use Netbeans instead of regular Eclipse. I set my app server on Netbeans ran the wizard to create a web project using jsf framework. So far so good. Right after that nothing worked. Deployment of a the war failed on number of occassions no matter what configurations I tweaked. Changed jsf libraries to use latest mojarra libraries, used the same libraries with netbeans project (new project). I even went back to my favorite IDE Eclipse and as you might have guessed that didn’t help either. Don’t get me wrong here I am a hard core java/jee developer and I do know my environment quite well. This post I guess is more of a frustration that working on such a powerful programming language and platform requires so much of tweaking of things all over the place starting from IDE to the app container.
I will keep posting my findings until I have found a solution for creating a “Hello World” application.