-4

I have a table that is the main table and then I have a two table(a,b) that have a foreign key of the main table. How can i retrieve by the a table and b table example:

table header is main id=1

then on the table a got inserted one , two than table b got inserted one, two

I want to display it by the row is one-one then two-two but right now i got an issue by displaying one-one, one-two, two-one, two-two my friend said that i must use hashmap the code below make me loop for unnecessary value

//this my code

ArrayList<Main>listMain = mainDAO.getlAllMain(Connection con);
for(Main listMain : listMain) {
  ArrayList<A> listA = aDAO.getallbyMainId(Connection con, int main);
  for(A listA : listA) {
    ArrayList<B> listB = bDAO.getallbyMainId(Connection con, int main);
    for(B listB : listB) {
      Main main = new Main();
      main.setMainName(listMain.getName());
      a.set(listA.getName);
      b.set(listB.getName);
    }
  }
}
Anton Balaniuc
  • 10,889
  • 1
  • 35
  • 53
Steven Y.
  • 41
  • 1
  • 6
  • 4
    I'm lost. Can your friend help you? – nicomp Jul 09 '18 at 13:57
  • 4
    `punctuation marks` srsly, use them... You really need to clarify your question, otherwise nobody can help you becaus nobody understands you. Also why do you need our help if your friend seems to have an idea? – XtremeBaumer Jul 09 '18 at 14:00
  • 1
    @AntonBalaniuc Thanks, that makes it a little bit better. – Glains Jul 09 '18 at 14:04
  • *"this my code"* well: no, this code does not compile. – Timothy Truckle Jul 09 '18 at 14:09
  • Hey friend, you really need to read over this: https://stackoverflow.com/help/how-to-ask In your question, you post your title as "for each nested for each"... That is not a question. Also, you write what you have done, but you have not specifically asked any question at all. Revise the question and I would be 100% happy to help. – Travis Tubbs Jul 09 '18 at 14:19

1 Answers1

0

You shouldn't use nested for-each loops here.

Try an indexed for on the listMain and store the result of each iteration in a kind of tuple. This tuple could be stored in a Map. As your friend mentioned, you could use a HashMap for this. Something like this should do the trick:

    ArrayList<Main>listMain = mainDAO.getlAllMain(Connection con);
    Map<Integer, Pair<A, B>> map = new Hashmap<>();

    for (int i = 0; i < listMain.size(); i++) {
       ArrayList<A> listA = aDAO.getallbyMainId(Connection con, int main);
       ArrayList<B> listB = bDAO.getallbyMainId(Connection con, int main);
       A aValue = listA.get(i);
       B bValue = listB.get(i);
       map.put(i, new Pair(aValue, bValue));
    }

I'm using the apache-commons class Pair as a tuple here. See below to get more information about tuples in java. You could for example use a custom class for this purpose.

It is also possible to change the type of the pair content and the map key to be a String depending on the information u want to use later.

I hope i understood your problem correctly and that this answer is helping you.

See also

A Java collection of value pairs? (tuples?)

Microbob
  • 66
  • 4