ERROR - Collectors.averagingInt - Type mismatch: cannot convert from Object to int

Error

Type mismatch: cannot convert from Object to int within Collectors.averagingInt 

Error Type

Compile Time ( Java 8 )

Sample Code

Set<Integer> intSet = new HashSet<Integer>();
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
System.out.println(intSet.stream().collect(Collectors.averagingInt(p->p)));

Cause

The method averagingInt expects the argument as function returning Integer and not returning Object.

Resolution

Specify the argument as the function returning Integer.

Set<Integer> intSet = new HashSet<Integer>();
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);   
System.out.println(intSet.stream().collect(Collectors.averagingInt(p->(Integer)p)));