Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 784
NarendraR Avatar asked Dec 07 '18 05:12

NarendraR


People also ask

How do you compare items in an ArrayList?

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.

Can you make an ArrayList equal to another ArrayList?

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.

Can you set two ArrayLists equal to each other?

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

Can an ArrayList can contain multiple references to the same object?

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.


5 Answers

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;
}
like image 118
nbrooks Avatar answered Oct 15 '22 05:10

nbrooks


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);
    }
like image 32
Naghaveer R Avatar answered Oct 15 '22 04:10

Naghaveer R


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())
like image 23
Jonathan Wilson Avatar answered Oct 15 '22 04:10

Jonathan Wilson


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());
like image 31
Dim78 Avatar answered Oct 15 '22 05:10

Dim78


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)));
}
like image 32
Naman Avatar answered Oct 15 '22 06:10

Naman