Reputation: 2807
I could think of these things,
Arrays.asList(byte[])
converts byte[]
to List<byte[]>
,I was just wondering Is there any library function to do that?
Upvotes: 48
Views: 61667
Reputation: 31
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());
Upvotes: 3
Reputation: 3841
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 byte
to Byte
.
Upvotes: 1
Reputation: 1
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));
Upvotes: 0
Reputation: 3426
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));
Upvotes: 22
Reputation: 1216
As this post suggests: the guava Bytes class can help out:
byte[] bytes = ...
List<Byte> byteList = Bytes.asList(bytes);
Upvotes: 20
Reputation: 5668
For Byte[]
instead of byte[]
this would work:
Byte[] array = ....
List<Byte> list = Arrays.asList(array);
Upvotes: 18