Mercenary
Mercenary

Reputation: 2176

Check if values in one arraylist is present in another

I need to check if any of the values in one arraylist is present in another arraylist:

import java.util.ArrayList;

public class SampleCode {

    ArrayList<Integer> in = new ArrayList<>();
    ArrayList<Integer> is = new ArrayList<>();
    public static void main(String[] args) {
        new SampleCode().work();
    }
    public void work(){
        in.add(3);in.add(4);in.add(5);
        is.add(1);is.add(2);is.add(3);
        if(is.containsAll(in)){
            System.out.println("It does not contain");
        }
    }
}

It prints "It does not contain". I need to know if there is a way to compare these two arraylists and if any of the values are present in the other arraylist, it should return false. I know iterating can help. Is there any simple way to do this?

Upvotes: 1

Views: 3790

Answers (6)

AJ.
AJ.

Reputation: 4534

   Collection.indexOfSubList(List<?> source, List<?> target)

Returns the starting position of the first occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.

More formally, returns the lowest index i such that source.subList(i, i+target.size()).equals(target), or -1 if there is no such index. (Returns -1 if target.size() > source.size().) This implementation uses the "brute force" technique of scanning over the source list, looking for a match with the target at each location in turn.

Collections.disjoint(Collection<?> c1, Collection<?> c2)

Returns true if the two specified collections have no elements in common.

Care must be exercised if this method is used on collections that do not comply with the general contract for Collection. Implementations may elect to iterate over either collection and test for containment in the other collection (or to perform any equivalent computation). If either collection uses a nonstandard equality test (as does a SortedSet whose ordering is not compatible with equals, or the key set of an IdentityHashMap), both collections must use the same nonstandard equality test, or the result of this method is undefined. Care must also be exercised when using collections that have restrictions on the elements that they may contain. Collection implementations are allowed to throw exceptions for any operation involving elements they deem ineligible. For absolute safety the specified collections should contain only elements which are eligible elements for both collections. Note that it is permissible to pass the same collection in both parameters, in which case the method will return true if and only if the collection is empty.

Throws:

NullPointerException - if one collection contains a null element and null is not an eligible element for the other collection. (optional) NullPointerException - if one collection contains a null element and null is not an eligible element for the other collection. (optional) ClassCastException - if one collection contains an element that is of a type which is ineligible for the other collection. (optional)

Upvotes: 1

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26084

Try this one

public void work(){
        in.add(3);in.add(4);in.add(5);;
        is.add(1);is.add(2);is.add(3);;
        ArrayList<Integer> matched = new ArrayList<Integer>(in);
        matched.retainAll(is);
        if(matched.size()>0){
            System.out.println(matched);
        }else{
            System.out.println("It does not contain");
        }
    }

Upvotes: 1

sp00m
sp00m

Reputation: 48837

Another possible solution:

public static boolean containsNone(List<?> list, List<?> of) {
    List<?> temp = new ArrayList<Object>(list);
    temp.retainAll(of);
    return temp.isEmpty();
}

For example:

List<String> ref = Arrays.asList("w1", "w2", "w3");
List<String> ok = Arrays.asList("w4", "w5");
List<String> ko = Arrays.asList("w1", "w5");
System.out.println(containsNone(ok, ref));
System.out.println(containsNone(ko, ref));

Prints:

true
false

Upvotes: 1

Eel Lee
Eel Lee

Reputation: 3543

public boolean isListNotOverlapping(List<Integer> yourList1, List<Integer> yourList2) {
    for(Integer i : yourList1) {
         if (yourList2.contains(i)) {
             return false;
         }
    }
    return true;
}

Upvotes: 0

Apostolos
Apostolos

Reputation: 10498

Collections.disjoint(list1, list2);

is your answer

see Collections documentation

Upvotes: 0

hvgotcodes
hvgotcodes

Reputation: 120318

I think you can use

Collections.disjoint

which says

Returns true if the two specified collections have no elements in common.

so it will return false if there are any elements in common.

Upvotes: 7

Related Questions