I need to switch part of my projects from C# to Java. But before which, I would like to compare two languages carefully and completely.
Regarding to lambda expression, I could write very elegant code via C#, the question is how to implement the same feature gracefully in Java? Thanks in advance!
class Program
{
enum Gender
{
Male,
Female
}
class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public Gender Gender { get; set; }
public override string ToString()
{
return string.Format("Id: {0}, Name: {1}, Age: {2}, Gender: {3}", Id, Name, Age, Gender);
}
}
static void Main(string[] args)
{
var students = new[] {
new Student { Id = 1, Name = "Nathanial Archibald", Age = 18, Gender = Gender.Male },
new Student { Id = 2, Name = "Georgina Sparks", Age = 19, Gender = Gender.Female },
new Student { Id = 3, Name = "Daniel Humphrey", Age = 20, Gender = Gender.Male },
new Student { Id = 4, Name = "Jenny Humphrey", Age = 17, Gender = Gender.Female },
};
var men = students.Where(p => p.Gender == Gender.Male);
var ageAbove18 = students.Where(p => p.Age > 18);
var humphrey = students.Where(p => p.Name.EndsWith("Humphrey"));
var idGT1orderbyAge = students.Where(p => p.Id > 1).OrderBy(p => p.Age);
foreach (var s in men) Console.WriteLine(s.ToString());
foreach (var s in ageAbove18) Console.WriteLine(s.ToString());
foreach (var s in humphrey) Console.WriteLine(s.ToString());
foreach (var s in idGT1orderbyAge) Console.WriteLine(s.ToString());
}
}