[英]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()
。 您需要為您的集合找到一些哈希函數。 我認為這個答案可能會對您有所幫助-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.