Generate unique key from List of int in Java

I have many (immutable) lists of Integer values.

A few of them are containing the exact same values. So in order to save memory I'd like to find those. For that reason I'm using a HashMap<String, List<Integer>>

A working approach is to simply concatenate the values to one large String and use it as a key in the HashMap.

This approach works reliably, but it is very slow and consumes a lot of memory.

My Integer values are in between 1 and 100,000,000. The lists are containing in between 1 and 1000 Integer values.

There could be up to 100,000,000 lists.

I need to be sure that there are no collisions.

Answers


  • transform the list into a BigInteger
  • Arrays.hashCode() or List.hashCode(), as appropriate.
  • A CRC32
  • An SHA256, 512, ...

You need to find some hash function for your collection. I think this answer may help you - https://cstheory.stackexchange.com/questions/3390/is-there-a-hash-function-for-a-collection-i-e-multi-set-of-integers-that-has


Try using a Set. Here is an example using Java8. It takes in two lists and creates a separate Set which contains just the duplicate entries from list1 and 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());

Need Your Help

How can I show an error message in an ASP.NET project using a popup window?

c# asp.net sql-server

I'm using this code but a compilation error occured. It says that the name server does not exist in current context. If I use Microsoft.SqlServer.Server then .HtmlEncode does not exist in the con...

How to read HTML5 textarea's text with vb.net code at the back

javascript asp.net vb.net html5

I am trying to read the HTML5 Textarea's (not ASP.NET textbox control) text and pass it to my aspx.vb code at the back of the web page. How can I do it?