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
You can compare two array lists using the equals() method of the ArrayList class, this method accepts a list object as a parameter, compares it with the current object, in case of the match it returns true and if not it returns false.
The clone() method of the ArrayList class is used to clone an ArrayList to another ArrayList in Java as it returns a shallow copy of its caller ArrayList.
Step 1: Declare the ArrayList 1 and add the values to it. Step 2: Create another ArrayList 2 with the same type. Step 3: Now, simply add the values from one ArrayList to another by using the method addAll().
An ArrayList can contain multiple references to the same object. The same object may belong to 2 different ArrayLists. ArrayList's add method makes a copy of the object and adds it to the list. Two variables can refer to the same Arraylist.
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;
}
In Java 8+, you can use stream().skip()
to skip intial list1.size() - list2.size()
objects, then compare with list2
.
if (list1.size() > list2.size()) {
AtomicInteger ordinal = new AtomicInteger(0);
boolean matched = list1.stream().skip(list1.size() - list2.size())
.allMatch(item -> item == list2.get(ordinal.getAndIncrement()));
System.out.println(matched);
}
Generate a new list, that is the last n items of list1, where n is the length of list 2. Then compare list 3 to list 1.
The 3rd list can be delivered like this:
ArrayList list3 = new ArrayList(list1.subList(list1.size() - list2.size(), list2.size())
To check, that list1 contain list2 from specific position, you can use function:
public static boolean compareArrsFromPosition(List<?> list1, List<?> list2, int fromPosition) {
if (list1.size()-fromPosition < list2.size()) return false;
return list1.subList(fromPosition,fromPosition+list2.size()).equals(list2);
}
For check of end of list1 you can call like this
compareArrsFromPosition(list1, list2, list1.size()-list2.size());
Alternatively, you can reverse both of your List
s (linear time though might require space to store a copy) and match all the elements iterating based on the size of list B
(assuming it would be of smaller size) as :
public static boolean listEndsWith(List<?> A, List<?> B) {
Collections.reverse(B); // modifies B, so you can choose to clone and reverse
Collections.reverse(A);
return IntStream.range(0, B.size())
.allMatch(i -> A.get(i).equals(B.get(i)));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With