What is equivalent to C#'s Select clause in JAVA's streams API

Viewed 4812

I wanted to filter list of Person class and finally map to some anonymous class in Java using Streams. I am able to do the same thing very easily in C#.

Person class

class Person
{
    public int Id { get; set; }

    public string Name { get; set; }

    public string Address { get; set; }
}

Code to map the result in desire format.

 List<Person> lst = new List<Person>();

 lst.Add(new Person() { Name = "Pava", Address = "India", Id = 1 });
 lst.Add(new Person() { Name = "tiwari", Address = "USA", Id = 2 });
 var result = lst.Select(p => new { Address = p.Address, Name = p.Name }).ToList();

Now if I wanted to access any property of newly created type I can easily access by using below mentioned syntax.

Console.WriteLine( result[0].Address);

Ideally I should use loop to iterate over the result.

I know that in java we have collect for ToList and map for Select. But i am unable to select only two property of Person class. How can i do it Java

4 Answers
Related