簡體   English   中英

從Java中的int列表生成唯一鍵

[英]Generate unique key from List of int in Java

我有很多(不可變的) Integer數值列表。

其中一些包含完全相同的值。 因此,為了節省內存,我想找到那些。 因此,我使用的是HashMap<String, List<Integer>>

一種可行的方法是簡單地將值連接到一個大String並將其用作HashMap的鍵。

這種方法工作可靠,但速度很慢,並且占用大量內存。

我的Integer數值介於1到100,000,000之間。 列表包含1到1000個Integer值。

最多可能有100,000,000個列表。

我需要確保沒有碰撞。

  • 將列表轉換成BigInteger
  • 適當的Arrays.hashCode()List.hashCode()
  • CRC32
  • SHA256、512,...

您需要為您的集合找到一些哈希函數。 我認為這個答案可能會對您有所幫助-https: //cstheory.stackexchange.com/questions/3390/is-there-a-hash-function-for-a-collection-ie-multi-set-of-integers-that-擁有

嘗試使用Set。 這是使用Java8的示例。 它接收兩個列表,並創建一個單獨的Set,其中僅包含來自list1和list2的重復條目:

    Integer[] a = {1,2,2,3,1};
    List<Integer> list1 = Arrays.asList(a);
    List<Integer> list2 = Arrays.asList(a);

    Set<Integer> duplicates = list1.stream().filter(entry -> list2.contains(entry)).collect(Collectors.toSet());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM