Commit bf143ed1 authored by Syed Javed Ali's avatar Syed Javed Ali

Added code for sorting

parent 1d0e68fa
......@@ -10,37 +10,49 @@ import java.util.stream.Collectors;
*/
public class ClientApp {
public static void main(String[] args) {
Person p1= new Person(1201,"HARI",8700,"HYD","DEV","40582678416578",
Person p1= new Person(1201,"HARI",8700,"HYD","DEV",20200725122530L,
Arrays.asList("7722448815","9966778844","9944668877"));
Person p2= new Person
(1202,"SHAM",5500,"PUNE","QA",
"69382678416578", Arrays.asList("8855447788","7547871478","0147248784"));
20201025013020L, Arrays.asList("8855447788","7547871478","0147248784"));
Person p3= new Person
(1203,"SAI",7500,"BANGLORE","DEV",
"14782678413689", Arrays.asList("7755447788","6767714785","01452487845"));
20200825032515L, Arrays.asList("7755447788","6767714785","01452487845"));
Person p4= new Person
(1204,"KIRAN",9500,"CHENNAI","DEV",
"89782678413689", Arrays.asList("8855447788","7667714785","01252487845"));
20201125052010L, Arrays.asList("8855447788","7667714785","01252487845"));
Person p5= new Person
(1203,"KHAN",4500,"CHENNAI","PROD",
"60582678413689", Arrays.asList("9955447788","1467714785","02352487845"));
20200625072045L, Arrays.asList("9955447788","1467714785","02352487845"));
List<Person> persons=Arrays.asList(p1,p2,p3,p4,p5);
List<Person> personList=Arrays.asList(p1,p2,p3,p4,p5);
// find the person id with highest salary in a given department
Optional<Person> optionalPerson= persons.stream().filter(p -> p.getDepartment().equals("DEV"))
.collect(Collectors.maxBy(Comparator.comparingInt(f-> f.getSalary())));
System.out.println
(optionalPerson.isPresent()?
"Person ID with highest salary :"+optionalPerson.get().getId():
"Record with highest salary not exist");
// print the no of employees at each location.
Map<String,Long> locationCount= persons.stream().collect(Collectors.groupingBy(Person::getLocation,Collectors.counting()));
for(String s:locationCount.keySet()){
System.out.println(s+"-"+locationCount.get(s));
}
Optional<Person> highestPaidPersonWrapper= personList.stream()
.filter(p-> p.getDepartment().equals("DEV")
).collect(Collectors.maxBy(Comparator.comparingInt(f-> f.getSalary())));
System.out.println("Highest Salary Person ID: "+highestPaidPersonWrapper.get().getId());
//print the no of employees at each location.
Map<String,Long> personCountByLocation=personList.stream()
.collect(Collectors.groupingBy(Person::getLocation,Collectors.counting()));
Set<Map.Entry<String,Long>> entrySet=personCountByLocation.entrySet();
for(Map.Entry<String,Long> entry:entrySet){
System.out.println(entry.getKey()+" "+entry.getValue());
}
//verify the two persons provided the same phone number
//sort the person list based on timestamp.
//sort descending
List<Person> sortBasedOnTimestamp= personList.stream()
.sorted(Comparator.comparing(Person::getTimestamp).reversed()).collect(Collectors.toList());
sortBasedOnTimestamp.forEach(System.out::println);
}
}
......@@ -19,6 +19,6 @@ public class Person {
private int salary;
private String location;
private String department;
private String timestamp;
private long timestamp;
private List<String> phones;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment