Employees date of birth is stored in an file in the following format:
08/06/1984
29/5/1982
20/1/1989
17/5/1989
22nd October 1991
14th December 1991
20th February 1994
27th July 1990
26th Dec
1991
12th Nov
1990
19th Dec
1990
27th Dec 1989
Java8 IntSummaryStatistics class to calculate total employees,
total age, and average age group of the organization.
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.util.IntSummaryStatistics;
import java.util.stream.Stream;
public class DemoExample {
public static void main(String... args) {
try (Stream<String> lines = Files.lines(Paths.get("demo"))) {
IntSummaryStatistics intstream = lines.sequential()
.mapToInt(e -> LocalDate.now().getYear() - Integer.parseInt(e.substring(e.trim().length() - 4)))
.summaryStatistics();
long total_age = intstream.getSum();
long total_count = intstream.getCount();
double avg_age = intstream.getAverage();
System.out.println("Total employees :" + total_count);
System.out.println("Total age is :" + total_age);
System.out.println("Average age is :" + avg_age);
} catch (Exception e) {
e.printStackTrace();
}
}
}
No comments:
Post a Comment