Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

What is Diamond operator in Java? Example Tutorial

Hello guys, if you are reading Java code and come across closed angle bracket with a diamond like shape and wondering what they are and what they do then you have come to the right place. They are known as Diamond operator in Java and I will explain you what they are and where should you use them in this article. The Diamond operator is a relatively new operator in Java which was first introduced in JDK 7  to improve type inference and reduce boilerplate Java coding. It is denoted with a closed angle bracket that resembles the shape of a diamond (<>) and that's why it's called the Diamond operator. If used correctly it can reduce typing and boilerplate coding in Java and result in much cleaner and readable code especially when you use Generics.  

I regularly use the Diamond operator in Java while declaring and initializing objects involving multiple Generic types like Map which holds another Map and because of improved type inference, I only need to declare the type information on the left-hand side of declaration. It reduces my typing by half, hence result in a faster coding experience. 

Before Java 6, generic type inference was poor in Java. You need to declare type information on both the right and left-hand sides of the variable declaration. This makes code unnecessary complicated, and hard to read, especially when using nested types, like Map with Entry whose key is String and value is List of Integers


By the way, if you're new to the Java programming world then you can also join the best free Java programming courses to kick start your Java developer journey. It's my curated list and contains the best free Java online courses from sites like Udemy, Pluralsight, Educative, and Coursera to learn and level up Java skills for both beginners and intermediate developers. 




What is Diamond Operator Example in Java?

Here is how you can simplify code by using the diamond operator in Java; remember, this feature is only available from Java SE 7 release, so you can use it in JDK 7 and 8 but not in Java 6. If you want to improve type inference in Java 6, take advantage of static factory methods, as suggested by Joshua Bloch in his book Effective Java.


What is Diamond operator in Java




When to use Diamond Operator in Java?

Here are some examples of using the Diamond operator in Java. These are the places where I found myself using diamond operator again and again. 

1. Reference variable Initialization

// without diamond operator
Map<String, Map<Long, Employ> = new HashMap<String, Map<Long, Employee>>();

// with diamond operator
Map<String, Map<Long, Employ> = new HashMap<>();


2. passing arguments to methods

public void print(Map<String, Map<Long, Employ> data){
     
   // code to print map content

}

when you call this method, you can use the Diamond operator as shown below

print ( new HashMap<>());




Advantages of using Diamond Operator in Java

Here are some key advantages of using the Diamond operator in Java:

1. Less typing

2. Improved readability

3. Concise code


Since all of these are related to improving code quality and developer productivity, I highly recommend Java developers both beginners and experienced one to start using diamond operator on the right hand side of assignment at least. It will not only save time and allow you to write code faster but also improve the readability of code. 

That's all about how to use the Diamond operator in Java. If you are writing Java 7, then it's always better to use the diamond operator; there is no reason not to use it. You can always save few keystrokes even if your type declaration is simple, but you will gain more in the case of complex and nested type declarations. You can also use the diamond operator while passing arguments to methods.


Other Core Java Articles and Tutorials you may like
  • How to compare objects on multiple fields in Java (example)
  • 20 Examples of Date and Time in Java 8 (tutorial)
  • Top 5 Books to Learn Java 8 (read here)
  • How to use Stream class in Java 8 (tutorial)
  • How to convert List to Map in Java 8 (solution)
  • How to use filter() method in Java 8 (tutorial)
  • How to use forEach() method in Java 8 (example)
  • How to use Stream API in Java 8 (learn here)
  • How to implement Comparator using lambda expression (see here)
  • How to filter Collection using Streams in Java 8 (check here)
  • Understanding Default methods of Java 8 (learn here)
  • How to join String in Java 8 (example)
  • How to use peek() method in Java 8 (example)
  • 8 Best Lambda and Functional Programming courses (best courses)

Thanks for reading this article so far. If you like this Java Diamond operator tutorials and examples then please share them with your friends and colleagues. If you have any questions or feedback about this Java 8 tutorial then please drop a note.

P.S.: If you want to learn more about new core Java features like Diamond operator then please see these best Java 8 to Java 16 courses. It explains all important features of Java 8 like lambda expressions, streams, functional interface, Optional, new date, and time API, and other miscellaneous changes.    

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.