limit method of Stream class

package com.demo.java8;

import java.util.Arrays;
import java.util.List;

public class Demo8 {
	public static void main(String[] args) {
		List<Emp8> empList = Arrays.asList(new Emp8[] { 
				new Emp8("1","Mumbai","20"),
				new Emp8("2","Delhi","23"),
				new Emp8("3","Kolkata","25"),
				new Emp8("4","Chennai","18"),
				new Emp8("5","Mumbai","11"),
				new Emp8("6","Kolkata","30"),
				new Emp8("7","Chennai","35"),
				new Emp8("8","Mumbai","15"),
				new Emp8("9","Delhi","22"),
				new Emp8("10","Kolkata","27")
				});
		
		//Example : to limit output to 3 only
		empList.parallelStream().filter(x->Integer.parseInt(x.getAge())>22).limit(3).forEach(System.out::println);
	}
}
class Emp8{
	private String id;
	private String city;
	private String age;
	public Emp8(String id, String city, String age) {
		super();
		this.id = id;
		this.city = city;
		this.age = age;
	}
	public String getId() {return id;}
	public String getCity() {return city;}
	public String getAge() {return age;}
	@Override
	public String toString() {
		return "Emp [id=" + id + ", city=" + city + ", age=" + age + "]";
	}
}

Leave a Comment