Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • 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:
BitSet toByteArray() Method in Java with Examples
Next article icon

BitSet stream() Method in Java with Examples

Last Updated : 27 Dec, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report

The stream() method of Java BitSet class is used to return a stream of indices for every bit contained in the BitSet. The indices are returned in increasing order. The size of the stream is the number of bits in the set state of the BitSet, which is equal to the value returned by the cardinality() method.

Syntax:

public IntStream stream()

Parameters: The method does not take any parameters.
Return Value: The method returns a stream of indices for every bit contained in the BitSet.

Below programs illustrate the working of java.util.BitSet.stream() method:
Program 1:




// Java code to illustrate stream()
import java.util.*;
import java.util.stream.IntStream;
  
public class BitSet_Demo {
    public static void main(String args[])
    {
        // Creating an empty BitSet
        BitSet init_bitset = new BitSet();
  
        // Use set() method to add elements into the Set
        init_bitset.set(10);
        init_bitset.set(20);
        init_bitset.set(30);
        init_bitset.set(40);
        init_bitset.set(50);
  
        // Displaying the BitSet
        System.out.println("BitSet: " + init_bitset);
  
        // Creating an IntStream
        IntStream indice_Stream = init_bitset.stream();
  
        // Displaying the working
        System.out.println("The BitSet: " + init_bitset);
        System.out.println("The stream of indices: "
                           + indice_Stream);
        System.out.println("The size of the stream: "
                           + indice_Stream.count());
    }
}
 
 
Output:
  BitSet: {10, 20, 30, 40, 50}  The BitSet: {10, 20, 30, 40, 50}  The stream of indices: java.util.stream.IntPipeline$Head@4eec7777  The size of the stream: 5  

Program 2:




// Java code to illustrate stream()
import java.util.*;
import java.util.stream.IntStream;
  
public class BitSet_Demo {
    public static void main(String args[])
    {
        // Creating an empty BitSet
        BitSet init_bitset = new BitSet();
  
        // Use set() method to add elements into the Set
        init_bitset.set(40);
        init_bitset.set(25);
        init_bitset.set(31);
        init_bitset.set(100);
        init_bitset.set(121);
        init_bitset.set(400);
        init_bitset.set(2);
        init_bitset.set(15);
        init_bitset.set(106);
        init_bitset.set(55);
  
        // Displaying the BitSet
        System.out.println("BitSet: " + init_bitset);
  
        // Creating an IntStream
        IntStream indice_Stream = init_bitset.stream();
  
        // Displaying the working
        System.out.println("The BitSet: " + init_bitset);
        System.out.println("The stream of indices: "
                           + indice_Stream);
        System.out.println("The size of the stream: "
                           + indice_Stream.count());
    }
}
 
 
Output:
  BitSet: {2, 15, 25, 31, 40, 55, 100, 106, 121, 400}  The BitSet: {2, 15, 25, 31, 40, 55, 100, 106, 121, 400}  The stream of indices: java.util.stream.IntPipeline$Head@4eec7777  The size of the stream: 10  


Next Article
BitSet toByteArray() Method in Java with Examples
author
chinmoy lenka
Improve
Article Tags :
  • Java
  • Java - util package
  • Java-BitSet
  • Java-Functions
Practice Tags :
  • Java

Similar Reads

  • BitSet size() Method in Java with Examples
    The size() Method of BitSet class in Java is used to know the size of this BitSet. This size is equal to the number of bits, each element has occupied in the BitSet. The maximum element in the set is the size - the first element Syntax: BitSet.hashCode() Parameters: The method does not accept any pa
    2 min read
  • Stream count() method in Java with examples
    long count() returns the count of elements in the stream. This is a special case of a reduction (A reduction operation takes a sequence of input elements and combines them into a single summary result by repeated application of a combining operation). This is a terminal operation i.e, it may travers
    2 min read
  • BitSet toByteArray() Method in Java with Examples
    The java.util.BitSet.toByteArray() is an inbuilt method of BitSet class that is used to produce a new byte array containing all of the bits of the existing BitSet. As per the official documentation, this process works in the following way: if, byte[] bytes = bit_set.toByteArray(); then, bytes.length
    2 min read
  • Stream dropWhile() method in Java with examples
    The dropWhile(java.util.function.Predicate) method returns two different types of stream depending upon whether the stream is ordered or not. If the stream is ordered then a stream of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate i
    3 min read
  • Stream peek() Method in Java with Examples
    In Java, Stream provides an powerful alternative to process data where here we will be discussing one of the very frequently used methods named peek() which being a consumer action basically returns a stream consisting of the elements of this stream, additionally performing the provided action on ea
    2 min read
  • Stream skip() method in Java with examples
    Prerequisite : Streams in java The skip(long N) is a method of java.util.stream.Stream object. This method takes one long (N) as an argument and returns a stream after removing first N elements. skip() can be quite expensive on ordered parallel pipelines, if the value of N is large, because skip(N)
    3 min read
  • BitSet class methods in Java with Examples | Set 3
    BitSet class methods in Set 3. / / | | | \ \ and notand flip isEmpty equal get intersect BitSet class methods in Java with Examples | Set 2 BitSet class methods are explained as follows : and / notand : java.util.BitSet.and() and java.util.BitSet.notand() method is a java.util.Bitset class method. .
    5 min read
  • Reader close() method in Java with Examples
    The close() method of Reader Class in Java is used to close the stream and release the resources that were busy in the stream, if any. This method has following results: If the stream is open, it closes the stream releasing the resources If the stream is already closed, it will have no effect. If an
    2 min read
  • BitSet length() Method in Java with Examples
    The length() Method of BitSet class in Java is used to know the logical size of this BitSet. This logical size is equal to the highest bit of the BitSet plus one. Syntax: BitSet.hashCode() Parameters: The method does not accept any parameters. Return Value: The method returns the logical size of the
    2 min read
  • Stream forEach() method in Java with examples
    Stream forEach(Consumer action) performs an action for each element of the stream. Stream forEach(Consumer action) is a terminal operation i.e, it may traverse the stream to produce a result or a side-effect. Syntax : void forEach(Consumer<? super T> action) Where, Consumer is a functional int
    2 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