If you’ve been developing software for a while, you know how to generate a random number and perhaps even securely with Java’s SecureRandom class. Unfortunately, generating secure random numbers is not as easy as simply using SecureRandom. In this java example, we’ve assembled a simple checklist to help you be successful when using secure random […]
Learn to apply if-else logic in a Java 8 stream of elements to filter elements based on certain condition. The if-else condition can be applied using the lambda expressions in stream filter() function.
The Java forEach is a utility method to iterate over a collection or stream and perform a certain action on each element of it.
Java 8 stream distinct by multiple fields example. Learn to find distinct objects from a stream by comparing multiple fields or creating a custom key class.
To get stream of random integers, longs or doubles within a given range – use the ints(), longs() and doubles() methods in Random and SecureRandom classes.
Learn to find min and max date, number, Char, String or object from stream of comparable elements in easy steps using Comparator.comparing() method.
To count the number of elements in stream in Java 8, use either the Stream.count() or Collectors.counting() methods.
To convert primitives intstream or longstream to List, Set or Array, you can adapt one of the following ways given with examples.
In Java 8, if you want to convert stream of objects to collection, then you can use one of the static methods in the Collectors class. The same process doesn’t work on streams of primitives, however. To convert a stream of primitives, you must first box the elements in their wrapper class and then collect […]
Learn to compile regular expression into java.util.function.Predicate. This can be useful when you want to perform some operation on matched tokens.
Java 8 allows to refer method from class or object using class::methodName syntax. Learn about different types of available method references in java 8.
Using java 8 stream API, you can use stream.distinct() method to filter or collect all distinct elements from a collection. Let’s learn how to find distinct elements with java stream API.
In this example, we will learn to use java 8 APIs along with Files.list() and DirectoryStream to list all files present in that directory.
In this example, we will learn to watch a directory along with all sub-directories and files inside it, using java 8 WatchService API.
Java 8 example to content into file. You may find examples of reading files using java 8 APIs in linked blog post. 1. Java 8 write to file using BufferedWriter BufferedWriter is used to write text to a character or byte stream. Before printing the characters, it stores the characters in buffer and print in […]
Let’s see how you can convert from string to date in java 8. 1) Convert string to date in ISO8601 format By default, java dates are in ISO8601 format, so if you have any string which represent date in ISO8601 format then you can use LocalDate.parse() or LocalDateTime.parse() methods directly. 2) Convert string to date […]
Learn to join string array with delimiter to produce single string. Use listed java example to convert list of strings or array of strings to single string.
Learn to find difference between two dates in days, months or any other time units – using Java 8 classes such as Duration, ChronoUnit and finally JodaTime.
All of us must have encountered NullPointerException in our applications. This exception happen when you try to utilize a object reference which has not been initialized, initialized with null or simply does not point to any instance. NULL simply means ‘absence of a value’. Most probably, the Romans were only ones, who didn’t run into […]
In java 8 learning series, we already learned about new way to read a file line by line using streams. Java 8 introduced one more good addition related to IO operations and that is Base64 support. I this post, we will learn about it. What is Base 64 encoding? When you have some binary data […]