我有许多(不可变的) Integer值列表。
其中一些包含完全相同的值。所以为了节省内存,我想找出它们。出于这个原因,我使用HashMap<String, List<Integer>>
一种可行的方法是简单地将这些值连接到一个较大的String,并将其用作HashMap中的键。
这种方法工作可靠,但它非常慢,并且消耗大量内存。
我的Integer值在1到100,000,000之间。这些列表包含1到1000个Integer值。
最多可以有100,000,000个列表。
我需要确保没有冲突。
发布于 2015-07-15 18:12:29
根据需要将列表转换为BigInteger
Arrays.hashCode()或List.hashCode()。
发布于 2015-07-15 18:04:24
你需要为你的集合找到一些散列函数。我认为这个答案可能会对你有所帮助-- https://cstheory.stackexchange.com/questions/3390/is-there-a-hash-function-for-a-collection-i-e-multi-set-of-integers-that-has
发布于 2015-07-15 18:21:32
尝试使用集合。下面是一个使用Java8的示例。它接受两个列表,并创建一个单独的集合,其中只包含来自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());https://stackoverflow.com/questions/31427139
复制相似问题