Compare two arraylists of different custom functions

Hi All,
I have two custom functions A and B which holds an Arraylist1 and Arraylist2 respectively. I need to create a separate custom function C to compare arraylist1 and arraylist2 from custom functions A and B.

Any help or docs on this please.

Either define those lists as static or return them in both methods and compare them afterwards.

Note: If you want to make sure that the same objects are present in both list (but in random order), you must sort both lists before comparison.

1 Like

hi,

each of function will return sorted list which are compared in a third function

1 Like

like

List numbers = new ArrayList<>()
Random r = new Random();
long xx = 100000000L;
long yy = 999999999L;
long number = 0L
number = xx+((long)(r.nextDouble()(yy-xx)));
numbers.add(number)
int counter = 8
while (counter >= 0){
number = xx+((long)(r.nextDouble()
(yy-xx)));
if(!numbers.contains(number)){
numbers.add(number)
counter–
}
}

Collections.sort(numbers, Collections.reverseOrder());
println ("numbers are in list with reversed order "+numbers)

numbers are in list with reversed order [911482675, 645759591, 580156490, 524209046, 462502075, 407193925, 388053954, 335252960, 277217303, 233413232]

more info here how to compare

1 Like

hello

maybe this would be help for you

TESTCASE
import com.kms.katalon.core.logging.KeywordLogger

CustomKeywords.'com.compare.two.lists.CompareTwoLists.getListsCompared'()

KeywordLogger logger = new KeywordLogger()
int n = 3; 
ArrayList<ArrayList<String> > aList =  new ArrayList<ArrayList<String> >(n);

aList = CustomKeywords.'com.compare.two.lists.CompareTwoLists.getNameListsCompared'()


if (aList.get(0).size > 0)
{
	logger.logInfo("Missing elements from list two: ")
	print ("Missing elements from list two: ")
	for (int a = 0; a < aList.get(0).size(); a++){
		print "missing name: "+aList.get(0).get(a)
		logger.logWarning("missing name: "+aList.get(0).get(a))
	}
}

if (aList.get(1).size() > 0){
	logger.logInfo("Missing elements from list one: ")
	print ("Missing elements from list one: ")
	for (int b = 0; b < aList.get(1).size(); b++){
		print "missing name: "+aList.get(1).get(b)
		logger.logWarning("missing name: "+aList.get(1).get(b))
	}
}

logger.logInfo("Common names are: ")
print ("Common names are: ")
for (int c = 0; c < aList.get(2).size(); c++){
	print "common name "+aList.get(2).get(c)
	logger.logInfo("common name: "+aList.get(2).get(c))
}

KEYWORD
class CompareTwoLists {

	@Keyword
	public List <String> getFirstNamesSortedList(){

		List <String> names = new ArrayList<>();
		names.add('Tim');
		names.add('John');
		names.add('David');
		names.add('Tor');

		Collections.sort(names, Collections.reverseOrder());
		return names;
	}

	@Keyword
	public List <String> getSecondNamesSortedList(){

		List <String> names = new ArrayList<>();
		names.add('Tim');
		names.add('John');
		names.add('Mat');
		names.add('Tor');

		Collections.sort(names, Collections.reverseOrder());
		return names;
	}

	@Keyword
	public ArrayList<ArrayList<String>> getNameListsCompared(){
		
		int n = 3;
		// Here aList is an ArrayList of ArrayLists
		ArrayList<ArrayList<String> > aList =  new ArrayList<ArrayList<String> >(n);

		List <String> listOne = new ArrayList<>();
		List <String> listTwo = new ArrayList<>();
		listOne = getFirstNamesSortedList();
		listTwo = getSecondNamesSortedList();

		//find missing elements from list two
		listOne.removeAll(listTwo);
		//println "missing elements from list two " + listOne;
		aList.add(listOne)
		
		listOne = getFirstNamesSortedList();
		listTwo = getSecondNamesSortedList();

		//find missing elements from list one
		listTwo.removeAll(listOne);
		//println "missing elements from list one " + listTwo;
		aList.add(listTwo)
		
		listOne = getFirstNamesSortedList();
		listTwo = getSecondNamesSortedList();

		//get common elements
		listTwo.retainAll(listOne);
		//println "common elements are: " + listTwo;
		aList.add(listTwo)
		

		return aList
	}
}

RESULTS
2019-09-19 19:40:07.657 WARN  c.k.katalon.core.logging.KeywordLogger   - Missing elements from list two: 
2019-09-19 19:32:51.369 WARN  c.k.katalon.core.logging.KeywordLogger   - missing name: David

2019-09-19 19:40:07.758 WARN  c.k.katalon.core.logging.KeywordLogger   - Missing elements from list one: 
2019-09-19 19:40:07.800 WARN  c.k.katalon.core.logging.KeywordLogger   - missing name: Mat

2019-09-19 19:40:07.815 INFO  c.k.katalon.core.logging.KeywordLogger   - Common names are: 
2019-09-19 19:40:07.862 INFO  c.k.katalon.core.logging.KeywordLogger   - common name: Tor
2019-09-19 19:40:07.906 INFO  c.k.katalon.core.logging.KeywordLogger   - common name: Tim
2019-09-19 19:40:07.962 INFO  c.k.katalon.core.logging.KeywordLogger   - common name: John
1 Like

If you want to simply compare that the two lists are exactly equal (i.e. same number of elements in the same order), then simply:

public boolean compareLists(List<Object> list1, List<Object> list2) {
    return list2.equals(list1)
}

If you want to make sure the lists contain the same elements, and disregard the ordering of those elements (thanks to @Marek_Melocik for catching my mistake :slight_smile: ):

public boolean compareLists(List<Object> list1, List<Object> list2) {
    return list1.size() == list2.size() && list2.containsAll(list1)
}

See the documentation for more on these.

2 Likes

Be careful while using this code. It would return true in this case:

List 1: a, b, c, d
List 2: a, b, c, d, e, f

There is quick fix:

return list1.size() == list2.size() && list2.containsAll(list1)

3 Likes

Thanks for catching that, I will update my answer with due credits!

1 Like