Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Difference between Stream.of() and Arrays.stream() method in Java
Next article icon

Difference between Stream.of() and Arrays.stream() method in Java

Last Updated : 03 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Arrays.stream()

The stream(T[] array) method of Arrays class in Java, is used to get a Sequential Stream from the array passed as the parameter with its elements. It returns a sequential Stream with the elements of the array, passed as parameter, as its source. Example: 

Java
// Java program to demonstrate Arrays.stream() method  import java.util.*; import java.util.stream.*;  class GFG {     public static void main(String[] args)     {          // Creating a String array         String[] arr = { "Geeks", "for", "Geeks" };          // Using Arrays.stream() to convert         // array into Stream         Stream<String> stream = Arrays.stream(arr);          // Displaying elements in Stream         stream.forEach(str -> System.out.print(str + " "));     } } 

Output:

Geeks for Geeks 

Stream.of()

The Stream of(T... values) returns a sequential ordered stream whose elements are the specified values. Stream.of() method simply calls the Arrays.stream() method for non-primitive types. Example: 

Java
// Java code for Stream of(T... values) // to get a sequential ordered stream whose // elements are the specified values.  import java.util.*; import java.util.stream.Stream;  class GFG {      // Driver code     public static void main(String[] args)     {         // Creating an Stream         Stream stream = Stream.of("Geeks", "for", "Geeks");          // Displaying the sequential ordered stream         stream.forEach(str -> System.out.print(str + " "));     } } 

Output:

Geeks for Geeks 

These both methods are the two most commonly used methods for creating a sequential stream from a specified array. Both these methods returns a Stream<T> when called with a non-primitive type T.

Difference between Arrays.stream() and Stream.of()

Even if Stream.of() is a wrapper over the Arrays.stream() method, there are certain point of differences which clarifies as when to use a Arrays.stream() or when to use Stream.of(). Below are some of the differences between the above two stated methods:

  1. Different return types: For primitives arrays (like int[], long[] etc), Arrays.stream() and Stream.of() have different return types. Example: Passing an integer array, the Stream.of() method returns Stream whereas Arrays.stream() returns an IntStream. 
Java
// Java program to demonstrate return type // of Arrays.stream() and Stream.of() method // for primitive arrays  import java.util.*; import java.util.stream.*;  class GFG {      public static void main(String[] args)     {         // Creating an integer array         int arr[] = { 1, 2, 3, 4, 5 };          // --------- Using Arrays.stream() ---------          // to convert int array into Stream         IntStream intStream = Arrays.stream(arr);          // Displaying elements in Stream         intStream.forEach(str -> System.out.print(str + " "));          // --------- Using Stream.of() ---------          // to convert int array into Stream         Stream<int[]> stream = Stream.of(arr);          // Displaying elements in Stream         stream.forEach(str -> System.out.print(str + " "));     } } 

Output:

1 2 3 4 5 [I@404b9385 
  1. Stream.of() needs flattening whereas Arrays.stream() does not: As the ideal class used for processing of Streams of primitive types are their primitive Stream types (like IntStream, LongStream, etc). Therefore Stream.of() needs to be explicitly flattened into its primitive Stream before consuming. Example: 
Java
// Java program to demonstrate need of flattening // Stream.of() method returned type for primitive arrays  import java.util.*; import java.util.stream.*;  class GFG {      public static void main(String[] args)     {         // Creating an integer array         int arr[] = { 1, 2, 3, 4, 5 };          // --------- Using Arrays.stream() ---------          // to convert int array into Stream         IntStream intStream = Arrays.stream(arr);          // Displaying elements in Stream         intStream.forEach(str -> System.out.print(str + " "));          // --------- Using Stream.of() ---------          // to convert int array into Stream         Stream<int[]> stream = Stream.of(arr);          // ***** Flattening of Stream<int[]> into IntStream *****          // flattenning Stream<int[]> into IntStream         // using flatMapToInt()         IntStream intStreamNew = stream.flatMapToInt(Arrays::stream);          // Displaying elements in IntStream         intStreamNew.forEach(str -> System.out.print(str + " "));     } } 

Output:

1 2 3 4 5 1 2 3 4 5 
  1. Stream.of() is generic whereas Arrays.stream is not: Arrays.stream() method only works for primitive arrays of int[], long[], and double[] type, and returns IntStream, LongStream and DoubleStream respectively. For other primitive types, Arrays.stream() won’t work. On the other hand, Stream.of() returns a generic Stream of type T (Stream). Hence, it can be used with any type. Example:
    • For Arrays.stream() method: 
Java
// Java program to demonstrate return type // of Arrays.stream() method // for primitive arrays of char  import java.util.*; import java.util.stream.*;  class GFG {      public static void main(String[] args)     {         // Creating a character array         char arr[] = { '1', '2', '3', '4', '5' };          // --------- Using Arrays.stream() ---------         // This will throw error          // to convert char array into Stream         Arrays.stream(arr);     } } 
  • Output:

Compilation Error in java code :- prog.java:20: error: no suitable method found for stream(char[]) Arrays.stream(arr); ^

  • For Stream.of() method: 
Java
// Java program to demonstrate return type // of Stream.of() method // for primitive arrays of char  import java.util.*; import java.util.stream.*;  class GFG {      public static void main(String[] args)     {         // Creating a character array         char arr[] = { '1', '2', '3', '4', '5' };          // --------- Using Stream.of() ---------         // Will work efficiently          // to convert int array into Stream         Stream<char[]> stream = Stream.of(arr);          // Displaying elements in Stream         stream.forEach(str -> System.out.print(str + " "));     } } 

Output:

[C@548c4f57 

Next Article
Difference between Stream.of() and Arrays.stream() method in Java

R

RishabhPrabhu
Improve
Article Tags :
  • Java
Practice Tags :
  • Java

Similar Reads

    Difference between Array and String in Java
    An array is a collection of similar type of elements that are stored in a contiguous memory location. Arrays can contain primitives(int, char, etc) as well as object(non-primitives) references of a class depending upon the definition of the array. In the case of primitive data type, the actual value
    5 min read
    Difference Between map() And flatMap() In Java Stream
    In Java, the Stream interface has a map() and flatmap() methods and both have intermediate stream operation and return another stream as method output. Both of the functions map() and flatMap are used for transformation and mapping operations. map() function produces one output for one input value,
    3 min read
    Difference Between Streams and Collections in Java
    Collection is an in-memory data structure, which holds all the values that the data structure currently has. Every element in the Collection has to be computed before we add it to the Collection. Operations such as searching, sorting, insertion, manipulation, and deletion can be performed on a Colle
    3 min read
    Flatten a Stream of Arrays in Java using forEach loop
    Given a Stream of Arrays in Java, the task is to Flatten the Stream using forEach() method. Examples: Input: arr[][] = {{ 1, 2 }, { 3, 4, 5, 6 }, { 7, 8, 9 }} Output: [1, 2, 3, 4, 5, 6, 7, 8, 9] Input: arr[][] = {{'G', 'e', 'e', 'k', 's'}, {'F', 'o', 'r'}} Output: [G, e, e, k, s, F, o, r] Approach:
    3 min read
    Difference between Arrays and Collection in Java
    An array in Java is a group of like-typed variables referred to by a common name. Arrays in Java work differently than they do in C/C++. Following are some important points about Java arrays. On the other hand, any group of individual objects which are represented as a single unit is known as the co
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences