How to compare multiple items of an arraylist into another arraylist at specific position?

  • A+

Background: there is product listing page and i have to grab all the product name (including out of stock product) and then have to verify that all out of stock product are in the end.

Problem : i have navigated all the page and stored the product names in an ArrayList.

lets say list1 and contents are -

[instant bcaa, vegan bcaa, complete bcaa energy™, branched chain amino acid (bcaa) tablets 1000mg, endure™, branched chain amino acids (bcaa), instant leucine, leucine tablets 1000mg, complete intra-workout™, leucine, bcaa jelly mix, complete hydration drink™, informed bcaa™, instant bcaa cocktail bundle] 

Now i have another list which have only Out Of Stock product

list2 and contents are -

[informed bcaa™, instant bcaa cocktail bundle] 

I have to make sure whether list1 has all the list2 items in the end in same sequence


This problem is essentially attempting to validate that a given list A, ends with a second list B.

You could implement this by determining the length of list B, backtracking that many spaces from the end of list A, and then doing a pair-wise comparison of both lists:

public static boolean listEndsWith(List<?> A, List<?> B) {     if (B.size() > A.size()) {         return false;     }      for (int i = A.size() - B.size(), j = 0; i < A.size(); i++, j++) {         if (!A.get(i).equals(B.get(j))) {             return false;         }     }      return true; } 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: