I am trying to practice with Guava and I have come across a problem and I am not comfortable with the solution I have found.
In my situation, I have id set per user and server. Despite of incredible simplification by Guava Table I have still problems inside the loop if statement block in construction table.
Table<Integer, Integer, Set> setPerUser = HashBasedTable.create();
for(Conversation conversation : conversations) {
Set uci = setPerUser.get(conversation.x, conversation.y);
if(uci == null) {
uci = new HashSet();
setPerUser.put(conversation.x, conversation.y, uci);
}
uci.add(conversation.id);
}
It seems that I have in wrong direction and if statements are alarming the situtation. Ok,
-
Is there a better way to transform a iterator to Table such as FluentIterator?
-
What do you think to have an initial value method such as ThreadLocal or Guava Supplier?
Thanks
Since you are storing a collection in your
TableCells, what you actually need is a “Multitable”, that automatically creates Collections for you as needed (asMultimapdoes). I am pretty sure that this requirement is complex enough to be beyond what the Guava team are willing to implements.I’d say what you are doing is the best possible way to deal with the situation (apart from the fact that your Set should be generic, of course)
Here’s a static factory method that does what you want to do in a more generic way: