1

What's the easiest way add all of the elements in an array to a list?

e.g.

List<Byte> l = new ArrayList<Byte>();
//Want to add each element in below array to l
byte[] b = "something".getBytes("UTF-8");

Is there some utility method or something that does this. I tried to use addAll, but that wants to add the actual array to the collection.

Thank you.

EDIT:

To clarify, I do want to do this with byte[] because I am going to eventually convert the list back to a byte[] and feed it into MessageDigest.update(). Does that help clarify?

EDIT 2:

So it seems like List<Byte> is bad; very bad. What data structure would be recommended if I am basically adding arrays of bytes (I am making a hash of some some SALTS and user information) to feed into MessageDigest?

0

2 Answers 2

6

For byte[], you're in trouble. Arrays.asList won't work because you've got a byte[] rather than a Byte[].

This is one of those "primitive vs class" problems. You can probably find 3rd party collection libraries which support this (such as the Apache Commons Lang project), but I don't believe there's anything out of the box. Alternatively, you could always just write the code yourself:

List<Byte> l = new ArrayList<Byte>();
for (byte b : "something".getBytes("UTF-8"))
{
    l.add(b);
}

Note that this is a pretty horribly inefficient storage format for a sequence of bytes. Even though autoboxing will ensure you don't create any extra Byte objects, you're still going to have 4 or 8 bytes per element, as they'll be references. It's very rare for a List<Byte> to be appropriate - are you sure you need it here?

If you're actually dealing with classes, e.g. you've got a String[] and you want a List<String> then you can use Arrays.asList():

String[] array = ...;
List<String> list = Arrays.asList(array);

That will return a read-only list wrapping the array though - if you want a mutable list, you can just pass that wrapper to the ArrayList constructor:

String[] array = ...;
List<String> list = new ArrayList<String>(Arrays.asList(array));
Sign up to request clarification or add additional context in comments.

4 Comments

Perhaps a ByteBuffer or ByteArrayOutputStream is a better choice.
Those damn Java generics and wrapper types! Anyhow, if you're not shy of throwing in some useful libraries, check out this answer using apache.commons.lang.ArrayUtils
Given the title, I'm guessing byte is just an example
I added some clarification, thanks for all the input, and I rarely work with Bytes so I can change the data structure if needed.
3

Unless you want to add every element one at a time in a loop, you can use Apache commons' ArrayUtils to convert the primitive array into an Object array, then use addAll():

byte[] b = "something".getBytes("UTF-8");
Byte[] bArray = ArrayUtils.toObject(b);

List<Byte> l = new ArrayList<Byte>();
l.addAll(bArray);

1 Comment

Thanks for the answer. I just don't think its worth adding the project, when I am just going to use it once.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.