60

I Have Two Array Lists, Declared as:

ArrayList<JRadioButton> category = new ArrayList<JRadioButton>();
ArrayList<Integer> cat_ids = new ArrayList<Integer>();

Both of the these fields contain exactly, the Same No of Values, which are infact corresponding in Nature.

I know I can iterate over one of the loops like this:

for(JRadioButton button: category)
{
     if(button.isSelected())
     {
           buttonName = button.getName();
           System.out.println(buttonName);       
     }
}

But, I would like to iterate over both the LISTS simultaneously. I know they have the exact same size. How do I Do that?

Maroun
  • 91,013
  • 29
  • 181
  • 233
Rohitink
  • 1,134
  • 3
  • 13
  • 21

6 Answers6

114

You can use Collection#iterator:

Iterator<JRadioButton> it1 = category.iterator();
Iterator<Integer> it2 = cats_ids.iterator();

while (it1.hasNext() && it2.hasNext()) {
    ...
}
Maroun
  • 91,013
  • 29
  • 181
  • 233
19

java8 style:

private static <T1, T2> void iterateSimultaneously(Iterable<T1> c1, Iterable<T2> c2, BiConsumer<T1, T2> consumer) {
    Iterator<T1> i1 = c1.iterator();
    Iterator<T2> i2 = c2.iterator();
    while (i1.hasNext() && i2.hasNext()) {
        consumer.accept(i1.next(), i2.next());
    }
}
//
iterateSimultaneously(category, cay_id, (JRadioButton b, Integer i) -> {
    // do stuff...
});
nix9
  • 608
  • 2
  • 6
  • 20
eugene82
  • 7,697
  • 2
  • 20
  • 30
16

If you do this often you may consider using a helper function to zip two lists into one pair list:

public static <A, B> List<Pair<A, B>> zip(List<A> listA, List<B> listB) {
    if (listA.size() != listB.size()) {
        throw new IllegalArgumentException("Lists must have same size");
    }

    List<Pair<A, B>> pairList = new LinkedList<>();

    for (int index = 0; index < listA.size(); index++) {
        pairList.add(Pair.of(listA.get(index), listB.get(index)));
    }
    return pairList;
}

You will also need a Pair implementation. Apache commons lang package has a proper one.

And with these you can now elegantly iterate on the pairlist:

ArrayList<JRadioButton> category = new ArrayList<JRadioButton>();
ArrayList<Integer> cat_ids = new ArrayList<Integer>();

for (Pair<JRadioButton, Integer> item : zip(category , cat_ids)) {
   // do something with JRadioButton
   item.getLeft()...
   // do something with Integer
   item.getRight()...
}
Zoltan.Tamasi
  • 1,352
  • 13
  • 26
  • 1
    will this work if the size of the lists are different ? – fiddle Jul 14 '15 at 19:58
  • 2
    No. That's why the throw new IllegalArgumentException... line is there. It's not obvious what to do if the two lists have different sizes: Trim the longer one, or use placeholders to fill the shorter. Because the placeholder can depend on the type of the lists, it's better not being handled in a generic implementation. – Zoltan.Tamasi Jul 15 '15 at 06:27
  • Great, and perfect! (sorry for thanx comment) – Daniel Hári Dec 19 '17 at 19:17
11

Try this

ArrayList<JRadioButton> category = new ArrayList<JRadioButton>();
ArrayList<Integer> cat_ids = new ArrayList<Integer>();
for (int i = 0; i < category.size(); i++) { 
    JRadioButton cat = category.get(i);
    Integer id= cat_ids.get(i);
    ..
}
Jaydeep Rajput
  • 3,505
  • 16
  • 35
1

Although you are expecting both sizes to be same, just to be on safer side get the sizes for both of them and make sure they are equal.

Let that size value be count. Then use generic for loop, iterate till count and acess the values as array indexes. If 'i' is the index, then acess as below in the for loop.

category[i] and cat_ids[i] 

category[i].isSelected() and so on

Trikaldarshiii
  • 10,994
  • 16
  • 64
  • 93
Rahul Sundar
  • 482
  • 7
  • 25
1
ArrayList<JRadioButton> category = new ArrayList<JRadioButton>();
ArrayList<Integer> cat_ids = new ArrayList<Integer>();
Iterator<JRadioButton> itrJRB = category.iterator();
Iterator<Integer> itrInteger = cat_ids.iterator();
while(itrJRB.hasNext() && itrInteger.hasNext()) {
    // put your logic here
}
AllTooSir
  • 47,910
  • 16
  • 124
  • 159