cancel
Showing results for 
Search instead for 
Did you mean: 

How to compare multiple arraylists ....

Former Member
0 Kudos

with different length?

Imagine you´ve got one ParentArrayList, which contains one or more ArrayLists as elements. The (Child)ArrayLists contains multiple String elements of different size.

Here is a small Graphic on this:

Parent_ArrayList

#

#

####Child_ArrayList_1->element1->element2->element3

#

#

####Child_ArrayList_2->element1->....elementn

#

#

####Child_ArrayList_n->element1->element2->element3

E.g.:I have to compare all the elements of the child_arraylists_1 with child_arraylists_2 , write all elements which are euqal into an other temparrayList.Then Compare temp arraylist with elements of Child_ArrayList_n and write it back into the temp_arraylist.

I hope I could explain my problem?!?

Thank you for any suggestions?!

Accepted Solutions (0)

Answers (1)

Answers (1)

former_member182372
Active Contributor
0 Kudos

Hi Mehmet,


	private final static String[][] initial_data = {{"1", "2", "3", "5"},
													{"2", "3", "5", "8", "12"},
													{"3", "5", "7", "12", "20"},
													{"4", "7", "3", "9", "5", "2"},
													{"5", "9", "2", "7", "9", "0", "3"}};
													
	private final static List Parent_ArrayList = new ArrayList();
	
	static {
		for (int i = 0; i < initial_data.length; i++) {
			final List Child_ArrayList = new ArrayList();
			for (int j = 0; j < initial_data<i>.length; j++) {
				Child_ArrayList.add(initial_data<i>[j]);
			}
			Parent_ArrayList.add( Child_ArrayList );
		}
	}

			Set common = null;
			for (Iterator iter = Parent_ArrayList.iterator(); iter.hasNext();) {
				List element = (List) iter.next();
				Set tempSet = new HashSet(element);
				if(null==common) {
					common = tempSet; 
				} else {
					common.retainAll(tempSet);
				}
			}

common will contain common string from all children.

Best regards, Maksim Rashchynski.

Former Member
0 Kudos

Hello Maksim,

Thank you for your suggestion and coding sample.

I´ve solved the problem without multiple arraylists.

kind regards,

Mehmet