Postingan

Menampilkan postingan dengan label Java 8

How to delete a key value pair from a HashMap during Iteration in Java - Example tutorial

Gambar
Suppose you have a Map or Dictionaries like HashMap or Hashtable, which contains key-value pairs like books and their prices, and you want to delete all books whose prices are greater than 40 USD, how do you that in Java? This is one of the most common scenarios while developing Java application and many Java programmer, will say that they will iterate over Map and check each entry and then use the remove(Object key) or remove(Object key, Object value) methods from java.util.Map to delete any mapping where the value is greater than 40 USD. Though the approach is right, the answer is wrong . Yes, we'll iterate over Map to check each value but we'll not use the two remove() methods from java.util.Map interface because they will throw ConcurrentModficationException when you call them to remove mapping during iteration. Instead, we'll use the Iterator.remove() method to delete any key-value pair, where the value is greater than 40 USD. The Iterator is a common interface which ...

How to Sort a HashMap by Values in Ascending and Descending Order in Java 8 - Example Tutorial

Gambar
In the last article, I have shown you how to sort a Map in Java 8 by keys and today, I'll teach you how to sort a Map by values using Java 8 features e.g. lambda expression, method reference, streams, and new methods added into the java.util.Comparator and Map.Entry classes. In order to sort any Map e.g. HashMap, Hashtable, LinkedHashMap, TreemMap, or even ConcurrentHashMap, you can first get set of entries by using the entrySet() method and then you can get the stream by calling the stream() method. The entrySet()  method returns a Set which inherit the stream() method from the java.util.Collection class. Once you got the stream, you can just call the sorted() method which can sort all Map.Entry objects available in Stream using a Comparator. In order to compare entries of a Map by values, you can use the newly added Map.Entry.comparingByValue() method from the java.util.Map.Entry class. This is the counterpart of comparingByKey() method which we have used in the last article. Bo...

Java 8 Certifications - Oracle Java SE 8 Programmer 1 (1Z0-808) - Latest OCAJP Exam (OCP 11 1Z0-815 and 1Z0-816)

Gambar
Update: The latest version of Java SE certification is now OCAJP 11 (1Z0-815) and OCPJP 11 (1Z0-816) which is based upon Java 11 version. You can take these two exams to become a certified Java 11 developer, but if you already have an Oracle Certified Professional Java Programmer for the Java 6, 7, or 8 versions, then you can take the upgrade OCP Java 6, 7, and 8 to Java SE 11 Developer exam (Exam Code 1Z0-817). I don't have much detail, so I still keep this post as it is, but once I have got more information. I will also update this post. Today one of my readers asked about what is the latest OCPJP or SCJP exams available and is there a Java certification available for Java SE 9 and Java SE 10? This prompted me to browse through Oracle's certification website. During my casual browsing, I noticed that even though it's almost a year since Java SE 9 was released and 6 months since Java SE 10 was released the latest Java certification is still the Java  SE 8 certification. I...

How to use filter + map + collect + Stream in Java? Example Tutorial

Gambar
Hello guys, many of my readers emailed me to write a post about the map and filter function of Java 8 because they found it difficult to understand and use. Even though I have previously blogged about both map() and filter(), I am writing this post again to explain the concept in more layman's language for a better understanding of my readers and fellow Java developers. The map() function is a method in Stream class which represents a functional programming concept. In simple words, the map() is used to transform one object into another by applying a function . That's the reason the Stream.map(Function mapper) takes a function as an argument. For example, by using map() function, you can convert a list of String into List of Integer by applying Integer.valueOf() method to each String on the input list. All you need is a mapping function to convert one object to another, and the map() function will do the transformation for you. It is also an intermediate stream operation whi...

What is a Functional interface in Java 8? @FunctionalInterface Annotation Examples Tutorial

Gambar
The Functional interface is one of the most important concepts of Java 8 which actually powers lambda expression but many developers don't put enough effort to understand it and spend time learning lambda expression and Stream API without first understanding the role of the functional interface in Java 8. Unless you know what is a functional interface and how lambda is related to it, you can't use powerful features of Java 8 likeLambda expression and Stream API. Without knowledge of functional interface , you won't be able to understand where you can use a lambda in the code but also you will struggle to write lambda expression the method is expecting, hence, it's important to have a good understanding of functional interface in Java 8. In this article, I'll try to fill that gap by explaining what is a functional interface, what is a @FunctionalInterface annotation, how they are related to a lambda expression, and how they help you to use the lambda expression in ...

How to Convert a Lambda Expression to Method Reference in Java 8?

Gambar
If you have been coding in Java 8 then you may know that using method reference in place of lambda expression makes your code more readable, hence it is advised to replace lambda expression with method reference wherever possible. But, the big question is, how do you find whether you can replace a lambda with method reference? Yes, it's not that easy, especially if you have been using Java 8 only for a couple of months and struggling to get the functional programming concepts and idioms sorted in your head. Sometimes, IDEs like IntelliJ IDEA and Eclipse does offer some hints to convert lambda expression to method reference but it does make sense to learn the logic behind it, otherwise, it won't make sense. The simple rule to replace lambda expression with method reference is built on common sense, which you will learn in this article. If you look closely, lambda is nothing but a code block which you pass to a function to execute. If you already have that code in form of a meth...

Difference between StringJoiner vs String.join in Java 8 with Examples

Gambar
Joining multiple String literals or object into one is a common programming requirement and you will often find situations where you need to convert a list of String or a Collection of String into a CSV String for your application. For a long time, JDK API has no way to join multiple String literals or objects together, which forces programmers to write hacks like looping through all String objects and manually joining them using String concatenation to create the final, joined String. Even though this approach worked, it was filled with errors and hacks like you need to be careful not to add delimiter before the first element and after the last element, which often caused issues, particularly in case of beginners and junior Java developers. But the bigger problem with that approach was that everyone needs to re-invent the wheel. Since it was a very common requirement you will find many programmers writing the same routines and making the same mistakes and often ending in StackOverflow...

How to Remove a Key, Value, or Entry from an HashMap in Java 8 - Example Tutorial

Gambar
In the last article, I have shown how to remove key/value pairs from a map in Java based upon some conditions like remove all entries where values are greater than some values. I have told you that you must use the Iterator's remove() method for removing mapping while iteration to avoid ConcurrentModificationException, but that's not required anymore in Java 8. From JDK 8 onwards, you can selectively remove mapping without iterating over Map. The JDK 8 API has added several new methods to existing interfaces e.g. java.util.Collection, java.util.Map, Comparator etc because the interface can now contain concrete methods in form of static and default methods. One of such methods is removeIf()  method of Collection class which allows you to remove entries based upon some condition or predicate. It takes a lambda expression, which can be used to supply the predicate you want. For example, you can rewrite the above code in Java 8 as shown in the following example: Since here we need ...

Top 5 Java 8 and 9 Courses for Programmers - Best of Lot

Gambar
Hello guys, if you are looking for some free resources, e.g., books, tutorials, and courses to learn and master new features introduced in Java 8 and Java 9, then you have come to the right place. In the past, I have shared some of the best Java 8 tutorials and books, and in this article, I am going to share some of the best, free Java 8 and Java 9 courses from Udemy and Pluarlsight with you. These courses are pretty similar to many paid courses you usually buy, and many of these are made free by just promotional or educational purposes by their instructor. You can join these courses to learn JDK 8 and JDK 9 features in a couple of days. I used to start learning with books a couple of years back, but nowadays, online courses are my preferred way of learning. There are so many excellent free courses available on Udemy, which you can join to enhance your knowledge. Though, sometimes, I have noticed that free courses turn into paid internships, especially after the instructor reaches thei...

Top 5 Best Java 8 Tutorials, Courses, and Books

Gambar
It's been quite some time since the Java 8 was released but Java community is still buzzing with functional programming and lambda expression. Many developers, programmers, and bloggers have already shared lots of really good tutorial to learn lambda expressions, probably the biggest thing in Java world after Generic was introduced in Java 5. While the term "lambda expression" may sound abstract and academic, Java 8 Lambdas can have a big impact on how you program every day. Java 8 has not only introduced Lambda expressions but also Stream API, Functional interfaces, new Date and Time API and default methods, which has completely changed the way you write your Java code. As a professional Java developer, you will have to learn Java 8 one day, and it's better to do sooner than later. Even if you are not using many of the Java 8 features, one thing is sure that It's definitely changed change how you use Collection framework classes e.g. ArrayList or HashMap in your...