This website uses cookies to ensure you get the best experience on our website.

Skip to content
LOGO
  • Company

    About IGNEK

    Learn about our story and our mission.

    Careers

    We're hiring!

    We always looking for talented peoples. Join our team!

    Blogs

    The latest industry news, updates and info.

    Case Studies

    Take a closer look at the projects we've delivered

    Webinar

    Watch our latest organized webinar

    Get in touch with our HR team

    • hr@ignek.com
    • +91 93284 95160
    • Ahmedabad, Gujarat, India – 382470
  • Services

    Enterprise Portal Development

    Custom Enterprise Portal Development for flawless Solutions

    Dedicated Development Team

    Expand Teams, Optimize Development with Our Expertise

    Back-end Development

    Empowering your digital presence with robust backend development expertise

    Front-end Development

    Crafting engaging user experiences through expert frontend development

    Digital Transformation

    Embrace digital transformation by modernizing process

    SaaS Development

    Transform your business with SaaS Innovative Solutions

    Technologies

    Liferay Services

    Development & Customization

    Theme

    Upgradation

    Migration

    Proof of Concept

    Performance Tunning

    Architecture

    Support & Maintenance

    E-commerce

    Expert Advice

    React Services

    Enterprise Development

    Custom Application

    Single Page Application

    Architecture

    API Integration

    Migration

    Consulting

    Maintenance & Support

    Spring Boot Services

    Web Service

    MVC Web Development

    API Integration

    Security

    Migration

    Microservices

    Support & Maintenance

    AEM Services

    Consulting Service

    Site Development

    Migration & Upgradation

    Integration

    Maintenance & Support

    Custom Solutions

    Content Management

    Team Augementation

  • Solution

    Ready for digital excellence

    In our rapidly evolving world, businesses are always on the lookout for fresh ways to improve their operations and connect with their audiences.

    Contact US

    Solutions

    SaaS : Software As A Service

    Transforming industries through cutting edge SaaS solutions.

    Customer Portal : Boost Business Productivity

    Streamline operations and enhance productivity with our Customer Portal solution.

    Liferay Based Intranet Portal

    Internal portal for company communication, collaboration and resources

    Integration

    Matomo Integration with Liferay

    Integrate Matomo to enable user interactions seamlessly.

    Microsoft Teams integration with Liferay

    Integrate to enable seamless collaboration, messaging within your portal.

    Jira Integration With Liferay

    Optimizes business operations by integrating with Jira.

  • Hire Us

    Liferay

    Get expert Liferay developers for seamless portal solutions.

    React JS

    Get expert ReactJS developers for dynamic web solutions.

    Web Developer

    Get custom web solutions from IGNEK's expert developers.

    Spring Boot

    Get top-notch Spring Boot developers for your project success!

    Healthcare

    Get top Healthcare Developers for cutting-edge medical software.

    MERN Stack

    Get expert MERN developers from IGNEK for custom, innovative project solutions.

    Why Hire Developers From IGNEK ?

    • Certified Developer
    • Transparent Communication
    • Flexible Hiring Modals
    • Fully Signed NDA
    • Cost Effective
    • Easy Exit Policy

    Get in touch with our Sales team

    • sales@ignek.com
    • +91 6351576580
    • info@ignek.com
    • Ahmedabad, Gujarat, India – 382470
SCHEDULE CALL
  • COMPANY
    • About
    • Case Studies
    • Blogs
    • Career
    • Webinar
  • SERVICES
    • Enterprise Portal Development
    • Dedicated Development Team
    • Back-end Development
    • Front-end Development
    • Digital Transformation
    • SaaS Development
  • SOLUTION
    • Customer Portal:  Boost Business Productivity
    • SaaS : Software as a Service
    • Liferay Based Employee Intranet Portal
  • TECHNOLOGY
    • Liferay Services
      • Liferay Development and Customization
      • Liferay Theme Development Service
      • Liferay DXP 7.4 Upgrade
      • Liferay Migration
      • Liferay Proof Of Concept
      • Liferay Performance Tuning
      • Liferay Architecture Design Service
      • Liferay Support And Maintenance Service
      • Liferay Ecommerce Development
      • Liferay Expert Advice
    • ReactJS Services
      • ReactJS Enterprise Application Development
      • ReactJS Custom Application Development
      • ReactJS Single Page Application Development (SPA)
      • ReactJS Applications Architecture
      • ReactJS API Integration
      • ReactJS Migration
      • ReactJS Consulting
      • ReactJS Maintenance and Support
    • Spring Boot Services
      • Spring Boot Web Development Service
      • Spring MVC Web Development
      • Spring Boot API Integration Service
      • Spring Boot Security
      • Spring Boot Migration Service
      • Spring Boot Microservices
      • Spring Boot Support & Maintenance Service
    • AEM Development Services
      • AEM Consulting Services
      • AEM Site Development
      • AEM Migration & Upgradation
      • AEM Integration Services
      • AEM Maintenance & Support
      • AEM Content Management
      • Custom AEM Solutions
      • AEM Team Augmentation
  • INTEGRATION
  • HIRE US
    • Hire Liferay Developer
    • Hire ReactJS Developer
    • Hire Spring Boot Developer
    • Hire Healthcare Developer
    • Hire Web Developer
    • Hire MERN Stack Developer
  • CONTACT US
  • Company

    About IGNEK

    Careers

    We're hiring!

    Blogs

    Case Studies

    Webinar

    Get in touch with our HR team

    • hr@ignek.com
    • +91 93284 95160
    • Ahmedabad, Gujarat, India – 382470
  • Services

    Enterprise Portal Development

    Dedicated Development Team

    Back-end Development

    Front-end Development

    Digital Transformation

    SaaS Development

    Technologies

    Liferay Liferay Services
    • Development & Customization
    • Theme
    • Upgradation
    • Migration
    • Proof of Concept
    • Performance Tuning
    • Architecture
    • Support & Maintenance
    • E-commerce
    • Expert Advice
    React React Services
    • Enterprise Development
    • Custom Application
    • Single Page Application
    • Architecture
    • API Integration
    • Migration
    • Consulting
    • Maintenance & Support
    Spring Boot Spring Boot Services
    • Web Service
    • MVC Web Development
    • API Integration
    • Security
    • Migration
    • Microservices
    • Support & Maintenance
    AEM AEM Services
    • Consulting Service
    • Site Development
    • Migration & Upgradation
    • Integration
    • Maintenance & Support
    • Custom Solutions
    • Content Management
    • Team Augmentation
  • Solution

    Ready for digital excellence

    In our rapidly evolving world, businesses are always on the lookout for fresh ways to improve their operations and connect with their audiences.

    Contact US

    Solutions

    SaaS : Software As A Service

    Customer Portal : Boost Business Productivity

    Liferay Based Intranet Portal

    Integration

    Matomo Integration with Liferay

    Microsoft Teams integration with Liferay

    Jira Integration With Liferay

  • Hire Us

    Liferay

    React JS

    Web Developer

    Spring Boot

    Healthcare

    MERN Stack

    Why Hire Developers From IGNEK ?

    • Certified Developer
    • Transparent Communication
    • Flexible Hiring Modals
    • Fully Signed NDA
    • Cost Effective
    • Easy Exit Policy

    Get in touch with our Sales team

    • sales@ignek.com
    • +91 6351576580
    • IGNEK
    • Ahmedabad, Gujarat, India – 382470
  • SCHEDULE CALL

Stream API in Java

  • Spring Boot
  • February 7, 2025

Share On :

Introduction

The Streams API introduced in Java 8 offers a new way to process data in collections (like lists and sets) with simple, readable code. It allows developers to perform complex data processing operations on collections using a functional programming approach. In this blog, we will explore the key concepts of the Stream API, including intermediate and terminal operations, and parallel stream processing.

Prerequisites 
  • Basic Java Knowledge
  • Java 8 features
  • IntelliJ IDEA or Any other IDE
What is Stream API?

In Java, a stream is a sequence of elements that can be generated using various operations. Streams can be created using collections, arrays, or I/O channels. Stream operations are divided into two categories: intermediate and terminal.

1. Intermediate operations

Intermediate operations change one stream into another. They are lazy, which means they do not execute any processing until a terminal operation is initiated.

2. Terminal operations

Terminal operations produce a result or a side effect. Once a terminal operation is completed, the stream is considered consumed and cannot be reused.

Stream API operations  

1. Intermediate operation

  • filter

The filter method is used to select elements that match the given predicate.

Example

				
					public class StreamApiExample {

  public static void main(String[] args) {

    List<String> names = Arrays.asList("Apple", "Banana", "Mango", "Chhery", "Chikoo");
    List<String> filteredNames =
        names.stream().filter(name -> name.startsWith("C")).collect(Collectors.toList());
    System.out.println("Using Filter method fruit name start with C :" + filteredNames);
}
}


//Output: 

Using Filter method fruit name start with C :[Chhery, Chikoo]
				
			
  • map

The map method is used to transform each element of the stream using the provided function.

Example

				
					import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamApiExample {

  public static void main(String[] args) {

    List<String> names = Arrays.asList("Apple", "Banana", "Mango", "Chhery", "Chikoo");
    List<Integer> nameLength =
        names.stream().map(String::length)
            .collect(Collectors.toList());
    System.out.println("Length of names in the list : " + nameLength);
   }
}


//Output: 

Length of names in the list : [5, 6, 5, 6, 6]


				
			
  • flatMap

The flatMap method is used to combine a stream of collections into a single stream.

Example

				
					import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamApiExample {

  public static void main(String[] args) {

    List<List<String>> names =
        Arrays.asList(Arrays.asList("Apple", "Banana"), Arrays.asList("Mango", "Watermelon"));
    List<String> flattenedList = names.stream().flatMap(List::stream).collect(Collectors.toList());
    System.out.println("Combined two list into a single list :" + flattenedList);
  }
}


//Output: 

Combined two list into a single list :[Apple, Banana, Mango, Watermelon]


				
			
  • distinct

The distinct method is used to remove duplicate elements from the stream.

Example

				
					import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamApiExample {

  public static void main(String[] args) {

    List<Integer> numbers = Arrays.asList(1,2,2,3,4,5,2,10,1,7,5,2);
    List<Integer> distinctNumbers = numbers.stream()
        .distinct()
        .collect(Collectors.toList());
    System.out.println("Remove all duplicate numbers in the list : "+distinctNumbers);
  }
}


//Output: 

Remove all duplicate numbers in the list : [1, 2, 3, 4, 5, 10, 7]


				
			
  • sorted

The sorted method is used to sort elements of the stream.

Example

				
					import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamApiExample {

  public static void main(String[] args) {

    List<String> names = Arrays.asList("Chhaya", "Mahi", "Alpa", "Zainab", "Radha");
    List<String> sortedNames = names.stream()
        .sorted()
        .collect(Collectors.toList());
    System.out.println("Sorted elements list : "+sortedNames);
  }
}


Output: 

Sorted elements list : [Alpa, Chhaya, Mahi, Radha, Zainab]


				
			
  • peek

The peek method is used for debugging purposes. It allows us to perform an action on each element of the stream without modifying it. 

Example

				
					import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamApiExample {

  public static void main(String[] args) {
    List<String> names = Arrays.asList("Chhaya", "Mahi", "Alpa", "Zainab", "Radha");
    List<String> result = names.stream().peek(System.out::println).collect(Collectors.toList());
    System.out.println("Print elements : " + result);
  }
}


//Output: 

Print elements : [Chhaya, Mahi, Alpa, Zainab, Radha]


				
			
  • limit

The limit method is used to truncate the stream to a specified number of elements.

Example: 

				
					import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamApiExample {

  public static void main(String[] args) {

    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    List<Integer> limitedNumbers = numbers.stream().limit(3).collect(Collectors.toList());
    System.out.println("Result : " + limitedNumbers);

  }
}


//Output: 

Result : [1, 2, 3]


				
			
  • skip

The skip method skips the first n elements of the stream.

Example

				
					import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamApiExample {

  public static void main(String[] args) {
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    List<Integer> skippedNumbers = numbers.stream().skip(2).collect(Collectors.toList());
    System.out.println("Result : "+skippedNumbers);
  }
}


//Output: 

Result : [3, 4, 5]


				
			

2. Terminal operation

  • forEach

The forEach method is used to perform an action for each element of the stream.

Example

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

public class StreamApiExample {

  public static void main(String[] args) {
    List<String> names = Arrays.asList("Chhaya", "Mahi", "Alpa", "Zainab", "Radha");
    names.stream().forEach(System.out::println);
  }
}


//Output: 

Chhaya
Mahi
Alpa
Zainab
Radha


				
			
  • collect

The collect method is used to add elements into collections or other data structures.

Example

				
					import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamApiExample {

  public static void main(String[] args) {
    List<String> names = Arrays.asList("Chhaya", "Mahi", "Alpa", "Zainab", "Radha");
    List<String> collectedNames = names.stream()
        .collect(Collectors.toList());
    System.out.println("Result : "+collectedNames);
  }
}


//Output: 

Result : [Chhaya, Mahi, Alpa, Zainab, Radha]


				
			
  • reduce

The reduce method is used to combine the elements of the stream into a single result using a binary operator.

Example

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

public class StreamApiExample {

  public static void main(String[] args) {
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    int sum = numbers.stream().reduce(0, Integer::sum);
    System.out.println("Sum of numbers : " + sum);
  }
}


//Output: 

Sum of numbers : 15


				
			
  • allMatch

The allMatch method checks if all elements of the stream match a given predicate.

Example

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

public class StreamApiExample {

  public static void main(String[] args) {
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    boolean allEven = numbers.stream().allMatch(n -> n % 2 == 0);
    System.out.println(allEven);
  }
}


//Output: 

false


				
			
  • anyMatch

The anyMatch method checks if any element of the stream matches a given predicate.

Example

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

public class StreamApiExample {

  public static void main(String[] args) {
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    boolean anyEven = numbers.stream().anyMatch(n -> n % 2 == 0);
    System.out.println(anyEven);
  }
}


//Output: 

true


				
			
  • noneMatch

The noneMatch method checks if no elements of the stream match a given predicate.

Example

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

public class StreamApiExample {

  public static void main(String[] args) {
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    boolean noneNegative = numbers.stream()
        .noneMatch(n -> n < 0);
    System.out.println(noneNegative);
  }
}


//Output: 

true

				
			
  • findFirst

The findFirst method returns the first element of the stream, if any.

Example

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

public class StreamApiExample {

  public static void main(String[] args) {
    List<String> names = Arrays.asList("Chhaya", "Mahi", "Alpa", "Zainab", "Radha");
    Optional<String> first = names.stream().findFirst();
    System.out.println(first);
  }
}


//Output: 

Optional[Chhaya]


				
			
  • findAny

The findAny method returns any element of the stream, which is useful in parallel streams.

Example

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

public class StreamApiExample {

  public static void main(String[] args) {
    List<String> names = Arrays.asList("Chhaya", "Mahi", "Alpa", "Zainab", "Radha");
    Optional<String> any = names.stream()
        .findAny();
    System.out.println(any);
  }
}


//Output: 

Optional[Chhaya]


				
			
  • max and min

The max and min methods return the maximum and minimum elements of the stream, respectively, based on a comparator.

Example

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

public class StreamApiExample {

  public static void main(String[] args) {
    List<Integer> numbers = Arrays.asList(11, 22, 33, 44, 55);
    Optional<Integer> max = numbers.stream()
        .max(Integer::compare);
    System.out.println(max);
    Optional<Integer> min = numbers.stream()
        .min(Integer::compare);
    System.out.println(min);
  }
}


//Output: 

Optional[55]
Optional[11]


				
			
  • toArray

The toArray method converts the stream into an array.

Example

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

public class StreamApiExample {

  public static void main(String[] args) {
    List<String> names = Arrays.asList("Chhaya", "Mahi", "Alpa", "Zainab", "Radha");
    String[] namesArray = names.stream().toArray(String[]::new);
    System.out.println(Arrays.toString(namesArray));
  }
}


//Output: 

[Chhaya, Mahi, Alpa, Zainab, Radha]


				
			

3. Parallel

The Java Stream API also allows parallel processing, which can significantly improve performance for large datasets. To create a parallel stream, We can call the parallelStream() method on a collection.

Example

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

public class StreamApiExample {

  public static void main(String[] args) {

    List<Integer> numbers = Arrays.asList(1,2,3,4,5);
    int sum = numbers.parallelStream().reduce(0,Integer::sum);
    System.out.println("Sum of numbers : "+sum);
  }
}


//Output: 

Sum of numbers : 15


				
			
Conclusion 

The Java Stream API provides a powerful and flexible way to process collections and data sequences. By understanding the various intermediate and terminal operations, and the benefits and caveats of parallel streams, we can write more efficient and readable code. Whether we’re filtering, mapping, or reducing data that can enhance our Java Application.

Explore Our Services

Discover how we can help your business thrive, whether you’re running a small startup, an SME, or a large enterprise. We’re here to understand your unique needs and goals, offering the expertise and resources to support your journey to success.
Stay informed about our Spring Boot services and updates by subscribing to our newsletter—just fill in the details below to subscribe.

Loading
Loading...

Related Blogs

June 7, 2024
Spring Boot With Swagger Integration
Spring Boot With Swagger Integration
December 23, 2024
Apache Kafka with Custom Objects in Spring Boot
Apache Kafka with Custom Objects in Spring Boot
December 23, 2024
Best Practices for Working with Spring Boot
Best Practices for Working with Spring Boot
Loading...

Featured Projects Portfolios

November 25, 2024
Liferay DXP Based Intranet Portal Migration from 7.0 to 7.4
Liferay DXP Based Intranet Portal Migration from 7.0 to 7.4
November 29, 2024
Public Enterprise Website for Laundry Services
Public Enterprise Website for Laundry Services
November 29, 2024
Employee Intranet Portal for Government Entity
Employee Intranet Portal for Government Entity

Digital Solutions for Your Business with IGNEK

4.9

5.0

5.0

5.0

LOGO

Making the world a better place through constructing elegant hierarchies

COMPANY

  • About
  • Career
  • Case Study
  • Blogs

SERVICES

  • Enterprise Portal Development
  • Dedicated Development Team
  • Back-end Development
  • Front-end Development
  • Digital Transformation
  • SaaS Development

HIRE US

  • Liferay
  • Spring Boot
  • ReactJS
  • Healthcare
  • MERN Stack
  • AEM

CONTACT

  • info@ignek.com
  • info@ignek.com
SALES
  • sales@ignek.com
  • (+91) 635 157 6580
CAREER
  • hr@ignek.com
  • (+91) 932 849 5160
  • E 910-912, Ganesh Glory 11, Jagatpur Road, SG Highway, Ahmedabad, Gujarat - 382470

© 2018-2025 IGNEK, Inc. All rights reserved

Linkedin Facebook X-twitter Instagram