Performing Join Operation in Java, on a small sets of data retrieved from the database. (context: web application)

前端 未结 3 1554
小鲜肉
小鲜肉 2021-01-03 14:17

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

3条回答
  •  难免孤独
    2021-01-03 14:28

    For sure, you can do a fast JOIN in Java. I know only about three reasons against doing it:

    • it's additional work
    • the non-matching rows get fetched needlessly
    • the amount of data may be too large to be handled in memory

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

提交回复
热议问题