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 toString() Method in Java with Examples
Next article icon

BitSet toLongArray() Method in Java with Examples

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

The java.util.BitSet.toLongArray() is an inbuilt method of BitSet class that is used to produce a new long array containing all of the bits of the existing BitSet. As per the official documentation, this process works in the following way:

if, long[] longs = bit_set.toLongArray();
then, longs.length == (bit_set.length()+63)/64 and,
bit_set.get(n) == ((longs[n/64] & (1L<<(n%64))) != 0) for all n < 64 * longs.length

Syntax:

Bit_Set.toLongArray()

Parameter: The method does not take any parameters.

Return value: The method returns the set consisting of LongArray representation of the elements of the given BitSet.

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




// Java code to illustrate toLongArray()
import java.util.*;
  
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);
  
        long[] arr = init_bitset.toLongArray();
        System.out.println("The LongArray is: " + arr);
  
        // Displaying the byteArray
        System.out.println("The elements in the LongArray:");
        for (int i = 0; i < arr.length; i++)
            System.out.print(arr[i] + ", ");
    }
}
 
 
Output:
  BitSet: {10, 20, 30, 40, 50}  The LongArray is: [J@232204a1  The elements in the LongArray:  1127000493261824,  

Program 2:




// Java code to illustrate toLongArray()
import java.util.*;
  
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(48);
        init_bitset.set(64);
        init_bitset.set(15);
        init_bitset.set(95);
        init_bitset.set(105);
        init_bitset.set(21);
  
        // Displaying the BitSet
        System.out.println("BitSet: " + init_bitset);
  
        long[] arr = init_bitset.toLongArray();
        System.out.println("The LongArray is: " + arr);
  
        // Displaying the byteArray
        System.out.println("The elements in the LongArray:");
        for (int i = 0; i < arr.length; i++)
            System.out.print(arr[i] + ", ");
    }
}
 
 
Output:
  BitSet: {15, 21, 48, 64, 95, 105}  The LongArray is: [J@232204a1  The elements in the LongArray:  281474978840576, 2201170739201,  


Next Article
BitSet toString() 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 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
  • BitSet toString() Method in Java with Examples
    The java.util.BitSet.toString() is an inbuilt method of BitSet class that is used to get a string representation of the bits of the sets in the form of a set of entries separated by “, “. So basically the toString() method is used to convert all the elements of BitSet into String. In addition to thi
    2 min read
  • BitSet stream() Method in Java with Examples
    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() m
    2 min read
  • AbstractSet toArray() method in Java with Example
    The toArray() method of Java AbstractSet is used to form an array of the same elements as that of the AbstractSet. Basically, it copies all the element from a AbstractSet to a new array. Syntax: Object[] arr = AbstractSet.toArray() Parameters: The method does not take any parameters. Return Value: T
    2 min read
  • ArrayList toArray() method in Java with Examples
    The toArray() method of ArrayList is used to return an array containing all the elements in ArrayList in the correct order. Declaring toArray() methodpublic Object[] toArray() or public <T> T[] toArray(T[] a)Parameters: This method either accepts no parameters or it takes an array T[] as a par
    3 min read
  • AbstractSet toArray(T[]) method in Java with Example
    The toArray(T[]) method method of AbstractSet class in Java is used to form an array of the same elements as that of the AbstractSet. It returns an array containing all of the elements in this AbstractSet in the correct order; the run-time type of the returned array is that of the specified array. I
    4 min read
  • 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
  • Byte toString() method in Java with examples
    The toString() method of Byte class is a built in method in Java which is used to return a String value. public String toString() Syntax: ByteObject.toString() Return Value: It returns a String object, the value of which is equal to the value of the ByteObject. Below is the implementation of toStrin
    2 min read
  • DelayQueue toArray() method in Java with Examples
    The toArray() method of DelayQueue is used to return an array containing all the elements in DelayQueue. There elements are not in any specific order in the array.Syntax: public Object[] toArray () or public T[] toArray (T[] a) Parameters: This method either accepts no parameters or it takes an arra
    4 min read
  • ByteBuffer putLong() methods in Java with Examples
    putLong(int value) The putLong(int value) method of java.nio.ByteBuffer Class is used to write eight bytes containing the given long value, in the current byte order, into this buffer at the current position, and then increments the position by eight. Syntax: public abstract ByteBuffer putLong(long
    6 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