Is there a data structure that only stores hash codes and not the actual objects?2019 Community Moderator ElectionMemory size of Java 32-bit system BitSetsFastest way to determine if an integer's square root is an integerJava Class that implements Map and keeps insertion order?Why can't I retrieve an item from a HashSet without enumeration?Java tree data-structure?Gson: How to exclude specific fields from Serialization without annotationsHashcode and Equals for HashsetDirectly accessible data structure JavaJava HashSet vs Array PerformanceWhy hash set allows adding duplicate object?Why is (a*b != 0) faster than (a != 0 && b != 0) in Java?

How to simplify this time periods definition interface?

Look at your watch and tell me what time is it. vs Look at your watch and tell me what time it is

How to write cleanly even if my character uses expletive language?

What options are left, if Britain cannot decide?

My adviser wants to be the first author

Can I use USB data pins as power source

Are all passive ability checks floors for active ability checks?

Life insurance that covers only simultaneous/dual deaths

A Cautionary Suggestion

A link redirect to http instead of https: how critical is it?

Why do Australian milk farmers need to protest supermarkets' milk price?

How can you use ICE tables to solve multiple coupled equilibria?

Does Mathematica reuse previous computations?

Can a druid choose the size of its wild shape beast?

In a future war, an old lady is trying to raise a boy but one of the weapons has made everyone deaf

AG Cluster db upgrade by vendor

If I can solve Sudoku can I solve Travelling Salesman Problem(TSP)? If yes, how?

What do Xenomorphs eat in the Alien series?

Python if-else code style for reduced code for rounding floats

(Calculus) Derivative Thinking Question

How to deal with taxi scam when on vacation?

Is a party consisting of only a bard, a cleric, and a warlock functional long-term?

Should we release the security issues we found in our product as CVE or we can just update those on weekly release notes?

If curse and magic is two sides of the same coin, why the former is forbidden?



Is there a data structure that only stores hash codes and not the actual objects?



2019 Community Moderator ElectionMemory size of Java 32-bit system BitSetsFastest way to determine if an integer's square root is an integerJava Class that implements Map and keeps insertion order?Why can't I retrieve an item from a HashSet without enumeration?Java tree data-structure?Gson: How to exclude specific fields from Serialization without annotationsHashcode and Equals for HashsetDirectly accessible data structure JavaJava HashSet vs Array PerformanceWhy hash set allows adding duplicate object?Why is (a*b != 0) faster than (a != 0 && b != 0) in Java?










7















My use-case is that I'm looking for a data structure in Java that will let me see if an object with the same hash code is inside (by calling contains()), but I will never need to iterate through the elements or retrieve the actual objects. A HashSet is close, but from my understanding, it still contains references to the actual objects, and that would be a waste of memory since I won't ever need the contents of the actual objects. The best option I can think of is a HashSet of type Integer storing only the hash codes, but I'm wondering if there is a built-in data structure that would accomplish the same thing (and only accept one type as opposed to HashSet of type Integer which will accept the hash code of any object).










share|improve this question



















  • 4





    Is your hash function perfect? Or can you have multiple objects with the same hash value?

    – arshajii
    2 hours ago






  • 5





    what about hashing collisions?

    – Nathan Hughes台湾不在中国
    2 hours ago






  • 7





    The HashSet will contain a reference to your object, not a copy, so don't worry about space. A HashSet<Integer> would probably use up more space because it has references to integers.

    – Sweeper
    2 hours ago












  • I agree with @Sweeper, unless you have a real need for super-duper optimization. Also, your second idea with storing hashcodes as integer wouln't be more efficient as it would store the hash+the hash of the hash.

    – Joel
    2 hours ago











  • @Sweeper The HashSet uses internally a HashMap. The memory space is the same.

    – Octavian R.
    2 hours ago
















7















My use-case is that I'm looking for a data structure in Java that will let me see if an object with the same hash code is inside (by calling contains()), but I will never need to iterate through the elements or retrieve the actual objects. A HashSet is close, but from my understanding, it still contains references to the actual objects, and that would be a waste of memory since I won't ever need the contents of the actual objects. The best option I can think of is a HashSet of type Integer storing only the hash codes, but I'm wondering if there is a built-in data structure that would accomplish the same thing (and only accept one type as opposed to HashSet of type Integer which will accept the hash code of any object).










share|improve this question



















  • 4





    Is your hash function perfect? Or can you have multiple objects with the same hash value?

    – arshajii
    2 hours ago






  • 5





    what about hashing collisions?

    – Nathan Hughes台湾不在中国
    2 hours ago






  • 7





    The HashSet will contain a reference to your object, not a copy, so don't worry about space. A HashSet<Integer> would probably use up more space because it has references to integers.

    – Sweeper
    2 hours ago












  • I agree with @Sweeper, unless you have a real need for super-duper optimization. Also, your second idea with storing hashcodes as integer wouln't be more efficient as it would store the hash+the hash of the hash.

    – Joel
    2 hours ago











  • @Sweeper The HashSet uses internally a HashMap. The memory space is the same.

    – Octavian R.
    2 hours ago














7












7








7


1






My use-case is that I'm looking for a data structure in Java that will let me see if an object with the same hash code is inside (by calling contains()), but I will never need to iterate through the elements or retrieve the actual objects. A HashSet is close, but from my understanding, it still contains references to the actual objects, and that would be a waste of memory since I won't ever need the contents of the actual objects. The best option I can think of is a HashSet of type Integer storing only the hash codes, but I'm wondering if there is a built-in data structure that would accomplish the same thing (and only accept one type as opposed to HashSet of type Integer which will accept the hash code of any object).










share|improve this question
















My use-case is that I'm looking for a data structure in Java that will let me see if an object with the same hash code is inside (by calling contains()), but I will never need to iterate through the elements or retrieve the actual objects. A HashSet is close, but from my understanding, it still contains references to the actual objects, and that would be a waste of memory since I won't ever need the contents of the actual objects. The best option I can think of is a HashSet of type Integer storing only the hash codes, but I'm wondering if there is a built-in data structure that would accomplish the same thing (and only accept one type as opposed to HashSet of type Integer which will accept the hash code of any object).







java hashset






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 hours ago







B Yellow

















asked 2 hours ago









B YellowB Yellow

413




413







  • 4





    Is your hash function perfect? Or can you have multiple objects with the same hash value?

    – arshajii
    2 hours ago






  • 5





    what about hashing collisions?

    – Nathan Hughes台湾不在中国
    2 hours ago






  • 7





    The HashSet will contain a reference to your object, not a copy, so don't worry about space. A HashSet<Integer> would probably use up more space because it has references to integers.

    – Sweeper
    2 hours ago












  • I agree with @Sweeper, unless you have a real need for super-duper optimization. Also, your second idea with storing hashcodes as integer wouln't be more efficient as it would store the hash+the hash of the hash.

    – Joel
    2 hours ago











  • @Sweeper The HashSet uses internally a HashMap. The memory space is the same.

    – Octavian R.
    2 hours ago













  • 4





    Is your hash function perfect? Or can you have multiple objects with the same hash value?

    – arshajii
    2 hours ago






  • 5





    what about hashing collisions?

    – Nathan Hughes台湾不在中国
    2 hours ago






  • 7





    The HashSet will contain a reference to your object, not a copy, so don't worry about space. A HashSet<Integer> would probably use up more space because it has references to integers.

    – Sweeper
    2 hours ago












  • I agree with @Sweeper, unless you have a real need for super-duper optimization. Also, your second idea with storing hashcodes as integer wouln't be more efficient as it would store the hash+the hash of the hash.

    – Joel
    2 hours ago











  • @Sweeper The HashSet uses internally a HashMap. The memory space is the same.

    – Octavian R.
    2 hours ago








4




4





Is your hash function perfect? Or can you have multiple objects with the same hash value?

– arshajii
2 hours ago





Is your hash function perfect? Or can you have multiple objects with the same hash value?

– arshajii
2 hours ago




5




5





what about hashing collisions?

– Nathan Hughes台湾不在中国
2 hours ago





what about hashing collisions?

– Nathan Hughes台湾不在中国
2 hours ago




7




7





The HashSet will contain a reference to your object, not a copy, so don't worry about space. A HashSet<Integer> would probably use up more space because it has references to integers.

– Sweeper
2 hours ago






The HashSet will contain a reference to your object, not a copy, so don't worry about space. A HashSet<Integer> would probably use up more space because it has references to integers.

– Sweeper
2 hours ago














I agree with @Sweeper, unless you have a real need for super-duper optimization. Also, your second idea with storing hashcodes as integer wouln't be more efficient as it would store the hash+the hash of the hash.

– Joel
2 hours ago





I agree with @Sweeper, unless you have a real need for super-duper optimization. Also, your second idea with storing hashcodes as integer wouln't be more efficient as it would store the hash+the hash of the hash.

– Joel
2 hours ago













@Sweeper The HashSet uses internally a HashMap. The memory space is the same.

– Octavian R.
2 hours ago






@Sweeper The HashSet uses internally a HashMap. The memory space is the same.

– Octavian R.
2 hours ago













5 Answers
5






active

oldest

votes


















6














A Bloom filter can tell whether an object might be a member, or is definitely not a member. You can control the likelihood of false positives. A single bit is stored per hash value.



The Guava library provides an implementation in Java.






share|improve this answer

























  • Nice. This seems like the solution for very low storage overhead. But you have to worry about the false negative case. If you can statistically eliminate that, this is great!

    – Steve
    2 hours ago







  • 1





    False positives, but you can control their probability. Another disadvantage is that you can't remove elements.

    – Andy Thomas
    2 hours ago











  • The question was for a data structure that checks using only the predefined hashCode(), which can potentially have 2^31 values (counting only positives). A Bloom filter that uses one hash function with 2 ^ 31 possible values would be extraordinarily large, seeing as it is basically just a BitSet. I don't see how that counts as "very low storage overhead".

    – Leo Aso
    2 hours ago



















0














There is no data structure like that, but you can create a wrapper around HashSet to suit your needs. Something like this:



public class MyHashSet

private HashSet<Integer> set;

public MyHashSet()
set = new HashSet<>();


public void add(int hash)
set.add(hash);


public boolean contains(int hash)
return set.contains(hash);







share|improve this answer


















  • 1





    I think the OP's question wasn't about API, but about memory usage. This doesn't help with that since the result still acts like a HashSet.

    – Steve
    2 hours ago











  • @Steve His main concern was a reference to the objects. Here it is storing only referencing to Integer objects.

    – Pritam Banerjee
    2 hours ago


















0














There is no such built-in data structure, because such a data structure is rarely needed. It's easy to build one, though.



public class HashCodeSet<T> 

private final HashSet<Integer> hashCodes;

public MyHashSet()
hashCodes = new HashSet<>();


public MyHashSet(int initialCapacity)
hashCodes = new HashSet<>(initialCapacity);


public HashCodeSet(HashCodeSet toCopy)
hashCodes = new HashSet<>(toCopy.hashCodes);


public void add(T element)
hashCodes.add(element.hashCode());


public boolean containsHashCodeOf(T element)
return hashCodes.contains(element.hashCode());


@Override
public boolean equals(o: Object)
return o == this

@Override
public int hashCode()
return hashCodes.hashCode(); // hash-ception


@Override
public String toString()
return hashCodes.toString();







share|improve this answer


















  • 1





    I think the OP's question wasn't about API, but about memory usage. This doesn't help with that since the result still acts like a HashSet.

    – Steve
    2 hours ago











  • I realize that I was unfair completely on this. I've removed my objections. Feel free to delete your comments on my comments. I still think there's something more to the problem, but I was off base. Sorry

    – Steve
    1 hour ago


















0














You could use a primitive collection implementation like IntSet to store values of hash codes. Obviously as others have mentioned this assumes collisions aren't a problem.






share|improve this answer






























    0














    If you want to track if a hash code is already present and to do it memory efficient a BitSet may suite your requirements.



    Look at the following example:



     public static void main(String[] args) 
    BitSet hashCodes = new BitSet();
    hashCodes.set("1".hashCode());

    System.out.println(hashCodes.get("1".hashCode())); // true
    System.out.println(hashCodes.get("2".hashCode())); // false



    The BitSet "implements a vector of bits that grows as needed.". It's a JDK "built-in data structure" which doesn't contain "references to the actual objects". It stores only if "the same hash code is inside".



    EDIT:

    As @Steve mentioned in his comment the implementation of the BitSet isn't the most memory efficient one. But there are more memory efficient implementations of a bit set - though not built-in.






    share|improve this answer

























    • I don't know how a BitSet stores individual bits. Since obviously this usage will spread bits across a very large input domain, are you sure those are stored efficiently? Just asking. The naive assumption is that the structure would be an array of bytes, where the array was just expanded to include any bit position and all positions before that, which would be monstrously inefficient. But I don't know how it actually represents bits spread way apart.

      – Steve
      2 hours ago











    • It appears that your solution won't work. See github.com/brettwooldridge/SparseBitSet

      – Steve
      1 hour ago











    • @Steve You're right. Found additionally this post. But the idea of an bit set is basically not bad. It's rather the implementation of the JDK BitSet.

      – LuCio
      1 hour ago











    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55190768%2fis-there-a-data-structure-that-only-stores-hash-codes-and-not-the-actual-objects%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    5 Answers
    5






    active

    oldest

    votes








    5 Answers
    5






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    A Bloom filter can tell whether an object might be a member, or is definitely not a member. You can control the likelihood of false positives. A single bit is stored per hash value.



    The Guava library provides an implementation in Java.






    share|improve this answer

























    • Nice. This seems like the solution for very low storage overhead. But you have to worry about the false negative case. If you can statistically eliminate that, this is great!

      – Steve
      2 hours ago







    • 1





      False positives, but you can control their probability. Another disadvantage is that you can't remove elements.

      – Andy Thomas
      2 hours ago











    • The question was for a data structure that checks using only the predefined hashCode(), which can potentially have 2^31 values (counting only positives). A Bloom filter that uses one hash function with 2 ^ 31 possible values would be extraordinarily large, seeing as it is basically just a BitSet. I don't see how that counts as "very low storage overhead".

      – Leo Aso
      2 hours ago
















    6














    A Bloom filter can tell whether an object might be a member, or is definitely not a member. You can control the likelihood of false positives. A single bit is stored per hash value.



    The Guava library provides an implementation in Java.






    share|improve this answer

























    • Nice. This seems like the solution for very low storage overhead. But you have to worry about the false negative case. If you can statistically eliminate that, this is great!

      – Steve
      2 hours ago







    • 1





      False positives, but you can control their probability. Another disadvantage is that you can't remove elements.

      – Andy Thomas
      2 hours ago











    • The question was for a data structure that checks using only the predefined hashCode(), which can potentially have 2^31 values (counting only positives). A Bloom filter that uses one hash function with 2 ^ 31 possible values would be extraordinarily large, seeing as it is basically just a BitSet. I don't see how that counts as "very low storage overhead".

      – Leo Aso
      2 hours ago














    6












    6








    6







    A Bloom filter can tell whether an object might be a member, or is definitely not a member. You can control the likelihood of false positives. A single bit is stored per hash value.



    The Guava library provides an implementation in Java.






    share|improve this answer















    A Bloom filter can tell whether an object might be a member, or is definitely not a member. You can control the likelihood of false positives. A single bit is stored per hash value.



    The Guava library provides an implementation in Java.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 2 hours ago

























    answered 2 hours ago









    Andy ThomasAndy Thomas

    68k980133




    68k980133












    • Nice. This seems like the solution for very low storage overhead. But you have to worry about the false negative case. If you can statistically eliminate that, this is great!

      – Steve
      2 hours ago







    • 1





      False positives, but you can control their probability. Another disadvantage is that you can't remove elements.

      – Andy Thomas
      2 hours ago











    • The question was for a data structure that checks using only the predefined hashCode(), which can potentially have 2^31 values (counting only positives). A Bloom filter that uses one hash function with 2 ^ 31 possible values would be extraordinarily large, seeing as it is basically just a BitSet. I don't see how that counts as "very low storage overhead".

      – Leo Aso
      2 hours ago


















    • Nice. This seems like the solution for very low storage overhead. But you have to worry about the false negative case. If you can statistically eliminate that, this is great!

      – Steve
      2 hours ago







    • 1





      False positives, but you can control their probability. Another disadvantage is that you can't remove elements.

      – Andy Thomas
      2 hours ago











    • The question was for a data structure that checks using only the predefined hashCode(), which can potentially have 2^31 values (counting only positives). A Bloom filter that uses one hash function with 2 ^ 31 possible values would be extraordinarily large, seeing as it is basically just a BitSet. I don't see how that counts as "very low storage overhead".

      – Leo Aso
      2 hours ago

















    Nice. This seems like the solution for very low storage overhead. But you have to worry about the false negative case. If you can statistically eliminate that, this is great!

    – Steve
    2 hours ago






    Nice. This seems like the solution for very low storage overhead. But you have to worry about the false negative case. If you can statistically eliminate that, this is great!

    – Steve
    2 hours ago





    1




    1





    False positives, but you can control their probability. Another disadvantage is that you can't remove elements.

    – Andy Thomas
    2 hours ago





    False positives, but you can control their probability. Another disadvantage is that you can't remove elements.

    – Andy Thomas
    2 hours ago













    The question was for a data structure that checks using only the predefined hashCode(), which can potentially have 2^31 values (counting only positives). A Bloom filter that uses one hash function with 2 ^ 31 possible values would be extraordinarily large, seeing as it is basically just a BitSet. I don't see how that counts as "very low storage overhead".

    – Leo Aso
    2 hours ago






    The question was for a data structure that checks using only the predefined hashCode(), which can potentially have 2^31 values (counting only positives). A Bloom filter that uses one hash function with 2 ^ 31 possible values would be extraordinarily large, seeing as it is basically just a BitSet. I don't see how that counts as "very low storage overhead".

    – Leo Aso
    2 hours ago














    0














    There is no data structure like that, but you can create a wrapper around HashSet to suit your needs. Something like this:



    public class MyHashSet

    private HashSet<Integer> set;

    public MyHashSet()
    set = new HashSet<>();


    public void add(int hash)
    set.add(hash);


    public boolean contains(int hash)
    return set.contains(hash);







    share|improve this answer


















    • 1





      I think the OP's question wasn't about API, but about memory usage. This doesn't help with that since the result still acts like a HashSet.

      – Steve
      2 hours ago











    • @Steve His main concern was a reference to the objects. Here it is storing only referencing to Integer objects.

      – Pritam Banerjee
      2 hours ago















    0














    There is no data structure like that, but you can create a wrapper around HashSet to suit your needs. Something like this:



    public class MyHashSet

    private HashSet<Integer> set;

    public MyHashSet()
    set = new HashSet<>();


    public void add(int hash)
    set.add(hash);


    public boolean contains(int hash)
    return set.contains(hash);







    share|improve this answer


















    • 1





      I think the OP's question wasn't about API, but about memory usage. This doesn't help with that since the result still acts like a HashSet.

      – Steve
      2 hours ago











    • @Steve His main concern was a reference to the objects. Here it is storing only referencing to Integer objects.

      – Pritam Banerjee
      2 hours ago













    0












    0








    0







    There is no data structure like that, but you can create a wrapper around HashSet to suit your needs. Something like this:



    public class MyHashSet

    private HashSet<Integer> set;

    public MyHashSet()
    set = new HashSet<>();


    public void add(int hash)
    set.add(hash);


    public boolean contains(int hash)
    return set.contains(hash);







    share|improve this answer













    There is no data structure like that, but you can create a wrapper around HashSet to suit your needs. Something like this:



    public class MyHashSet

    private HashSet<Integer> set;

    public MyHashSet()
    set = new HashSet<>();


    public void add(int hash)
    set.add(hash);


    public boolean contains(int hash)
    return set.contains(hash);








    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 2 hours ago









    Pritam BanerjeePritam Banerjee

    11k64567




    11k64567







    • 1





      I think the OP's question wasn't about API, but about memory usage. This doesn't help with that since the result still acts like a HashSet.

      – Steve
      2 hours ago











    • @Steve His main concern was a reference to the objects. Here it is storing only referencing to Integer objects.

      – Pritam Banerjee
      2 hours ago












    • 1





      I think the OP's question wasn't about API, but about memory usage. This doesn't help with that since the result still acts like a HashSet.

      – Steve
      2 hours ago











    • @Steve His main concern was a reference to the objects. Here it is storing only referencing to Integer objects.

      – Pritam Banerjee
      2 hours ago







    1




    1





    I think the OP's question wasn't about API, but about memory usage. This doesn't help with that since the result still acts like a HashSet.

    – Steve
    2 hours ago





    I think the OP's question wasn't about API, but about memory usage. This doesn't help with that since the result still acts like a HashSet.

    – Steve
    2 hours ago













    @Steve His main concern was a reference to the objects. Here it is storing only referencing to Integer objects.

    – Pritam Banerjee
    2 hours ago





    @Steve His main concern was a reference to the objects. Here it is storing only referencing to Integer objects.

    – Pritam Banerjee
    2 hours ago











    0














    There is no such built-in data structure, because such a data structure is rarely needed. It's easy to build one, though.



    public class HashCodeSet<T> 

    private final HashSet<Integer> hashCodes;

    public MyHashSet()
    hashCodes = new HashSet<>();


    public MyHashSet(int initialCapacity)
    hashCodes = new HashSet<>(initialCapacity);


    public HashCodeSet(HashCodeSet toCopy)
    hashCodes = new HashSet<>(toCopy.hashCodes);


    public void add(T element)
    hashCodes.add(element.hashCode());


    public boolean containsHashCodeOf(T element)
    return hashCodes.contains(element.hashCode());


    @Override
    public boolean equals(o: Object)
    return o == this

    @Override
    public int hashCode()
    return hashCodes.hashCode(); // hash-ception


    @Override
    public String toString()
    return hashCodes.toString();







    share|improve this answer


















    • 1





      I think the OP's question wasn't about API, but about memory usage. This doesn't help with that since the result still acts like a HashSet.

      – Steve
      2 hours ago











    • I realize that I was unfair completely on this. I've removed my objections. Feel free to delete your comments on my comments. I still think there's something more to the problem, but I was off base. Sorry

      – Steve
      1 hour ago















    0














    There is no such built-in data structure, because such a data structure is rarely needed. It's easy to build one, though.



    public class HashCodeSet<T> 

    private final HashSet<Integer> hashCodes;

    public MyHashSet()
    hashCodes = new HashSet<>();


    public MyHashSet(int initialCapacity)
    hashCodes = new HashSet<>(initialCapacity);


    public HashCodeSet(HashCodeSet toCopy)
    hashCodes = new HashSet<>(toCopy.hashCodes);


    public void add(T element)
    hashCodes.add(element.hashCode());


    public boolean containsHashCodeOf(T element)
    return hashCodes.contains(element.hashCode());


    @Override
    public boolean equals(o: Object)
    return o == this

    @Override
    public int hashCode()
    return hashCodes.hashCode(); // hash-ception


    @Override
    public String toString()
    return hashCodes.toString();







    share|improve this answer


















    • 1





      I think the OP's question wasn't about API, but about memory usage. This doesn't help with that since the result still acts like a HashSet.

      – Steve
      2 hours ago











    • I realize that I was unfair completely on this. I've removed my objections. Feel free to delete your comments on my comments. I still think there's something more to the problem, but I was off base. Sorry

      – Steve
      1 hour ago













    0












    0








    0







    There is no such built-in data structure, because such a data structure is rarely needed. It's easy to build one, though.



    public class HashCodeSet<T> 

    private final HashSet<Integer> hashCodes;

    public MyHashSet()
    hashCodes = new HashSet<>();


    public MyHashSet(int initialCapacity)
    hashCodes = new HashSet<>(initialCapacity);


    public HashCodeSet(HashCodeSet toCopy)
    hashCodes = new HashSet<>(toCopy.hashCodes);


    public void add(T element)
    hashCodes.add(element.hashCode());


    public boolean containsHashCodeOf(T element)
    return hashCodes.contains(element.hashCode());


    @Override
    public boolean equals(o: Object)
    return o == this

    @Override
    public int hashCode()
    return hashCodes.hashCode(); // hash-ception


    @Override
    public String toString()
    return hashCodes.toString();







    share|improve this answer













    There is no such built-in data structure, because such a data structure is rarely needed. It's easy to build one, though.



    public class HashCodeSet<T> 

    private final HashSet<Integer> hashCodes;

    public MyHashSet()
    hashCodes = new HashSet<>();


    public MyHashSet(int initialCapacity)
    hashCodes = new HashSet<>(initialCapacity);


    public HashCodeSet(HashCodeSet toCopy)
    hashCodes = new HashSet<>(toCopy.hashCodes);


    public void add(T element)
    hashCodes.add(element.hashCode());


    public boolean containsHashCodeOf(T element)
    return hashCodes.contains(element.hashCode());


    @Override
    public boolean equals(o: Object)
    return o == this

    @Override
    public int hashCode()
    return hashCodes.hashCode(); // hash-ception


    @Override
    public String toString()
    return hashCodes.toString();








    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 2 hours ago









    Leo AsoLeo Aso

    5,27411029




    5,27411029







    • 1





      I think the OP's question wasn't about API, but about memory usage. This doesn't help with that since the result still acts like a HashSet.

      – Steve
      2 hours ago











    • I realize that I was unfair completely on this. I've removed my objections. Feel free to delete your comments on my comments. I still think there's something more to the problem, but I was off base. Sorry

      – Steve
      1 hour ago












    • 1





      I think the OP's question wasn't about API, but about memory usage. This doesn't help with that since the result still acts like a HashSet.

      – Steve
      2 hours ago











    • I realize that I was unfair completely on this. I've removed my objections. Feel free to delete your comments on my comments. I still think there's something more to the problem, but I was off base. Sorry

      – Steve
      1 hour ago







    1




    1





    I think the OP's question wasn't about API, but about memory usage. This doesn't help with that since the result still acts like a HashSet.

    – Steve
    2 hours ago





    I think the OP's question wasn't about API, but about memory usage. This doesn't help with that since the result still acts like a HashSet.

    – Steve
    2 hours ago













    I realize that I was unfair completely on this. I've removed my objections. Feel free to delete your comments on my comments. I still think there's something more to the problem, but I was off base. Sorry

    – Steve
    1 hour ago





    I realize that I was unfair completely on this. I've removed my objections. Feel free to delete your comments on my comments. I still think there's something more to the problem, but I was off base. Sorry

    – Steve
    1 hour ago











    0














    You could use a primitive collection implementation like IntSet to store values of hash codes. Obviously as others have mentioned this assumes collisions aren't a problem.






    share|improve this answer



























      0














      You could use a primitive collection implementation like IntSet to store values of hash codes. Obviously as others have mentioned this assumes collisions aren't a problem.






      share|improve this answer

























        0












        0








        0







        You could use a primitive collection implementation like IntSet to store values of hash codes. Obviously as others have mentioned this assumes collisions aren't a problem.






        share|improve this answer













        You could use a primitive collection implementation like IntSet to store values of hash codes. Obviously as others have mentioned this assumes collisions aren't a problem.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 1 hour ago









        MarkMark

        24.2k44783




        24.2k44783





















            0














            If you want to track if a hash code is already present and to do it memory efficient a BitSet may suite your requirements.



            Look at the following example:



             public static void main(String[] args) 
            BitSet hashCodes = new BitSet();
            hashCodes.set("1".hashCode());

            System.out.println(hashCodes.get("1".hashCode())); // true
            System.out.println(hashCodes.get("2".hashCode())); // false



            The BitSet "implements a vector of bits that grows as needed.". It's a JDK "built-in data structure" which doesn't contain "references to the actual objects". It stores only if "the same hash code is inside".



            EDIT:

            As @Steve mentioned in his comment the implementation of the BitSet isn't the most memory efficient one. But there are more memory efficient implementations of a bit set - though not built-in.






            share|improve this answer

























            • I don't know how a BitSet stores individual bits. Since obviously this usage will spread bits across a very large input domain, are you sure those are stored efficiently? Just asking. The naive assumption is that the structure would be an array of bytes, where the array was just expanded to include any bit position and all positions before that, which would be monstrously inefficient. But I don't know how it actually represents bits spread way apart.

              – Steve
              2 hours ago











            • It appears that your solution won't work. See github.com/brettwooldridge/SparseBitSet

              – Steve
              1 hour ago











            • @Steve You're right. Found additionally this post. But the idea of an bit set is basically not bad. It's rather the implementation of the JDK BitSet.

              – LuCio
              1 hour ago
















            0














            If you want to track if a hash code is already present and to do it memory efficient a BitSet may suite your requirements.



            Look at the following example:



             public static void main(String[] args) 
            BitSet hashCodes = new BitSet();
            hashCodes.set("1".hashCode());

            System.out.println(hashCodes.get("1".hashCode())); // true
            System.out.println(hashCodes.get("2".hashCode())); // false



            The BitSet "implements a vector of bits that grows as needed.". It's a JDK "built-in data structure" which doesn't contain "references to the actual objects". It stores only if "the same hash code is inside".



            EDIT:

            As @Steve mentioned in his comment the implementation of the BitSet isn't the most memory efficient one. But there are more memory efficient implementations of a bit set - though not built-in.






            share|improve this answer

























            • I don't know how a BitSet stores individual bits. Since obviously this usage will spread bits across a very large input domain, are you sure those are stored efficiently? Just asking. The naive assumption is that the structure would be an array of bytes, where the array was just expanded to include any bit position and all positions before that, which would be monstrously inefficient. But I don't know how it actually represents bits spread way apart.

              – Steve
              2 hours ago











            • It appears that your solution won't work. See github.com/brettwooldridge/SparseBitSet

              – Steve
              1 hour ago











            • @Steve You're right. Found additionally this post. But the idea of an bit set is basically not bad. It's rather the implementation of the JDK BitSet.

              – LuCio
              1 hour ago














            0












            0








            0







            If you want to track if a hash code is already present and to do it memory efficient a BitSet may suite your requirements.



            Look at the following example:



             public static void main(String[] args) 
            BitSet hashCodes = new BitSet();
            hashCodes.set("1".hashCode());

            System.out.println(hashCodes.get("1".hashCode())); // true
            System.out.println(hashCodes.get("2".hashCode())); // false



            The BitSet "implements a vector of bits that grows as needed.". It's a JDK "built-in data structure" which doesn't contain "references to the actual objects". It stores only if "the same hash code is inside".



            EDIT:

            As @Steve mentioned in his comment the implementation of the BitSet isn't the most memory efficient one. But there are more memory efficient implementations of a bit set - though not built-in.






            share|improve this answer















            If you want to track if a hash code is already present and to do it memory efficient a BitSet may suite your requirements.



            Look at the following example:



             public static void main(String[] args) 
            BitSet hashCodes = new BitSet();
            hashCodes.set("1".hashCode());

            System.out.println(hashCodes.get("1".hashCode())); // true
            System.out.println(hashCodes.get("2".hashCode())); // false



            The BitSet "implements a vector of bits that grows as needed.". It's a JDK "built-in data structure" which doesn't contain "references to the actual objects". It stores only if "the same hash code is inside".



            EDIT:

            As @Steve mentioned in his comment the implementation of the BitSet isn't the most memory efficient one. But there are more memory efficient implementations of a bit set - though not built-in.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 1 hour ago

























            answered 2 hours ago









            LuCioLuCio

            2,8721924




            2,8721924












            • I don't know how a BitSet stores individual bits. Since obviously this usage will spread bits across a very large input domain, are you sure those are stored efficiently? Just asking. The naive assumption is that the structure would be an array of bytes, where the array was just expanded to include any bit position and all positions before that, which would be monstrously inefficient. But I don't know how it actually represents bits spread way apart.

              – Steve
              2 hours ago











            • It appears that your solution won't work. See github.com/brettwooldridge/SparseBitSet

              – Steve
              1 hour ago











            • @Steve You're right. Found additionally this post. But the idea of an bit set is basically not bad. It's rather the implementation of the JDK BitSet.

              – LuCio
              1 hour ago


















            • I don't know how a BitSet stores individual bits. Since obviously this usage will spread bits across a very large input domain, are you sure those are stored efficiently? Just asking. The naive assumption is that the structure would be an array of bytes, where the array was just expanded to include any bit position and all positions before that, which would be monstrously inefficient. But I don't know how it actually represents bits spread way apart.

              – Steve
              2 hours ago











            • It appears that your solution won't work. See github.com/brettwooldridge/SparseBitSet

              – Steve
              1 hour ago











            • @Steve You're right. Found additionally this post. But the idea of an bit set is basically not bad. It's rather the implementation of the JDK BitSet.

              – LuCio
              1 hour ago

















            I don't know how a BitSet stores individual bits. Since obviously this usage will spread bits across a very large input domain, are you sure those are stored efficiently? Just asking. The naive assumption is that the structure would be an array of bytes, where the array was just expanded to include any bit position and all positions before that, which would be monstrously inefficient. But I don't know how it actually represents bits spread way apart.

            – Steve
            2 hours ago





            I don't know how a BitSet stores individual bits. Since obviously this usage will spread bits across a very large input domain, are you sure those are stored efficiently? Just asking. The naive assumption is that the structure would be an array of bytes, where the array was just expanded to include any bit position and all positions before that, which would be monstrously inefficient. But I don't know how it actually represents bits spread way apart.

            – Steve
            2 hours ago













            It appears that your solution won't work. See github.com/brettwooldridge/SparseBitSet

            – Steve
            1 hour ago





            It appears that your solution won't work. See github.com/brettwooldridge/SparseBitSet

            – Steve
            1 hour ago













            @Steve You're right. Found additionally this post. But the idea of an bit set is basically not bad. It's rather the implementation of the JDK BitSet.

            – LuCio
            1 hour ago






            @Steve You're right. Found additionally this post. But the idea of an bit set is basically not bad. It's rather the implementation of the JDK BitSet.

            – LuCio
            1 hour ago


















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55190768%2fis-there-a-data-structure-that-only-stores-hash-codes-and-not-the-actual-objects%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Magento 2 duplicate PHPSESSID cookie when using session_start() in custom php scriptMagento 2: User cant logged in into to account page, no error showing!Magento duplicate on subdomainGrabbing storeview from cookie (after using language selector)How do I run php custom script on magento2Magento 2: Include PHP script in headerSession lock after using Cm_RedisSessionscript php to update stockMagento set cookie popupMagento 2 session id cookie - where to find it?How to import Configurable product from csv with custom attributes using php scriptMagento 2 run custom PHP script

            Can not update quote_id field of “quote_item” table magento 2Magento 2.1 - We can't remove the item. (Shopping Cart doesnt allow us to remove items before becomes empty)Add value for custom quote item attribute using REST apiREST API endpoint v1/carts/cartId/items always returns error messageCorrect way to save entries to databaseHow to remove all associated quote objects of a customer completelyMagento 2 - Save value from custom input field to quote_itemGet quote_item data using quote id and product id filter in Magento 2How to set additional data to quote_item table from controller in Magento 2?What is the purpose of additional_data column in quote_item table in magento2Set Custom Price to Quote item magento2 from controller

            How to solve knockout JS error in Magento 2 Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?(Magento2) knockout.js:3012 Uncaught ReferenceError: Unable to process bindingUnable to process binding Knockout.js magento 2Cannot read property `scopeLabel` of undefined on Product Detail PageCan't get Customer Data on frontend in Magento 2Magento2 Order Summary - unable to process bindingKO templates are not loading in Magento 2.1 applicationgetting knockout js error magento 2Product grid not load -— Unable to process binding Knockout.js magento 2Product form not loaded in magento2Uncaught ReferenceError: Unable to process binding “if: function()return (isShowLegend()) ” magento 2