Skip to content

Instantly share code, notes, and snippets.

@alexlarkou
alexlarkou / School.java
Created November 19, 2018 16:00
Example
public class School {
private final String _name;
private final Stream<Student> _students;
public School(String schoolName, Stream<Student> students){
_name = schoolName;
_students = students;
}
public String get_name() {
// C#
IEnumerable<string> schoolNames = schools.Select(s => s.get_name());
// Java
Stream<String> schoolNames = schools.map(s -> s.get_name());
// c#
IEnumerable<Student> studentsInAllSchools = schools.SelectMany(s => s.get_students());
// java
Stream<Student> studentsInAllSchools = schools.flatMap(s -> s.get_students());
// c#
List<Student> studentsInAllSchools = studentsInAllSchools.ToList();
// java
List<Student> studentsInAllSchoolsList = studentsInAllSchools.collect(Collectors.toList());
// or, if you specifically want an arrayList..
List<Student> studentsInAllSchoolsArrayList = studentsInAllSchools.collect(Collectors.toCollection(ArrayList::new));
// c#
bool doesThisStudentExist = studentsInAllSchools.Any(s => s.get_name() == "Alex");
// java
boolean doesThisStudentExist = studentsInAllSchools.anyMatch(s -> s.get_name() == "Alex");
// c#
bool doAllStudentsExceed100cmInHeight = studentsInAllSchools.All(s => s.get_height() > 100);
// java
boolean doAllStudentsExceed100cmInHeight = studentsInAllSchools.allMatch(s -> s.get_height() > 100);
// c#
IEnumerable<Student> studentsInFirstYear = studentsInAllSchools.Where(s => s.get_year() == 1);
// java
Stream<Student> studentsInFirstYear = studentsInAllSchools.filter(s -> s.get_year() == 1);
// c#
IEnumerable<IGrouping<int, IEnumerable<Student>>> studentsGrouppedByYear = studentsInAllSchools.GroupBy(s => s.get_year());
// java
Map<Integer, List<Student>> studentsGrouppedByYear = studentsInAllSchools.collect(Collectors.groupingBy(s -> s.get_year()));
// c#
schools.ForEach(s => s.set_address("test address"));
// java
schools.forEach(s -> s.set_address("test address"));
// c#
int count = schools.count();
double average = allHeights.average()
// java
int count = schools.count();
OptionalDouble average = allHeights.average();