Menu

j.u.BitSet::stream

Anonymous
2016-07-04
2016-07-04
  • Anonymous

    Anonymous - 2016-07-04

    j.u.BitSet::stream

    Is there already a helper in the StreamSupport? I failed to find one.
    Or a plan to back-port it? Or a more clever / concise work-around?
    Current work-around:

    j8.u.s.IntStreams
            .range(0, bitSet.length())
            .filter(bitSet::get);
    


    Or a more convoluted one:

    j8.u.s.IntStreams
            .iterate(bitSet.nextSetBit(0), i -> bitSet.nextSetBit(i + 1))
            .takeWhile(i -> 0 <= i); // && i <= Integer.MAX_VALUE
    


    /~Karel~/

     

    Last edit: Anonymous 2016-07-04
  • Stefan Zobel

    Stefan Zobel - 2016-07-04

    No, only Collections are supported. I had to draw a line somewhere. And BitSet.stream() being useful seems to be a very rare event.

    I don't think that there is way to do better than you already did. Your first workaround looks quite reasonable to me.

     
    • Anonymous

      Anonymous - 2016-07-04

      BitSet::stream might be a pretty rare case, I agree — I wouldn't stumble upon it were I not porting Stream of Permutations [Java Precisely, p. 139, ex. 175]. I just got misled at first by the BitSet::toLongArray's doc saying: "Returns a new long array containing all the bits in this bit set." to a naïve & incorrect:

      j8.u.s.LongStreams
              .of(bitSet.toLongArray())
              .mapToInt(l -> (int) l)
      


      Since the long[] returned forms in fact a bit-packed internal storage of the set, instead of ordinal positions of bits set to true as in the case of BitSet::stream.

      The line has to be drawn somewhere, indeed. Most of the time, it's sufficiently painless to go via T -> U[] -> Stream<U> or via T -> Collection<U> -> Stream<U>. Exceptions are scarce and usually require a range() + map(), like in:

      j8.u.s.IntStreams
              .range(0, charSequence.length())
              .map(charSequence::charAt)
      


      /~Karel~/

      P.S.: Thank you very much for such a great lib! I'm using the streamsupport + retrolambda combo in a conservative (J6) production environment for about a half a year now and it helps immensely — sanity-wise! ;o)

       

      Last edit: Anonymous 2016-07-04
  • Stefan Zobel

    Stefan Zobel - 2016-07-04

    Hi Karel,

    thanks for your kind words.

    Nice to hear of a usage on Java 6! Sometimes I'm led to believe that streamsupport is mostly utilized by Android devs. Initially, Java 6 & Java 7 were the only target platforms I could imagine.

    Cheers,
    Stefan

     

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.