【JAVA】UUID與變體使用(long型表示)


1.UUID種類:

UUID1:基於時間

UUID2:DCE安全

UUID3:基於名字(MD5)

UUID4:隨機數

UUID5:基於名字(SHA1)


 

2.UUID使用

UUID.randomUUID().toString()是javaJDK提供的一個自動生成主鍵的方法。


 

3.UUID工具類推薦:

  cn.hutool:一個很好的集成工具類。

  導入Maven的pom文件:

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.3</version>
        </dependency>

 


 

4.變體使用:

  根據字符串獲取UUID:

     String str = "123456789abcdef";
byte[] byteArr= str.getBytes(StandardCharsets.UTF_8); int hashCodeV = UUID.nameUUIDFromBytes(byteArr).toString().hashCode(); if (hashCodeV < 0) {// 有可能是負數 hashCodeV = -hashCodeV; } String string = String.format("%015d", hashCodeV); System.out.println(Long.parseLong(string));

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM