48

I could think of these things,

  1. Arrays.asList(byte[]) converts byte[] to List<byte[]>,
  2. looping through byte array and add each element to list

I was just wondering Is there any library function to do that?

7
  • 1
    Is Arrays.asList not a "library function"? Commented Nov 20, 2010 at 7:24
  • 8
    @vitaut: The point is that Arrays.asList does the wrong thing here. Commented Nov 20, 2010 at 7:25
  • there was some problem in my formatting, it converts it to List<byte[]> and not List<Byte> Commented Nov 20, 2010 at 7:27
  • 1
    IMHO byte[] is typically used for low level data transfer e.g. to disk/network. In such code you would never use List<Byte> as it uses alot more memory and doesn't provide useful functionality for these tasks. List<Byte> may be useful for you, but its not a common usecase. Commented Nov 20, 2010 at 9:17
  • late comment but i think this point was missed out . java.util.Arrays.asList(T... a) , here T... a is not same as byte[] or Byte[] , so any array say T[] would be treated as a single value of type T[] and not as multiple values of type T Commented Jul 7, 2015 at 15:50

6 Answers 6

22

Library Apache Commons Lang has ArrayUtils.toObject, which turns a primitive array to a typed object array:

int array[] = { 1, 2, 3 };
List<Integer> list = Arrays.asList(ArrayUtils.toObject(array));
Sign up to request clarification or add additional context in comments.

1 Comment

For this answer above : if bytes is an arrays of byte (byte[] bytes), you can do this following : List<Byte> byteList = Arrays.asList(ArrayUtils.toObject(bytes));
20

As this post suggests: the guava Bytes class can help out:

byte[] bytes = ...
List<Byte> byteList = Bytes.asList(bytes);

Comments

18

For Byte[] instead of byte[] this would work:

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

5 Comments

cannot convert from List<byte[]> to List<Byte>
Yeah, you are right. It only works with Byte[]. I modified the answer.
ohh k.. that's interesting... now can i convert byte[] to Byte[]?
This solution assumes that your array is of object Byte, not primitive byte!
4

Java 8 one-line byte[] to List<Byte> conversion, given array as input:

List<Byte> list = IntStream.range(0, array.length).mapToObj(i -> array[i]).collect(Collectors.toList());

Comments

1

I think the simplest pure Java way, without additional libraries, is this:

private static List<Byte> convertBytesToList(byte[] bytes) {
    final List<Byte> list = new ArrayList<>();
    for (byte b : bytes) {
        list.add(b);
    }
    return list;
}

But better check twice if you really need to convert from byteto Byte.

1 Comment

stackoverflow.com/questions/3770289/… contains some interesting Java 8 ways.
0
byte[] byteArray;
List<Byte> medianList=new ArrayList<>(); 
int median=0,count=0;
Path file=Paths.get("velocities.txt");
if(Files.exists(file)){
    byteArray=Files.readAllBytes(file);
}
medianList.addAll(Arrays.asList(byteArray));

1 Comment

Doesn't work, because you've declared the list with Byte and you are trying to add byte.

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.