2

Can anyone help me to know how can I have a "Set" of Bytes in Java? Thank you

Red Lion
  • 213
  • 3
  • 10
  • 18

4 Answers4

10

Set<Byte> option from the Java Collections Framework

If you want to harness the Java Collections Framework, you can have a java.util.Set<Byte>. Unfortunately Java generics doesn't work with primitive. java.lang.Byte is the box type for byte. Perhaps an implementation that you can use is a TreeSet; it is a SortedSet, so you can sort the (boxed) byte by their natural ordering.

See also

Related questions


BitSet option for performance

Another option is a java.util.BitSet. This is a very time and space efficient data structure that implements set representation using bits, i.e. an int i is "in the set" if bit i is set.

BitSet is not part of the Java Collections Framework.

Here's an example usage:

    BitSet bytes = new BitSet();

    bytes.set(3);
    bytes.set(7);
    bytes.set(11);
    bytes.set(11);
    bytes.set(1000);

    System.out.println(bytes);
    // {3, 7, 11, 1000}

    System.out.println(bytes.cardinality()); // 4

    System.out.println(bytes.get(10)); // false

Note that you may set bits that are out of the byte range.

In fact, you can't directly set all bits in the byte range, since byte is a signed datatype in Java. You can use bit masking to convert any byte to an int in the 0..255 range as follows:

    byte b = -1;
    // bytes.set(b); // throws IndexOutOfBoundsException
    bytes.set(b & 0xFF);
    System.out.println(bytes);
    // {3, 7, 11, 255, 1000}

You can then iterate over all byte in the set as follows:

    for (int i = -1; (i = bytes.nextSetBit(i + 1)) != -1; ) {
        byte b = (byte) i;
        System.out.println(b);
    }
    // 3, 7, 11, -1, -24

Note that because of the byte [-128,127] to int [0,255] mapping, negatives come after positives. BitSet also facilitates efficient set operations against other BitSet, like or, and, xor, equals, intersects, etc.

Related questions

Community
  • 1
  • 1
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
4

As polygenelubricants says, you can have a Set<Byte>. Another simple alternative would be to have:

boolean[] byteSet = new boolean[256];

This would be very efficient to check or set each value - and iterating over the set of values would only take 256 iterations anyway :)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • If the set is to be modified frequently, I think I would go with this solution (or a simple class that wrapped it). Constant autoboxing could slow it down. – Nick Aug 07 '10 at 09:48
  • 1
    How does this cope with bytes having negative values? – Tom Aug 07 '10 at 10:20
  • @Tom: You'd just add 128 to any index or something similar. Various ways of coping with it. Fundamentally we just need 256 boolean values though. – Jon Skeet Aug 07 '10 at 10:28
  • 1
    @Nick: Note that in this case autoboxing probably wouldn't be **too** bad as all the boxed byte values are cached - it wouldn't be creating objects constantly. – Jon Skeet Aug 07 '10 at 11:11
3

You can have a Set<Byte> but not a Set<byte>, Byte being an object and byte a primitive. If you create a set of Byte then you can add byte values to it and they will be auto-boxed to Bytes on the fly, giving you the expected result.

    Set<Byte> byteSet = new HashSet<Byte>();
    byte b1 = 1;
    Byte b2 = 1;
    byte b3 = 2;
    byteSet.add(b1);
    byteSet.add(b2);
    byteSet.add(b3);
    System.out.println(byteSet);

Outputs:

[1, 2]

It should be noted that, although this method is quite clear and uses the familiar Java Collections Framework, the auto-boxing can become a performance issue if you are doing lots of work on the set -- but to see this effect you are really going to have to hammer it.

More performant alternatives have been suggested, like java.util.BitSet by polygenelubricants, or Jon Skeet's array solution.

Tom
  • 4,742
  • 25
  • 32
  • thanks for all answers. I dont need to manipulate the Set. All I need is to have a pair of IP addresses (source and destination). Then per each pair i need to keep two pieces of information. May be two sets which will be mapped based on their index! like setA: set of IP addresses, Set B information about those ip addresses. – Red Lion Aug 07 '10 at 10:27
  • 1
    @Red Lion: Then you want a `Map` instead of two sets. Look at the link in my answer about what `Map` interface provides. – polygenelubricants Aug 07 '10 at 10:34
  • @Polygenlubricants: i cant use map as it maps one key to only one value. wat i need is saving 192.168.1.65 209.85.229.91 140045 0.00124 4 pieces of information all together. – Red Lion Aug 07 '10 at 17:03
1

If you often need collections containing primitive types, have a look at the Primitive Collections for Java. It implements the main types of collections for each primitive type, which should be faster and more memory efficient than using the wrapper Byte class.

Other similar libraries are available.

Nick
  • 11,475
  • 1
  • 36
  • 47