43

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?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Eternal Noob
  • 2,717
  • 5
  • 27
  • 41
  • 1
    Is `Arrays.asList` not a "library function"? – vitaut Nov 20 '10 at 07:24
  • 8
    @vitaut: The point is that Arrays.asList does the wrong thing here. – Jon Skeet Nov 20 '10 at 07:25
  • there was some problem in my formatting, it converts it to List and not List – Eternal Noob Nov 20 '10 at 07: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 as it uses alot more memory and doesn't provide useful functionality for these tasks. List may be useful for you, but its not a common usecase. – Peter Lawrey Nov 20 '10 at 09: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** – Srinath Ganesh Jul 07 '15 at 15:50
  • Think about using `java.io.ByteArrayOutputStream` instead of `List`. An example: [Most concise way to insert array of bytes into List?](https://stackoverflow.com/a/44744554/772981) – Jarekczek Jun 25 '17 at 08:26
  • It's remarkably _stupid_ that there's no easy way to get the result of `String.getBytes()` (a `byte[]`) as a collection or stream. Would it have killed them to have provided a `ByteStream` class next to `{Int,Long,Double}Stream`? – davidbak May 31 '23 at 16:44

6 Answers6

20

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));
Juanal
  • 4,344
  • 3
  • 19
  • 18
peenut
  • 3,366
  • 23
  • 24
  • 1
    For this answer above : if bytes is an arrays of byte (byte[] bytes), you can do this following : `List byteList = Arrays.asList(ArrayUtils.toObject(bytes));` – jpmottin Apr 03 '16 at 16:15
19

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

byte[] bytes = ...
List<Byte> byteList = Bytes.asList(bytes);
Community
  • 1
  • 1
Jens Nyman
  • 1,186
  • 2
  • 14
  • 16
17

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

  Byte[] array = ....
  List<Byte> list = Arrays.asList(array);
chkal
  • 5,598
  • 21
  • 26
3

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());
fps
  • 31
  • 2
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.

Datz
  • 3,156
  • 3
  • 22
  • 50
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));
Floern
  • 33,559
  • 24
  • 104
  • 119
Ammar
  • 1