Java 如何将UUID的ArrayList转换为ByteArray? 对象转换{ fun UUID.toByteArray():ByteArray{ val byteBuffer=byteBuffer.wrap(ByteArray(16)) byteBuffer.putLong(这个是最重要的位) byteBuffer.putLong(this.leastSignificantBits) 返回byteBuffer.array() } fun ByteArray.touid():UUID{ val byteBuffer=byteBuffer.wrap(此) val mostSignificantBits=byteBuffer.long val leastSignificantBits=byteBuffer.long 返回UUID(最大重要位,最小重要位) } }
我有上面的代码将UUID转换为ByteArray并将其转换回,但我还需要能够将ArrayList转换为ByteArray并将其转换回。我该怎么做呢?如果您想知道为什么我需要这样做,那是因为我需要存储一个HashMap,您可以使用Java 如何将UUID的ArrayList转换为ByteArray? 对象转换{ fun UUID.toByteArray():ByteArray{ val byteBuffer=byteBuffer.wrap(ByteArray(16)) byteBuffer.putLong(这个是最重要的位) byteBuffer.putLong(this.leastSignificantBits) 返回byteBuffer.array() } fun ByteArray.touid():UUID{ val byteBuffer=byteBuffer.wrap(此) val mostSignificantBits=byteBuffer.long val leastSignificantBits=byteBuffer.long 返回UUID(最大重要位,最小重要位) } },java,arrays,kotlin,Java,Arrays,Kotlin,我有上面的代码将UUID转换为ByteArray并将其转换回,但我还需要能够将ArrayList转换为ByteArray并将其转换回。我该怎么做呢?如果您想知道为什么我需要这样做,那是因为我需要存储一个HashMap,您可以使用+运算符连接独立的UUID字节数组,如下所示: val allUUIDs : ByteArray = listOfUUID.fold(ByteArray(0)) { buffer, uuid -> buffer + uuid.toByteArray() } 然而,
+
运算符连接独立的UUID字节数组,如下所示:
val allUUIDs : ByteArray = listOfUUID.fold(ByteArray(0)) { buffer, uuid -> buffer + uuid.toByteArray() }
然而,如果您需要转换长链,性能可能会显著下降。相反,您可以使用专用方法读取/写入字节缓冲区:
import java.nio.ByteBuffer
import java.util.*
import kotlin.RuntimeException
fun ByteBuffer.readUUIDs(nbUUIDs : Int = remaining()/16) : Sequence<UUID> {
if (nbUUIDs <= 0) return emptySequence()
val nbBytes = nbUUIDs * 16
// Defensive copy -> resulting sequence becomes independant from receiver buffer
val defCpy = ByteArray(nbBytes)
// slice is required to left source buffer untouched
slice().get(defCpy)
val view = ByteBuffer.wrap(defCpy)
return (1..nbUUIDs).asSequence()
.map { UUID(view.long, view.long) }
}
fun List<UUID>?.write() : ByteBuffer? {
if (this == null || isEmpty()) return null;
val buffer = ByteBuffer.allocate(Math.multiplyExact(size, 16))
forEach { uuid ->
buffer.putLong(uuid.mostSignificantBits)
buffer.putLong(uuid.leastSignificantBits)
}
buffer.rewind()
return buffer
}
fun main() {
val uuids = (0..3).asSequence().map { UUID.randomUUID() }.toList()
val writtenUUIDs = uuids.write()
val uuidSequence = writtenUUIDs ?.readUUIDs() ?: throw RuntimeException("Bug")
// Note that now, we can do whatever we want with the buffer. The sequence is not impacted
writtenUUIDs.getLong()
writtenUUIDs.putLong(-1)
val readBack = uuidSequence?.toList()
assert(uuids == readBack) { throw RuntimeException("Bug") }
println(uuids)
println(readBack)
}
import java.nio.ByteBuffer
导入java.util*
导入kotlin.RuntimeException
readUUIDs(nbUUIDs:Int=remaining()/16):序列{
if(nbUUIDs)结果序列从接收器缓冲区独立
val defCpy=ByteArray(nbBytes)
//需要切片才能使源缓冲区保持不变
slice().get(defCpy)
val view=ByteBuffer.wrap(defCpy)
return(1..nbUUIDs.asSequence()
.map{UUID(view.long,view.long)}
}
有趣的列表?.write():ByteBuffer{
if(this==null | | isEmpty())返回null;
val buffer=ByteBuffer.allocate(Math.multiplyExact(大小,16))
forEach{uuid->
buffer.putLong(uuid.mostSignificantBits)
buffer.putLong(uuid.leastSignificantBits)
}
buffer.rewind()
返回缓冲区
}
主要内容(){
val uuids=(0..3).asSequence().map{UUID.randomUUID()}.toList()
val writenuuids=uuids.write()
val uuidSequence=writenuuids?.readUUIDs()?:抛出运行时异常(“Bug”)
//请注意,现在,我们可以用缓冲区做任何我们想做的事情
writenuuids.getLong()
writenuuids.putLong(-1)
val readBack=uuidSequence?.toList()
断言(uuids==readBack){throw RuntimeException(“Bug”)}
println(uuids)
println(回读)
}
为什么不listofuid.map{it.toByteArray()}.toList()
并像listofbytearray.map{it.touid()}.toList()
一样将其转换回来呢?非常感谢这真的帮了我大忙!