Java 8 has added lots of good to use methods to make it easy to use with streams, easy to…
Category: Java
Problem: If input is given n = 20, then print all primes numbers till 20. (Prime = Number which can…
Encapsulation before Java 9 Private – Encapsulates method/variable within class. Not accessible outside of class. Default – Encapsulates method/variable within…
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 |
package ravi.tutorial.java.java6; import java.util.Arrays; import java.util.NavigableSet; import java.util.TreeSet; public class NavigableCollectionsDemo { public static void main(String[] args) { /* * Create NavigableSet */ NavigableSet<Integer> numbers = new TreeSet<>(); numbers.addAll(Arrays.asList(1, 2, 3, 4, 5)); /* * Examples of different methods of NavigableSet */ System.out.println("higher() - " + numbers.higher(3)); System.out.println("lower() - " + numbers.lower(3)); System.out.println("headSet() - " + numbers.headSet(3)); System.out.println("tailSet() - " + numbers.tailSet(3)); System.out.println("ceiling() - " + numbers.ceiling(3)); } } |
Output:
1 2 3 4 5 |
higher() - 4 lower() - 2 headSet() - [1, 2] tailSet() - [3, 4, 5] ceiling() - 3 |
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package ravi.tutorial.java.java7; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; public class MethodHandleTest { public static void main(String[] args) throws Throwable { /* * Prepare lookup */ MethodHandles.Lookup lookup = MethodHandles.lookup(); /* * Create a method type which has signature as * * Return type = Double, First method argument = Integer, Second method argument * = Long * * This is for matching add method in Calculator class */ MethodType mt = MethodType.methodType(Double.class, Integer.class, Long.class); /* * Get method handle from class Calculator, with method name 'add' and matches * method signature as per mt method type. */ MethodHandle mh = lookup.findVirtual(Calculator.class, "add", mt); /* * Call method on calcObject with method arguments as arg_1 & arg_2 */ Calculator calcObject = new Calculator(); Integer arg_1 = 1; Long arg_2 = 2l; Double output = (Double) mh.invokeExact(calcObject, arg_1, arg_2); System.out.println(output); } } /* * Demo class */ class Calculator { public Double add(Integer a, Long b) { Long l = (a + b); return l.doubleValue(); } } |
Output:
1 |
3.0 |
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…
What is it? Method in interface with default keyword. Method marked default will have an implementation within interface itself. Subclasses…
New features, examples, release notes of all Java Releases. Refer below table.