Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8459247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:17:05+00:00 2026-06-10T13:17:05+00:00

In Java, I have an array of a million or so flags true/false to

  • 0

In Java, I have an array of a million or so flags true/false to store in mind. Should BitSet help? Although it implements a Set, will it be possible to iterate its elements as fast as if it would be an array boolean[]?

Sorry if the question has been asked. First I tried to split an array into chunks of binary represented ints and form int[] as a result of those binaries, so I could reduce the size by 32, but this is quite low-level.

I found some critics of the BitSet elsewhere and that boolean[] stores a lot of extra memory => bad for large arrays.

Any better idea to store a million of flags?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:17:07+00:00Added an answer on June 10, 2026 at 1:17 pm

    I have an array of a million or so flags true/false to store in mind. Should BitSet help?

    You can have billions of bits in a BitSet.

    Although it implements a Set, will it be possible to iterate its elements as fast as if it would be an array boolean[]?

    A boolean[] uses one byte per bit (on most JVMs) whereas BitSet uses one bit per bit. For small arrays, a boolean[] is faster, but when you are testing the size of your CPU cache a BitSet can be more efficient.

    BTW: Using a BitSet is slightly slower for small sizes because it need to extract out a bit out of each a word of memory. A byte[] has the same issue, so if you want to set bit yourself, I suggest you use a int[] like BitSet does.


    An example using BitSet

    BitSet bitSet = new BitSet();
    // set bit 100
    bitSet.set(100);
    // get bit 99
    System.out.println("bit 99 is " + bitSet.get(99));
    System.out.println("bit 100 is " + bitSet.get(100) + " after set");
    bitSet.clear(100);
    System.out.println("bit 100 is " + bitSet.get(100) + " after clear");
    

    prints

    bit 99 is false
    bit 100 is true after set
    bit 100 is false after clear
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of Integers in Java, I would like use only a
I have a Java array defined already e.g. float[] values = new float[3]; I
I have an array of unsigned integers in C and a java array of
I have an array, for example (in Java) int[] a = new int[N]; I
I'm a Java programer lost in C++ and pointers :D I have an array
I have a 10x10 array in Java, some of the items in array which
I have a byte array, received from java application via network. I need to
Suppose that I have a 2D array (matrix) in Java like this... int[][] MyMat
I have a small program to do in Java. I have a 2D array
I have a bidimensional Object array in Java. Some indices aren´t used, because they

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.