Task - Collections - Student Statistics #56
Replies: 53 comments
-
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.Set;
import java.util.stream.Collectors;
class Student {
private String name;
private int id;
private String subject;
double percentage;
public Student(String name, int id, String subject, double percentage) {
this.name = name;
this.id = id;
this.subject = subject;
this.percentage = percentage;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public String getSubject() {
return subject;
}
public double getPercentage() {
return percentage;
}
@Override
public String toString() {
return name + "-" + id + "-" + subject + "-" + percentage;
}
}
public class StudentDriver {
public static void main(String[] args) {
Student student1 = new Student("Dinesh", 1, "Algorithms", 75);
Student student2 = new Student("Arnav", 2, "Automata", 55);
Student student3 = new Student("Anamika", 3, "Databases", 80);
Student student4 = new Student("Vishal", 4, "Networking", 40);
List<Student> studentList = Arrays.asList(student1, student2, student3, student4);
Map<Boolean, List<Student>> map = studentList.stream()
.collect(Collectors.partitioningBy(stu -> stu.getPercentage() > 60));
System.out.println(map);
List<Student> topPerformer = studentList.stream()
.sorted(Comparator.comparing(Student::getPercentage, Comparator.reverseOrder())).limit(3)
.collect(Collectors.toList());
System.out.println(topPerformer);
Map<String, Double> map2 = studentList.stream()
.collect(Collectors.toMap(Student::getName, Student::getPercentage));
System.out.println(map2);
Set<String> subjectSet = studentList.stream().map(Student::getSubject).collect(Collectors.toSet());
System.out.println(subjectSet);
OptionalDouble highest = studentList.stream().mapToDouble(Student::getPercentage).max();
System.out.println("Highest Percentage :" + highest.getAsDouble());
OptionalDouble lowest = studentList.stream().mapToDouble(Student::getPercentage).min();
System.out.println("Lowest Percentage :" + lowest.getAsDouble());
OptionalDouble average = studentList.stream().mapToDouble(Student::getPercentage).average();
System.out.println("Average Percentage :" + average.getAsDouble());
System.out.println("Total students: " + studentList.size());
Map<String, List<Student>> groupBySubject = studentList.stream()
.collect(Collectors.groupingBy(Student::getSubject));
System.out.println(groupBySubject);
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
package Datastructures; import java.util.*; class Student { } public class Arraylistop { } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
import java.util.Arrays; class Student { } public class StudentDriver { } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
package task56;
public class Student {
private String name;
private int id;
private String subject;
double percentage;
public Student(String name, int id, String subject, double percentage) {
this.name = name;
this.id = id;
this.subject = subject;
this.percentage = percentage;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public String getSubject() {
return subject;
}
public double getPercentage() {
return percentage;
}
@Override
public String toString() {
return "Student [name=" + name + ", id=" + id + ", subject=" + subject + ", percentage=" + percentage + "]";
}
}
package task56;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
public class StudentDriver {
public static void main(String[] args) {
Student student1 = new Student("Dinesh", 1, "Algorithms", 75);
Student student2 = new Student("Arnav", 2, "Automata", 55);
Student student3 = new Student("Anamika", 3, "Databases", 80);
Student student4 = new Student("Vishal", 4, "Networking", 40);
List<Student> studentList = Arrays.asList(student1,student2,student3,student4);
Map<Boolean,List<Student>>less60 = studentList.stream().collect(Collectors.partitioningBy(s->s.getPercentage()>60));
System.out.println(less60);
List<Student> topper = studentList.stream().sorted(Comparator.comparing(Student::getPercentage,Comparator.reverseOrder())).limit(3).collect(Collectors.toList());
System.out.println(topper);
Map<String, Double> names = studentList.stream().collect(Collectors.toMap(Student::getName, Student::getPercentage));
System.out.println(names);
Set<String> subject = studentList.stream().map(Student::getSubject).collect(Collectors.toSet());
System.out.println(subject);
List<Double> cgpa = studentList.stream().map(Student::getPercentage).collect(Collectors.toList());
System.out.println("highestpercentage:"+Collections.max(cgpa));
System.out.println("lowestpercentage:"+Collections.min(cgpa));
Double avg = student1.getPercentage()+student2.getPercentage()+student3.getPercentage()+student4.getPercentage();
Double avgPer = ((avg)/studentList.size());
System.out.println(avgPer);
int length = studentList.size();
System.out.println(length);
studentList.stream().collect(Collectors.groupingBy(Student::getSubject)).forEach((k,v)->System.out.println("\n"+ k +"Group By" + v + "\n"));
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Hi @akash-coded , Please find my solution. My name sayan panja. import java.util.ArrayList; public class EdurecaStudentStatics{ } |
Beta Was this translation helpful? Give feedback.
-
|
package edureka; import java.util.*; public class Student { } |
Beta Was this translation helpful? Give feedback.
-
|
` public class StudentImplementation { } ` |
Beta Was this translation helpful? Give feedback.
-
|
import java.util.ArrayList; class Student { } public class StudentDriver { } |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
|
@akash-coded |
Beta Was this translation helpful? Give feedback.
-
|
@akash-coded |
Beta Was this translation helpful? Give feedback.
-
|
@akash-coded : Below is the solution. import java.util.*; public class StudentStats { } class Student { } |
Beta Was this translation helpful? Give feedback.
-
|
import java.util.*; public class Main { } class Student { } |
Beta Was this translation helpful? Give feedback.
-
|
import java.util.ArrayList; class Student { } public class StudentsDetails { } |
Beta Was this translation helpful? Give feedback.
-
|
import java.util.ArrayList; class Student { } public class Studentstats { } |
Beta Was this translation helpful? Give feedback.
-
import java.util.*;
class Student {
private String name;
private int id;
private String subject;
double percentage;
public Student(String name, int id, String subject, double percentage) {
this.name = name;
this.id = id;
this.subject = subject;
this.percentage = percentage;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public String getSubject() {
return subject;
}
public double getPercentage() {
return percentage;
}
@Override
public String toString() {
return name + "-" + id + "-" + subject + "-" + percentage;
}
}
public class StudentAnalyticsNoStream {
public static void main(String[] args) {
Student student1 = new Student("Dinesh", 1, "Algorithms", 75);
Student student2 = new Student("Arnav", 2, "Automata", 55);
Student student3 = new Student("Anamika", 3, "Databases", 80);
Student student4 = new Student("Vishal", 4, "Networking", 40);
List<Student> studentList = new ArrayList<>();
studentList.add(student1);
studentList.add(student2);
studentList.add(student3);
studentList.add(student4);
Map<Boolean, List<Student>> partitioned = new HashMap<>();
partitioned.put(true, new ArrayList<>());
partitioned.put(false, new ArrayList<>());
for (Student student : studentList) {
if (student.getPercentage() > 60) {
partitioned.get(true).add(student);
} else {
partitioned.get(false).add(student);
}
}
System.out.println("Partitioned Students (Above 60% vs 60% or Below):");
System.out.println(partitioned);
System.out.println();
List<Student> sortedByPerformance = new ArrayList<>(studentList);
sortedByPerformance.sort((s1, s2) -> Double.compare(s2.getPercentage(), s1.getPercentage()));
System.out.println("Top 3 Performing Students:");
for (int i = 0; i < Math.min(3, sortedByPerformance.size()); i++) {
System.out.println(sortedByPerformance.get(i));
}
System.out.println();
System.out.println("Name and Percentage of Each Student:");
Map<String, Double> nameToPercentage = new LinkedHashMap<>();
for (Student s : studentList) {
nameToPercentage.put(s.getName(), s.getPercentage());
}
for (String name : nameToPercentage.keySet()) {
System.out.println(name + ": " + nameToPercentage.get(name) + "%");
}
System.out.println();
Set<String> subjects = new LinkedHashSet<>();
for (Student s : studentList) {
subjects.add(s.getSubject());
}
System.out.println("Subjects Offered in the College:");
for (String subject : subjects) {
System.out.println(subject);
}
System.out.println();
double highest = Double.MIN_VALUE;
double lowest = Double.MAX_VALUE;
double total = 0;
for (Student s : studentList) {
double perc = s.getPercentage();
if (perc > highest) highest = perc;
if (perc < lowest) lowest = perc;
total += perc;
}
double average = total / studentList.size();
System.out.println("Performance Statistics:");
System.out.println("Highest Percentage: " + highest + "%");
System.out.println("Lowest Percentage: " + lowest + "%");
System.out.println("Average Percentage: " + average + "%");
System.out.println();
System.out.println("Total Number of Students: " + studentList.size());
System.out.println();
Map<String, List<Student>> subjectMap = new LinkedHashMap<>();
for (Student s : studentList) {
String subject = s.getSubject();
subjectMap.putIfAbsent(subject, new ArrayList<>());
subjectMap.get(subject).add(s);
}
System.out.println("Students Grouped by Subject:");
for (String subject : subjectMap.keySet()) {
System.out.println(subject + ": " + subjectMap.get(subject));
}
}
} |
Beta Was this translation helpful? Give feedback.
-
**
import java.util.*; class Student { } public class Main { } |
Beta Was this translation helpful? Give feedback.
-
|
Bhavana class Student { } public class StudentDriver { } |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
You are given the following blueprint for representing a college Student:
Create four Student instances:
Create a list of these students called
studentListand do the following operations:Expected Output:
Beta Was this translation helpful? Give feedback.
All reactions