how to iterate through rows of a dataframe along with summing up the values and stop when sum equals to a particular value

I have the dataframe as below. Below dataframe says how many coins a particular user has bought at a particular time

Id coins timestamp
1  3      t1
1  2      t2
1  6      t3
2  1      t1
2  4      t2

Below dataframe tells when the user starts spending the coins he has brought in the above table. For example

Id spent timestamp
1   2    t1.1
1   3    t1.2
1   1    t2
1   2    t3
1   4    t4

Based on the above two tables, I have to find at what timestamp user has completely used the coins he has bought at each timestamp. For example, output for the above data.

Id coins timestamp_bought timestamp_spent
1  3     t1               t1.2
1  2     t2               t1.2
1  6     t3               t4

What is the best way to achieve this using spark in java. I have tried the below methodology.

  1. I have joined both the tables with their id and timestamp_spent greater than timestamp_brought.
  2. Created a java pair rdd (JavaPairRDD>) by grouping with id key.
  3. I have collected the values of each id into a list and iterated over that.