3

How would I go about storing a HashSet with an unknown size in a mysql table. I know I can loop through it and store it into a longtext field.

However when I retrieve that field and store it into a string temporarily it will use extra memory to store that huge string of info.

Is there any easy way to store a HashSet?

4 Answers 4

5

A SQL table with indexed columns is basically a hash set.

You shouldn't try to store (persist) a binary representation of a HashSet in a table. You should store (persist) the data of your HashSet as rows and columns and then read that data into your HashSet on the Java side.

In other words, you should be using a database to store your data directly rather than saving a serialized representation of a Java collection that holds your data. That's what a database is meant to do... store/persist data in a structured, consistent manner. If all you really need to do is to serialize a HashSet then why bother with a database at all?

Sign up to request clarification or add additional context in comments.

3 Comments

This is the optimal solution if querying or indexing of the data in the HashSet is required. Otherwaise, just use a text representation (possibly JSON) like Bohemian suggested.
I would like to hear the scenario where you store structured data in a database and never expect to query it. Not saying that I don't think it's possible. I'd just like to hear the reasoning.
I've built a system to store used power equipment. We needed to store all the common fields in a structured way. There was also user-entered, meta-data though that was whatever they wanted to enter. There was no requirement for people to be able to search or filter on this data; it was only for display and comparison purposes so we just stored it in a JSON format within a single relational DB column, kinda emulating the whole "document store" effect you get with data stores like MongoDB. I will admit though, it was a unique scenario.
1

I would turn it into JSON and store it as text.

There are libraries out there that convert to and from JSON with ease - eg googles gson

2 Comments

This would certainly be better than a binary, serialized format in a blob but it still makes it hard to query and index if necessary. If it simply for storage and retrieval though, this is the way to go.
Fair comment, but the question didn't mention anything about searching the hash
0

Yes. Each element in a separate record in a table with foreign key to the 'parent' table. Yhay is how JPA handles it.

Comments

0

You can serialize it and save it as BLOB type. Because HashSet implements Serializable. http://download.oracle.com/javase/1.4.2/docs/api/java/util/HashSet.html

http://download.oracle.com/javase/1.4.2/docs/api/serialized-form.html#java.util.HashSet

3 Comments

This may be the "easiest" thing to implement but it makes maintenance difficult. Storing binary representations of data removes your ability to effectively query, index, or report on the data.
he didn't said the he's going to maintain and query via SQL. take your -1 back.
Agreed, he didn't say he needed to query the data. But your point about not needed to maintain it is just silly. Even if it is not YOU needing to maintain the software, it is still best to think about what it would take for someone else to do it and having a BLOB hold this data is just bad for everyone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.