I have to perform the following activity:
7.- showDifferentSubjectsOrderedAlphabetically()
It must show the subjects in which there is a student enrolled, ordered alphabetically. The output would be: Subjects: LM PROGR
To do this, I had to previously create different classes, these:
package classes;
import java.util.List;
public class Student {
private final long id;
private final String name;
private byte age;
private String group;
private int grant; // Beca.
private List<Grade> grades; // Lista de notas.
public Student(long id, String name, byte age, String group, int grant, Grade... grades) {
this.id = id;
this.name = name;
this.age = age;
this.group = group;
this.grant = grant;
this.grades = List.of(grades);
}
/*
Getters and Setters
*/
public long getID() {
return id;
}
public String getName() {
return name;
}
public byte getAge() {
return age;
}
public void setAge(byte age) {
this.age = age;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public int getGrant() {
return grant;
}
public void setGrant(int grant) {
this.grant = grant;
}
public List<Grade> getGrades() {
return grades;
}
public void setGrades(List<Grade> grades) {
this.grades = grades;
}
} // end class
ackage classes;
import java.util.Objects;
public class Grade {
private final String subject;
private float mark;
public Grade(String subject, float mark) {
this.subject = subject;
this.mark = mark;
}
public String getSubject() {
return subject;
}
public float getMark() {
return mark;
}
public void setMark(float mark) {
this.mark = mark;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Grade grade = (Grade) o;
return subject.equals(grade.subject);
}
@Override
public int hashCode() {
return Objects.hash(subject);
}
} // end class
package classes;
import java.util.ArrayList;
import java.util.List;
public class Database {
private List<Student> students = new ArrayList<>();
public Database() {
// ong id, String name, byte age, String group, int grant, Grade... grades
students.add(new Student(1, "Germán Ginés", (byte) 23, "1o CFGS DAM", 2000, new Grade("PROGR", 8), new Grade("LM", 3)));
students.add(new Student(2, "Baldomero", (byte) 21, "1o CFGS DAM", 0, new Grade("PROGR", 5), new Grade("LM", 4)));
students.add(new Student(3, "Ana Guerra", (byte) 17, "1o CFGS SMR", 4000, new Grade("PROGR", 8)));
}
public List<Student> queryAllStudents() {
return students;
}
}
I tried:
public String[] showDifferentSubjectsOrderedAlphabetically() {
return database.queryAllStudents().stream()
.map(Student::getGrades)
}
Y
public String[] showDifferentSubjectsOrderedAlphabetically() {
return database.queryAllStudents().stream()
.flatMap(Student::getGrades)
}
In the first it returns a super weird type: Stream> and in the second it returns a list of objects... In either case, I can't go on.
Can someone give me a hand?
Thank you very much.
The problem is that map() is an intermediate operation, which returns another Stream (that weird type you say appears to you). If you want to get a result that you can easily handle, then add a terminal operation at the end, for example:
Flatmap is necessary since getGrades returns a list of Grades, so you need to "pull" the items from that list, so there is no list of lists left. Also, you need to order it by subject, and as they told you in the comment, use distinct() to remove the duplicates.