62

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
  • 94,125
  • 30
  • 188
  • 241
Rohitink
  • 1,154
  • 3
  • 14
  • 21

6 Answers6

118

You can use Collection#iterator:

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

while (it1.hasNext() && it2.hasNext()) {
    ...
}
Maroun
  • 94,125
  • 30
  • 188
  • 241
21

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
  • 638
  • 2
  • 8
  • 20
eugene82
  • 8,432
  • 2
  • 22
  • 30
18

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,382
  • 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
12

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,605
  • 17
  • 35
  • 4
    I would add i < category.size() && i < cat_ids.size() just in case – giorashc Apr 13 '13 at 07:16
  • 1
    Little out of scope, but always use interface when your are working with collections. List category = new ArrayList(); – Vaclav Stengl Jul 04 '17 at 11:02
  • 1
    Not a nice answer. Typically you only know the interface (List), the get(i) method makes this solution O(n^2). Avoid. – gabor Dec 10 '21 at 12:53
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
  • 11,174
  • 16
  • 67
  • 95
Rahul Sundar
  • 480
  • 8
  • 26
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
  • 48,828
  • 16
  • 130
  • 164