Barış Kısır

Senior Software Developer

Navigation
 » Home
 » RSS

Java Stream Library (Equivalent of C# LINQ)

12 Nov 2016 » java

If you are familiar with LINQ, Stream Library is for you!

Requirements

Java 8 or newer

Example

class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
List<User> userList = new List<User>();

Where

userList.Where(x=>x.Id>5).ToList();
userList.stream().filter(x->x.Id>5).collect(Collectors.toList());

Select

userList.Select(x=>x.Name).ToList();
userList.stream().map(x->x.Name).collect(Collectors.toList());

ToList

userList.ToList();
userList.collect(Collectors.toList());

Take

userList.Take(10).ToList();
userList.limit(10).collect(Collectors.toList());

OrderBy

userList.OrderBy(x=>x.Id).ToList();
userList.sorted().collect(Collectors.toList());

Distinct

userList.Distinct().ToList();
userList.distinct().collect(Collectors.toList());

First

userList.First().ToList();
userList.findFirst().collect(Collectors.toList());

You can look at here for docs

Java Stream Library