JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Java 8 Stream limit method example.


The Stream.limit() method returns a Stream with elements of the source stream, truncated to be no longer than the specified size.

While limit() is generally a cheap operation on sequential stream pipelines, it can be quite expensive on ordered parallel pipelines, especially for large values of maxSize, since limit(n) is constrained to return not just any n elements, but the first n elements in the encounter order. Using an unordered stream source (such as generate(Supplier)) or removing the ordering constraint with BaseStream.unordered() may result in significant speedups of limit() in parallel pipelines, if the semantics of your situation permit. If consistency with encounter order is required, and you are experiencing poor performance or memory utilization with limit() in parallel pipelines, switching to sequential execution with BaseStream.sequential() may improve performance.

package com.java2novice.streams;

import java.util.UUID;
import java.util.stream.Stream;

public class StreamLimitEx {

	public static void main(String a[]) {

		Stream.of("bus", "car", "bicycle", "flight", "train").limit(3).forEach(System.out::println);
		System.out.println("--------------------------------");
		Stream.generate(UUID::randomUUID).limit(10).forEach(System.out::println);
	}
}

Output:
bus
car
bicycle
--------------------------------
a8e3bdb0-bf47-438d-9428-ef4846dd903f
1d06eab8-41e4-41dd-9221-fd3f7557dc45
a0ca62ee-a4a8-474a-a4df-bc82c70e6035
b0a3b4e5-9586-4b96-baa7-cbbc96daa24e
c8382595-ed6c-4ec3-b91e-b299198243a9
1bc9a030-b470-4fbf-a8bf-4e6cc8752b00
8384aec5-190b-49fb-889d-a473b83754e3
0c1ee9f1-210b-4239-9023-cf55329731b4
5be41901-cf26-4659-ae00-cf2a2c036df6
fcfcb3b1-128d-4758-adc7-9d0f7df158ca
<< Previous Program | Next Program >>

Java 8 Streams Examples

  1. How Java 8 Streams work?
  2. Java 8 Streams parallelism introduction.
  3. Explain non-interference behavior of Java 8 Streams.
  4. Create Java 8 Stream using Stream.of() method example.
  5. Create Java 8 Stream using List example.
  6. Create Java 8 Stream using Stream.generate() method.
  7. Java 8 Stream.filter() example.
  8. Java 8 Stream.map() example.
  9. Java 8 Stream flatmap method example.
  10. Java 8 Stream peek method example.
  11. Java 8 Stream distinct method example.
  12. Java 8 Stream sorted method example.
  13. Java 8 Stream limit method example.
  14. Java 8 Stream forEach method example.
  15. Java 8 Stream toArray method example.
  16. Java 8 Stream reduce method example.
  17. Java 8 Stream collect method example.
  18. Java 8 Stream concat method example.
  19. Java 8 Stream anyMatch(), allMatch() and noneMatch() example.
  20. Java 8 Stream findFirst(), findAny() example.
  21. Primitive type Stream example.
Knowledge Centre
What is fail-fast in java?
A fail-fast system is nothing but immediately report any failure that is likely to lead to failure. When a problem occurs, a fail-fast system fails immediately. In Java, we can find this behavior with iterators. Incase, you have called iterator on a collection object, and another thread tries to modify the collection object, then concurrent modification exception will be thrown. This is called fail-fast.
Famous Quotations
The greatest obstacle to discovery is not ignorance; it is the illusion of knowledge.
-- Daniel J. Boorstin

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.