How to Iterate through two ArrayLists Simultaneously?
JavaJava Problem Overview
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?
Java Solutions
Solution 1 - Java
You can use Collection#iterator
:
Iterator<JRadioButton> it1 = category.iterator();
Iterator<Integer> it2 = cats_ids.iterator();
while (it1.hasNext() && it2.hasNext()) {
...
}
Solution 2 - Java
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...
});
Solution 3 - Java
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()...
}
Solution 4 - Java
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);
..
}
Solution 5 - Java
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
Solution 6 - Java
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
}