In this article we will go through very basic & simple examples of Java Flow API (java.util.concurrent.Flow) which was introduced…
Category: Java 9
In Java 9, there are few useful methods introduced to java.util.stream.Stream. Stream.iterate – For loop with index. Equivalent for for (int i = 0; i < sortedNames.length; i++) {...} Stream.takeWhile –…
Encapsulation before Java 9 Private – Encapsulates method/variable within class. Not accessible outside of class. Default – Encapsulates method/variable within…
In Java 9, factory methods are introduced for collections API. Below are examples. Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
//Example import java.util.List; import java.util.Map; import java.util.Set; public class CollectionsFactoryMethods { public static void main(String[] args) { /* * List factory method */ List<String> numberList = List.of("One", "Two", "Three"); System.out.println(numberList); /* * Set factory method */ Set<String> numberSet = Set.of("One", "Two", "Three"); System.out.println(numberSet); /* * Map factory method */ Map<String, String> numberMap = Map.of("1", "One", "2", "Two", "3", "Three"); System.out.println(numberMap); } } |
Output:
1 2 3 |
[One, Two, Three] [One, Two, Three] {3=Three, 2=Two, 1=One} |
Since Java 8, underscore “_” was marked for removal as legal identifier i.e. variable / method name etc. & was…
What is it? Private method in interface with implementation. Private method can be used within default or static methods within…