Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

hashset

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).

like image 493
B Yellow Avatar asked Mar 15 '19 21:03

B Yellow


People also ask

Does every object have a hashCode?

In Java, every object has a method hashCode that is simple to understand but still it's sometimes forgotten or misused. Here are three things to keep in mind to avoid the common pitfalls.

Which of the following method is used to find out hashCode of an object?

Java Object hashCode() is a native method and returns the integer hash code value of the object. The general contract of hashCode() method is: Multiple invocations of hashCode() should return the same integer value, unless the object property is modified that is being used in the equals() method.

Can two of the same objects have the same hash?

It is perfectly legal for two objects to have the same hashcode. If two objects are equal (using the equals() method) then they have the same hashcode. If two objects are not equal then they cannot have the same hashcode.

What is a HashSet?

HashSet is a class that extends AbstractSet and implements the Set interface in Java. It is a very useful tool that allows you to store unique items and access them in constant time (on average). No duplicate values are stored.

What are the three characteristics of the hash function in the data structure?

The three characteristics of the hash function in the data structure are: Hashing in data structure uses hash tables to store the key-value pairs. The hash table then uses the hash function to generate an index. Hashing uses this unique index to perform insert, update, and search operations.

How is data stored in a hash table?

In a hash table, data is stored in an array format, where each data value has its own unique index value. Access of data becomes very fast if we know the index of the desired data.

Which of the following is an application of hash tables?

Message Digest is one of the applications of Hash tables. Password Verification also uses Hash tables. Data Structures (Programming Languages). Hash tables play an important role in synchronization. Hash tables are used in Compiler Operation.

What is hashing in data structure in Java?

With hashing in the data structure, you can narrow down the search and find the number within seconds. You can also consider doing our Java Bootcamp course from upGrad to upskill your career. This blog will give you a deeper understanding of the hash method, hash tables, and linear probing with examples. What is Hashing in Data Structure?


2 Answers

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. Each hash value maps to a single bit.

The Guava library provides an implementation in Java.

like image 106
Andy Thomas Avatar answered Oct 12 '22 23:10

Andy Thomas


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.

like image 23
Mark Avatar answered Oct 13 '22 00:10

Mark