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
Consumer interface accepts single input argument and returns no result
while Supplier interface does not take any input but returns output
public static Map<String, List<Student>> customizedGpaGroupingBy() {
Map<String, List<Student>> studentGapGrouping =
StudentDataBase
.getAllStudents()
.stream()
.collect(
Collectors.groupingBy(stud -> stud.getGpa()>=5? "OUTSTANDING":"AVERAGE"));
return studentGapGrouping;
}
customizedGpaGroupingBy().forEach((x, y) -> {
System.out.println(x + " ");
y.forEach(a ->
System.out.println("name= " + a.getName() + " gpa=" + a.getGpa()));
});
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
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)
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)
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"));
list.add(new Person(100, "Gopal"));
Map<Integer, String> map = list.stream()
.collect(Collectors.toMap(Person::getId, Person::getName, (x, y) -> x + ", " + y));
map.forEach((x, y) -> System.out.println("Key: " + x + ", value: " + y));
}
Parameters | Description |
---|---|
keyMapper | 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. |