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 code examples to write content into file. You may find examples of reading files using java 8 APIs in linked blog post. 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 bunches. […]
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 […]
Let’s see the example of joining array of String to produce single String. We may need this information many times during development specially while parsing contents out of JSON or XML. 1) Join String Array using String.join() method – Java 8 String.join() method has two flavors. i) join(CharSequence delimiter, CharSequence… elements) This method can be […]
In this tutorial, I am listing down 3 ways to know the difference between two dates in java. Fist way is to calculate using Jodatime API which was available even before java 8 release. Later 2 ways are using new Date Time API features available since java 8. Difference between dates before java 8 using […]
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 […]
In this post, I am giving example of a very useful task in day to day programming i.e. reading files line by line using java IO and perform some operations of lines. Before moving ahead, let me mention the file content which I will read in all examples in this post. I will read the […]
Java 8 has brought many awesome features for java developers. Some of them I have already described in Comparator changes, Streams examples, Internal vs. external iterations, predicates, functional interfaces, default methods, lambda expressions and date and time API changes. All above changes were related to lambda expressions, which is the most attention grabber and somehow […]
SO far till java 7, we had String.split() method which can split a string based on some token passed as parameter. It returned list of string tokens as string array. But, if you want to join a string or create a CSV by concatenating string tokens using some separator between them, you have to iterate […]
Example of using java 8 lambda with Comparator interface to sort a list of objects – or create multiple sorters for collection of objects.