You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Notes about Default methods in an interface in java 8
1) If you have 2 interfaces which contains same non-default methods then, inside an implementation class either
you provide InterfaceName.super.methodName(); to directly refer a method from super interface or else it will
call default implementation inside the implementation class itself.
2) If you have multiple interfaces with DEFAULT method/s, and if any one or all of them are OVERRIDDEN in
client class, then while calling those methods in implementation class it always refers to the child
implementation method of client class. For examples refer to the defaults package inside this repository.
3) A method in an interface should have only one of these following modifiers at a time
public, abstract, static, default, strictfp
Difference between Consumer Functional Interface and Supplier Functional Interface
Consumer interface accepts single input argument and returns no result
while Supplier interface does not take any input but returns output
How to iterate a Map using lambda in forEach loop
filename and method name: StreamGoupingByExample.java , customizedGpaGroupingBy()
OutPut: to remove square brackets and for clean output
AVERAGE OUTSTANDING
name= Adam gpa=3.6 name= Dave gpa=6.0
name= Jenny gpa=1.8 name= Viccky gpa=7.0
name= Emily gpa=4.0 name= Gopal gpa=8.9
name= Sophia gpa=3.5 name= Vijay gpa=5.5
name= James gpa=3.9
Java-8-Code Samples using Lambda, Streams, Functional Interfaces etc.
To avoid duplicate keys in Collectors.toMap() using List to Map with Key Mapper, Value Mapper and Merge Function and assign it to LinkedHashMap
<T,K,U> Collector<T,?,Map<K,U>> toMap(Function<? super T,? extends K> keyMapper,
Function<? super T,? extends U> valueMapper,
BinaryOperator<U> mergeFunction)
Code :
public static void main(String[] args) {
List<Person> list = new ArrayList<>();
list.add(new Person(100, "Mohan"));
list.add(new Person(100, "Sohan"));
list.add(new Person(300, "Mahesh"));
LinkedHashMap<Integer, String> map =list.stream()
.collect(Collectors.toMap(Person::getId, Person::getName,
(x, y) -> x+", "+ y, LinkedHashMap::new));
map.forEach((x, y) -> System.out.println("Key: " + x +", value: "+ y));
}
To avoid duplicate keys in Collectors.toMap() using BinaryOperator and Supplier OR List to Map with Key Mapper, Value Mapper, Merge Function and Map Supplier
<T,K,U,M extends Map<K,U>> Collector<T,?,M> toMap(Function<? super T,? extends K> keyMapper,
Function<? super T,? extends U> valueMapper,
BinaryOperator<U> mergeFunction,
Supplier<M> mapSupplier)
A mapping function to produce the map keys for each input stream element.
valueMapper
A mapping function to produce the map values for each input stream element.
mergeFunction
A binary operator which is to resolve collisions between values associated with the same key. The inputs to this function are the values which belong to the same key.
mapSupplier
A function which provies a new instance of the desired implementation of the Map.