In Java 9, Private Methods in Interfaces has been introduced. This enables code sharing between non-abstract methods in an interface. In this article apart from understanding the Private Method in Interface, we will learn how the Interface has evolved in different versions of Java. [Read more…]
In this Jackson Tree Model Example, we will learn how to convert Java Object to JSON and vice-versa ( JSON to Java object). Jackson Tree Model creates a tree representation of a JSON similar to DOM Tree. Hence it is possible to traverse through each node. Jackson provides JsonNode API through we will be accessing the individual node using the node name.The readTree and writeTree methods of the Jackson ObjectMapper is used to read and write JSON tree.
Jackson reads and writes JSON through a high-performance Jackson Streaming API, with a low memory and process overhead. The only problem with Streaming API is that we need to take care of all the tokens while parsing JSON data. All the JSON values must be read/write in the same order in which it arrives. [Read more…]
There are times when we need to convert Java project into a Maven project, this will be a huge task when you have more number of projects. There is a plugin called “m2e” plugin which lets us perform this task with ease. Just follow the below step to convert Java Project to Maven project. [Read more…]
Multiple Inheritance in Java is nothing but one class extending more than one class. Previous versions of Java(until JDk 7) doesn’t support Multiple Inheritance because it causes a famous problem called “Diamond Problem“ and hence indirectly Multiple Inheritance in Java is achieved using Interfaces. After the introduction of Default Methods in Java 8, even the interfaces can also have the method bodies. In this tutorial let’s see how the Diamond Problem is prevented in Java 8. [Read more…]
Jackson JSON Parser is a very popular JSON Java Parser, which can easily transform Java Objects to JSON and vice versa and Jackson API even provides default mapping for most of the objects which need to be serialized. In my previous articles, we have learnt How to Read a JSON in Java and How to write JSON object to File using GSON and JSON.simple API. In this Jackson JSON Example, let’s see how to use Jackson API to read JSON file and Write a JSON file. [Read more…]
Java Interface always can contain the method declaration and cannot contain method implementation (method body), “there is no possible way of adding method implementation to a Java Interface” has become a myth now after the introduction of Default Method in Java 8. Default Method or Defender methods is a new feature which lets the developer add a new method to an existing interface and provides the implementation for it without breaking existing implementation of the interface. For defining method body, we simply need to add Java default keyword while declaring the method. Let’s look into the below example and understand how Default Methods works. [Read more…]
GSON is an open source JSON parser developed by Google to serialize and deserialize JSON in Java. In my previous article, we looked at reading and writing JSON to a file using JSON.simple. In this article, we will learn how to use GSON to Read and Write JSON in Java (parse JSON file to Java objects and vice-versa Java object to JSON) [Read more…]
All the Collection Interfaces in Java (List, Set, Map etc) will be extending the Iterable interface as the super interface. In Java 8 a new method has been introduced for Iterating over collections in Java.
void forEach(Consumer<? super T> action)
In this Java 8, ForEach Example article lets see how to iterate through a List and Map with the new forEach Loop in Java 8. [Read more…]
In my previous article we have learnt How to read JSON file in Java, now let’s see how we can write JSON Object to File in Java. Here also we will be using the JSON.simple library download the json-simple-1.1.1.jar (or) if you are running on maven add the below dependency to your pom.xml
<dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1.1</version> </dependency>