我有一个byte[]的ArrayList,我想知道是否可以使用Java 8的流将其转换为byte[]。ArrayList中的所有数组大小都相同。
ArrayList<byte[]> buffer = new ArrayList();
byte[] output = buffer.stream(...)
ArrayList<byte[]> buffer = new ArrayList();
byte[] output = buffer.stream(...)
试试这个。
List<byte[]> list = Arrays.asList("abc".getBytes(), "def".getBytes());
byte[] result = list.stream()
.collect(
() -> new ByteArrayOutputStream(),
(b, e) -> b.write(e, 0, e.length),
(a, b) -> {}).toByteArray();
System.out.println(new String(result));
// -> abcdef
b.writeTo(a);
就足够了。 - user167019b.write(e, 0, e.length)
时,你不需要处理IOException
异常,因此整个lambda表达式简化为(b, e) -> b.write(e, 0, e.length)
。此外,我赞同@user167019的观点,不要编写有问题的combiner函数,尤其是当有简单直接的实现方法时,例如b.writeTo(a)
。 - Holger以下是使用Guava库的可能解决方案:
List<byte[]> list = Arrays.asList("abc".getBytes(), "def".getBytes());
byte[] res = Bytes.toArray(list.stream()
.map(byteArray -> Bytes.asList(byteArray))
.flatMap(listArray -> listArray.stream())
.collect(Collectors.toList()));
flatMap
应该是您正在寻找的,理想情况下应该如下所示:byte[] output = buffer.stream().flatMap(x -> Arrays.stream(x)).toArray(n -> new byte[n])
但是它无法编译。
使用一些辅助方法:
private Byte[] box(final byte[] arr) {
final Byte[] res = new Byte[arr.length];
for (int i = 0; i < arr.length; i++) {
res[i] = arr[i];
}
return res;
}
private byte[] unBox(final Byte[] arr) {
final byte[] res = new byte[arr.length];
for (int i = 0; i < arr.length; i++) {
res[i] = arr[i];
}
return res;
}
byte[] output = unBox(buffer.stream().flatMap(x -> Arrays.stream(box(x))).toArray(n -> new Byte[n]));
toArray
不能用于输出原始数组(泛型不允许它)。这仍然无法编译。 - 4castlebyte[]
转换为List<Byte>
和反向转换的Bytes
:
public static List<Byte> asList(byte... backingArray)
并且
public static byte[] toArray(Collection<? extends Number> collection)
public static void main(String[] args) {
List<byte[]> list = Arrays.asList("abc".getBytes(), "def".getBytes());
byte[] flattened= flatByteList(list);
System.out.println(new String(flattened)); // abcdef
}
private static byte[] flatByteList(List<byte[]> list) {
int byteArrlength = list.get(0).length;
byte[] result = new byte[list.size() * byteArrlength]; // since all the arrays have the same size
for (int i = 0; i < list.size(); i++) {
byte[] arr = list.get(i);
for (int j = 0; j < byteArrlength; j++) {
result[i * byteArrlength + j] = arr[j];
}
}
return result;
}