Skip to content

Instantly share code, notes, and snippets.

@rr-codes
Created March 19, 2021 21:43
Show Gist options
  • Save rr-codes/0df0d59fa00845430fc02b1272c1c09f to your computer and use it in GitHub Desktop.
Save rr-codes/0df0d59fa00845430fc02b1272c1c09f to your computer and use it in GitHub Desktop.
package com.eecs1021;
import java.io.PrintStream;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class Student {
private final String name;
private final double gpa;
Student(String name, double gpa) {
this.name = name;
this.gpa = gpa;
}
public String getName() {
return name;
}
public double getGpa() {
return gpa;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return Double.compare(student.gpa, gpa) == 0 && name.equals(student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, gpa);
}
@Override
public String toString() {
return String.format("Student{name='%s', gpa=%.2f}", name, gpa);
}
}
public class StreamOps {
/**
* Returns a list of all the student names. To convert a Stream to a List, use {@code .collect(Collectors.toList()} on the stream.
* To map a student to a name, use {@code .map(s -> s.getName())} (alternatively, {@code .map(Student::getName)})
*/
public static List<String> getNames(Stream<Student> students) {
return null;
}
/**
* Returns the sum of all students' gpas. To map a Stream to a double, use {@code .mapToDouble(s -> s.getGpa())} (alternatively, {@code .mapToDouble(Student::getGpa)}).
* To get the sum of all values in a {@code DoubleStream}, use the {@code .sum()} method
*/
public static double sumOfAllGPAs(Stream<Student> students) {
return 0.0;
}
/**
* Returns the first element of the stream, or {@code null} if the stream is empty. To get the first element
* of a Stream, use {@code .findFirst()}, which returns an {@code Optional<T>}. To convert an {@code Optional<T>}
* to a non optional value of `T`, use {@code .orElse()}.
*
* In this case, it should return `null` if the Optional is empty.
*
* Because Streams can only typically be iterated once, you cannot use an if-else statement.
*/
public static Student first(Stream<Student> students) {
return null;
}
/**
* Returns a Stream of all student names that start with "A". First, map the stream to a stream of names,
* then filter the stream of names to only include names that start with "A" (using the {@code startsWith()} method
* of String)
*/
public static Stream<String> namesThatStartWithA(Stream<Student> students) {
return null;
}
/**
* For each student in the stream, print their description to the {@code out} stream.
* You can use the method reference {@code out::println} as the argument for the stream's
* {@code forEach} method (alternatively, {@code s -> out.println(s)})
*/
public static void printStudents(Stream<Student> students, PrintStream out) {
}
/**
* Returns true if there exists a student in the stream who's name is equal to the specified name.
* To test if any element in a stream satisfies a predicate, use the {@code .anyMatch()} method of streams.
*/
public static boolean doesContainName(Stream<Student> students, String name) {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment