Let's say I have a vector of a hashset of strings:
Vector<HashSet<String>> strSetVector = new Vector<HashSet<String>>();
I have 4 hashsets containing the following strings:
"A", "B"
"C", "D"
"B", "C"
"E", "F"
I want to combine the sets that have at least one common value so that I end up with:
"A", "B","C", "D"
"E", "F"
The obvious solution is to iterate multiple times thru the vector and each hashset to find common values but this will take a while to process with a vector size of 1000+ and HashSets of size up to 100. I would also have to go thru the process again if i merge a hashset to see if there are now other hashsets that can be merged. For example, first vector iteration would combine B,C to A,B so that I would end up with:
"A", "B", "C"
"C", "D"
"E", "F"
Next iteration of the vector/hashset:
"A", "B", "C", "D"
"E", "F"
Next iteration of the vector/hashset would not find any common strings so there would be nothing to merge and I would be done.
I would like a more elegant solution to what seems like a simple problem. Any ideas?