5

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

1
  • assuming the list have no duplicates (each alone): find all elements that are on both (e.g. using retainAll()); remove all this elements (removeAll) and add them again at the end (addAll()) - eventually using a copy of the first list. Or maybe, stream-like: for every element on list2, if it is also on list1, remove it from list1 and add it to the end of list1... Commented Dec 7, 2018 at 6:04

5 Answers 5

4

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;
}
Sign up to request clarification or add additional context in comments.

Comments

3

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);
    }

Comments

2

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())

2 Comments

Cool idea, though this would throw an IndexOutOfBoundsException if list2 is longer than list1, so you'd need to check that first.
Yes, you are right. I like your method better in that regard
1

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());

1 Comment

You can do it as a single statement, return list1.size()-fromPosition >= list2.size() && list1.subList(fromPosition,fromPosition+list2.size()).equals(list2);.
1

Alternatively, you can reverse both of your Lists (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)));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.