In context of a web application, Is it suitable to do some JOIN operation in Java, on the data retrieved from the database(from first query), and use that \"JOIN\"ed
For sure, you can do a fast JOIN in Java. I know only about three reasons against doing it:
Without an SQL DB you have no choice, anyway. So, assuming the simplest case, i.e., joining on a condition like A.x = B.x
, you can do it easily by computing the intersection using Set.retainAll
. For each member of the intersection you need to combine all corresponding rows from the first table with all corresponding rows from the second one.
Assuming there are no more interesting columns in the tables, you're done. Otherwise you need to attach them somehow. In case x
is unique in both A
and B
you can use Maps, otherwise you need a Multiset (there are many implementations available, e.g., in Guava).
Are you asking how to do a set intersection in Java? Given your definition of the data--two datasets, entirely integers--here is my suggestion:
Set a = new HashSet(the list of elements from the first dataset) Set b = new HashSet(the list of elements fro the second dataset)
Set joined = a.retainAll(b); // this is how to do set intersections with java.util.Set
Joining data in Java is not a bad idea at all. In some situations it it may be faster and more sensible than doing joins in the database (it depends on many factors). Especially if you have no other choice :-)
If you can get the results from the database as standard jdbc resultsets, then you can use the family of intelligent rowsets: JoinRowSet, FilteredRowSet etc. They gather tabular data from the database and them join and filter, and serialize them on the Java side (bit like ado.net). Standard implementations are not part of the standard, but you will find them in com.sun.rowset package.
If you only need to make this single join, they might be an overkill, but since you are using some nosql db, you might want to standardize the way you filter and join.