Java Lobby Announcements

Subscribe to Java Lobby Announcements feed
Recent posts in Java on
Updated: 7 hours 19 min ago

Java Holiday Calendar 2016 (Day 4): Use RemoveIf in Java Collections

Sun, 2016-12-04 12:01

Today's tip is to use the removeIf() method (that all collection classes like List have) rather than manually iterating over the elements and removing them. For large data sets, removeIf() can be orders of magnitudes faster than other methods. It also looks much better in your code. Why? Read more here and see for yourself! In the meantime, let's see how to put it to use.

Do This items.removeIf(i -> predicate(i));

Categories: Java

The $1 Solution to Avoid the Null Pointer Exception

Sun, 2016-12-04 07:31

This post is all about the null pointer exception, which we usually counter in our everyday programming. Before diving into the nitty-gritty of null, I want to go into why I named my post the $1 solution to avoid the null pointer exception.

Tony Hoare invented the null reference, later admitting that it was a billion-dollar mistake. Rather than making the same mistake of a null dollar solution, it would be apt to title my post the $1 dollar solution.

Categories: Java

Java Holiday Calendar 2016 (Day 3): Initializing Maps in the Smartest Way

Sat, 2016-12-03 12:01

Today's tip is how to initialize Java Maps in a type-safe way with Java 8. With Java 9, we will get even better ways of creating immutable Maps. Until then, by defining two utility methods...

public static <K, V> Entry<K, V> entry(K key, V value) { return new AbstractMap.SimpleEntry<>(key, value); } public static <K, U> Collector<Entry<K, U>, ?, Map<K, U>> entriesToMap() { return Collectors.toMap(Entry::getKey, Entry::getValue); }

Categories: Java

Lamba Expressions in Java 8

Sat, 2016-12-03 07:31

Lambda expressions are the most popular feature of Java 8. They introduce functional programming concepts to Java, which is a completely object-oriented and imperative programming language. How functional programming languages work is beyond the scope of this article, but we will extract a feature that will make the difference obvious for us who work with OOP.

How functional programming languages work is beyond the scope of this article, but we will extract a feature that will make the difference obvious to us who work with OOP.

Categories: Java

Discover Your Developer Fantasy Character in the &quot;State of the Developer Nation&quot; Survey

Fri, 2016-12-02 22:43

We’re proud to be supporting the State of the Developer Nation Survey run by our friends at VisionMobile! This is the 12th developer survey, focusing on a 360 view of developer tools, skills and salaries.

The survey features questions on topics like programming languages, platforms, app categories, new technologies, revenue models, IoT verticals - and of course - tools. It’s a survey made by developers, so the questions will be relevant plus you will get to learn something new - and it only takes 15 minutes!

Categories: Java

Java Holiday Calendar 2016 (Day 2): Favor Composition Over Inheritance

Fri, 2016-12-02 14:01

Today's tip is to avoid inheritance. For good reasons, there can only be one super class for any given Java class. Furthermore, exposing abstract or base classes in your API that are supposed to be inherited by client code is a very big and problematic API commitment. Avoid API inheritance altogether, and instead consider providing static interface methods that take one or several lambda parameters and apply those given lambdas to a default internal API implementation class.

This also creates a much clearer separation of concerns. For example, instead of inheriting from a public API class AbstractReader and overriding abstract void handleError(IOException ioe), it is better to expose a static method or a builder in the Reader interface that takes a Consumer<IOException> and applies it to an internal generic ReaderImpl.

Categories: Java

Inheritance vs. Composition [Not Comic]

Fri, 2016-12-02 12:01
Categories: Java

Merging Overlapping Intervals [Code Snippet]

Fri, 2016-12-02 10:31

I recently came across a problem of merging data with overlapping lower and upperbound data. I tried multiple approaches.

The below code uses the Double Linked List approach:

Categories: Java

Does Your Code Speak English?

Fri, 2016-12-02 09:01

No. How? What for? No, it is not a common feature yet. There are no standard libraries for natural language processing. You won't get good answers to natural language questions with both search engines and virtual assistants (like Siri). If you intend to integrate them in your application, you can use API for SiriGoogle Assistant, or Cortana. But only the fact there are several different types of API can discourage more than possible excitement from integration with naturally speaking assistants.

Another possibility is the Semantic Web. Let's write Hello World with it. At first, looks simple enough, just defining data, then querying with a SQL-like language. Fine, but why do we need to use URIs and triples (subject-object-predicate) for that? What’s about code? Not short and resembles XML processing. Maybe it'd be better to try human-readable notation? But here we see the "human-readable" URI again. Is dc:title human-readable? Yes, it may in this example, but another time it would be dc_fr12:ttl. Maybe, microformatting is the better choice? Possibly yes, if used within HTML, otherwise no.

Categories: Java

Using IDEA to Delegate Build And Run Actions To Gradle

Fri, 2016-12-02 07:31

IntelliJ IDEA 2016.3 introduces the option to delegate the IDE build and run actions to Gradle. So if we invoke the Build Project action from the Buildmenu IntelliJ IDEA invokes the correct tasks using Gradle. Also the Run and Debug actions from the Run menu are executed with Gradle.

If we want this behaviour we need to changed the preferences of IntelliJ IDEA. We must open the preferences dialog window and then go to Build, Execution, Deployment | Build Tools | Gradle | Runner. Here we check the option Delegate IDE build/run actions to gradle and we close the window:

Let's open a simple Java project with a Gradle build file in IDEA. Next we invoke the Build Project action with the shortcut key Cmd+F9 (on macOS, other operating systems probably have a different shortcut key). Our code is compiled and we can open the Run view to see the output:

Categories: Java

Redis Users Rejoice! The JCache API Is Here

Thu, 2016-12-01 12:01

The JCache API implementation for Redis has been expected by Java community since its publication. Finally, the day has come. The Redisson team now proudly presents to you our JCache API (JSR-107) implementation for Redis, an implementation based on the Redisson framework.

Let's get started with a few configuration examples. Redisson supports two configuration formats — JSON and YAML. If a configuration instance is not defined at runtime, then Redisson will look for /redisson-jcache.json or /redisson-jcache.yaml in the classpath.

Categories: Java

How to Infer Your Linux FD Numbers With Java (and Why)

Thu, 2016-12-01 08:31

In Badoo's BI, we use a combination of Exasol and Hadoop for ETL purposes. One of our common usage patterns is to export of some queries from Exasol to HDFS (for later analysis, or just for some kind of backup). The fastest and simpliest way is to dump query results from Exasol is to execute such a query:

EXPORT (SELECT * FROM fact_user_registration) INTO LOCAL CSV FILE '/local/tmp/user_reg.csv';

As a result, a table dump will appear on our HDD. After the file appears, it should be uploaded to HDFS. That's simple and easy to be implemented, but, when running in parallel, we may run out of disk space.

Categories: Java

Multi-Tenancy Using JPA, Spring, and Hibernate (Part 1)

Thu, 2016-12-01 07:31

Multi-tenancy allows an application to behave as multiple independent applications hosted for different clients (i.e. organizations). This might not sound impressive, however as the number of clients increase it becomes more evident that it is easier and more cost effective to run a single application hosted for all the clients rather than hosting an independent application for each client.

Multi-tenancy has become more popular lately and is very useful for the economy in software companies since it makes your service more affordable using cheaper installations/costs. This is because multi-tenancy can lead your business to a higher level of competitive differentiation.

Categories: Java

Spring Tips: Managing Distributed Transactions With JTA [Video]

Thu, 2016-12-01 04:01

Hi Spring fans! In this tip, we’ll look at how to manage distributed transactions in Spring with JTA.

Categories: Java

Java Holiday Calendar 2016: Use the @FunctionalInterface Annotation

Thu, 2016-12-01 03:01

As the holidays draw near, it's important to keep your skills sharp! That's why DZone MVB Per-Åke Minborg is going to be posting a quick tip a day through the holiday season to help keep your code fighting fit. To get things started, here's a lesson about the @FunctionalInterface annotation and why you should use it.

Today's tip is to annotate a functional interface with the @FunctionalInterface annotation, thereby signaling that API users may use lambdas to implement the interface. It also ensures that the interface remains usable for lambdas over time by preventing abstract methods from accidentally being added to the API later on.

Categories: Java

This Week in Spring: Reactive Programming and Springing to the Cloud

Wed, 2016-11-30 12:01

Welcome to another installment of This Week in Spring! I can’t believe how quickly this year has gone! This week I’m in Melbourne, Australia for the YOW! conference and then week it’s off to Brisbane and then Sydney for the next editions of the same show. Australia is the furthest I’ve ever been from my ‘native’ timezone — so even though I always post This Week in Spring every Tuesday, I appreciate that it’s still Monday for anybody west of Europe right now! Tonight, I’ll join my pal, IntelliJ’s Trisha Gee, and we’ll be speaking at the Melbourne JVM User Group. I’m super excited to be here, for my first time, helping bring the Spring down under. If you’re around, then say hi (@starbuxman)!

Categories: Java

7 Steps to Preparing for Java 8 Certification

Wed, 2016-11-30 11:01

In many cases, certifications are regarded as proof of knowledge of a specific subject. This consideration makes people more willing to earn certification for personal satisfaction and showcasing their skills. Java certification is the most desirable certification listed in job postings for software developers. Specifically,

  • Java certification places you ahead of many job applicants for Java related jobs.
  • The certification exam contains tricky questions regarding Java. Therefore, in job interviews, you would be more prepared to answer tricky and hard questions than other candidates as you would likely to be exposed to them before.
  • Last but not least, the skills gained while preparing for a certification would help you create more quality and robust software.

Here are the steps to become a certified Java professional:

Categories: Java

Hibernate Fix: Avoid Referencing the ID in the Persistent Class's Equals()

Wed, 2016-11-30 10:01

Because the programmer can define the meaning of Java Equality, it is important not to use the id field in this definition if the id field is a surrogate key. This is because Hibernate only sets the field when saving the object. Hence, for example, if you add the object to some set collection, then saving the object will result in its identity changing, and part of the rules about using Set/Map collection class is that the contained object's identity must not change while it is in the collection (the behavior of a set is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is an element in the set. A special case of this prohibition is that it is not permissible for a set to contain itself as an element).

How Industries and Developers Comply With This Quality Tule

A benchmark of this coding rule across 9k opportunities in CAST Appmarq's code base (more than 2,000 applications are analyzed, monitored, and benchmarked) tells us developers comply in 6 cases out of 10, with 20 violations by application on average. This global compliance score (60.88%) is pretty low if we consider critical security rules generally score around 95% across the different programming languages and industries.

Categories: Java

How I Explained Backbone.js to Java Developers in 45 Minutes

Wed, 2016-11-30 08:31

I was involved in a project that was planned to be demoed at a conference. My role was to lead frontend and support backend-to-frontend development in order to meet the deadline. As the project progressed, it turned out that backend devs had finished their part and were excited to code some JS and backbone… To my surprise, it turned out that about a half an hour introduction meeting was enough to get them going! How was that possible?

  • Oh, so this is just like feeding a Java Bean to a freemarker template!

Categories: Java

Streams in Hibernate and Beyond

Wed, 2016-11-30 07:31

In version 5.2, Hibernate has moved to Java 8 as a baseline. Keeping up with the new functional paradigm of Java 8 with lambdas and streams, Hibernate 5.2 also supports handling a query result set as a stream. Admittedly a small addition to the API, streams add significant value by allowing the Hibernate user to leverage streams parallelism and functional programming without creating any custom adaptors.

This post will elaborate on the added superficially small but fundamentally important streams feature of Hibernate 5.2 and then discuss how the Java 8 stream ORM Speedment takes the functional paradigm further by removing the language barrier and thus enabling a clean declarative design.

Categories: Java