Which method returns true if the collection contains all of the elements in the given collection?

There is Category.containsEvery, which is inherited by Iterable. It checks for each element of the parameter whether it is contained in the receiver, so that bigger.containsEvery(smaller) is equivalent to this:

smaller.every(bigger.contains)

(Note that it is swapped around.) The expression in the brackets here is a method reference, we could also write this expanded with a lambda:

smaller.every(o => bigger.contains(o))

So in your example:

print({'a', 'b', 'b'}.containsEvery({'b', 'a'})); // Should be true print({'a', 'b', 'b'}.containsEvery({'a', 'a'})); // Should be false

... actually, those both return true. Why do you think the latter one is false?

Did you think of multiset semantics (i.e. the number of occurrences in the "superset" iterable need to be at least as much as the smaller one)? Or do you want a sublist? Or do you just want to know whether the second iterable is at the start of the first (startswith)?

I don't know about any multiset implementation for Ceylon (I found a multimap, though). If you are running on the JVM, you can use any Java one, like from Guava (though that also doesn't have a "contains all with multiples" function, as far as I can see). For small iterables, you can use .frequencies() and then compare the numbers:

Boolean isSuperMultiset<Element>({Element*} bigger, {Element*} smaller) => let (bigFreq = bigger.frequencies()) every({ for(key->count in smaller.frequencies()) count <= (bigFreq[key] else 0) })

For sublist semantics, the SearchableList interface has the includes method, which checks whether another list is a sublist. (It is not implemented by many classes, though, you would need to convert your first iterable into an Array, assuming it is not a String/StringBuilder.)

For startsWith semantics, you could convert both to lists and use then List.startsWith. There should be a more efficient way of doing that (you just could go through both iterators in parallel).

There is corresponding, but it just stops after the shorter one ends (i.e. it answers the question "does any of those two iterables start with the other", without telling which one is the longer one). Same for a bunch of other pair related functions in ceylon.language.

If you know the length of both of the Iterables (or are confident that .size is fast), that should solve the issue:

Boolean startsWith<Element>({Element*}longer, {Element*}shorter) => shorter.size <= longer.size && corresponding(longer, shorter);

Which method returns true if the collection contains all of the elements in the given collection?
Siddharth Singh

The kotlin.collections package is part of Kotlin’s standard library and contains all collection types, such as Map, List, Set, etc.

The package provides the ArrayList class, which is a mutable list and uses a dynamic, resizable array as the backing storage.

The containsAll() method

The ArrayList class contains the containsAll() method, which checks for the presence of all elements of the specified input collection in the array list.

Syntax

fun containsAll(elements: Collection<E>): Boolean

Parameters

The containsAll() method takes the collection of elements to search as input.

Return value

containsAll() returns true if all the elements of the input collection are present in the array list, and false otherwise.

Things to note

  • ArrayList follows the sequence of insertion of elements.

  • ArrayList is allowed to contain duplicate elements.

The illustration below shows the function of the containsAll() method.

fun main(args : Array<String>) {

val monthList = ArrayList<String>()

monthList.add("January")

monthList.add("February")

monthList.add("March")

monthList.add("April")

monthList.add("May")

println("Printing ArrayList elements--")

println(monthList)

val inputList = ArrayList<String>()

inputList.add("January")

inputList.add("February")

println("Printing Input collection elements--")

println(inputList)

val result = monthList.containsAll(inputList)

println("Is input list present in the array list? : ${result}")

}

  • Line 2: We create an empty array list named monthList to store the strings.

  • Line 4 to 8: We use the add() method to add a few month names to the ArrayList object, such as "January", "February", "March", "April", and "May".

  • Line 12 to 14: We create a new array list inputList and add month names "January" and "February" to it.

  • Line 17: We call the containsAll() method and pass the inputList collection as the parameter. The containsAll() method returns true, as all elements of the input collection are present in the array list.

We use the println() function of the kotlin.io package to display the Arraylist elements and result of the containsAll() method.

RELATED TAGS

The syntax of the containsAll() method is:

arraylist.containsAll(Collection c);

Here, arraylist is an object of the ArrayList class.

containsAll() Parameters

The containsAll() method takes a single parameter.

  • collection - checks if all elements of collection are present in the arraylist.

containsAll() Return Value

  • returns true if the arraylist contains all elements of collection
  • throws ClassCastException if the class of elements present in arraylist is incompatible with the class of elements in the specified collection
  • throws NullPointerException if collection contains null elements and the arraylist does not allow null values

Note: We can say that the containsAll() method checks if the collection is a subset of the arraylist.

Example 1: Java ArrayList containsAll()

import java.util.ArrayList; class Main { public static void main(String[] args) { // create an ArrayList ArrayList<String> languages1 = new ArrayList<>(); // insert element to the ArrayList languages1.add("JavaScript"); languages1.add("Python"); languages1.add("Java"); System.out.println("ArrayList 1: " + languages1); // create another ArrayList ArrayList<String> languages2 = new ArrayList<>(); // add elements to ArrayList languages2.add("Java"); languages2.add("Python"); System.out.println("ArrayList 2: " + languages2); // check if ArrayList 1 contains ArrayList 2 boolean result1 = languages1.containsAll(languages2); System.out.println("ArrayList 1 contains all elements of ArrayList 2: " + result1); // check if ArrayList 2 contains ArrayList 1 boolean result2 = languages2.containsAll(languages1); System.out.println("ArrayList 2 contains all elements of ArrayList 1: " + result2); } }

Output

ArrayList 1: [JavaScript, Python, Java] ArrayList 2: [Java, Python] ArrayList 1 contains all elements of ArrayList 2: true ArrayList 2 contains all elements of ArrayList 1: false

In the above example, we have created two arraylists named languages1 and languages2. Notice the expression,

// return true languages1.containsAll(languages2)

Here, the containsAll() method checks if languages1 contains all elements of languages2. Hence, the method returns true. However, notice the following expression,

// return false languages2.containsAll(languages1)

Here, the containsAll() method checks if languages2 contains all elements of languages1. Hence, it returns false.

Note: The containsAll() method is not specific to the ArrayList class. The class inherits from the List interface.

Example 2: containsAll() Between Java ArrayList and HashSet

import java.util.ArrayList; import java.util.HashSet; class Main { public static void main(String[] args) { // create an ArrayList ArrayList<Integer> numbers = new ArrayList<>(); // add element to ArrayList numbers.add(1); numbers.add(2); numbers.add(3); System.out.println("ArrayList: " + numbers); // create a HashSet HashSet<Integer> primeNumbers = new HashSet<>(); // add elements to HashSet primeNumbers.add(2); primeNumbers.add(3); System.out.println("HashSet: " + primeNumbers); // check if ArrayList contains all elements of HashSet boolean result1 = numbers.containsAll(primeNumbers); System.out.println("ArrayList contains all elements of HashSet: " + result1); // check if HashSet contains all elements of ArrayList boolean result2 = primeNumbers.containsAll(numbers); System.out.println("HashSet contains all elements of ArrayList: " + result2); } }

Output

ArrayList: [1, 2, 3] HashSet: [2, 3] ArrayList contains all elements of HashSet: true HashSet contains all elements of ArrayList: false

In the above example, we have created an arraylist named numbers and a hashset named primeNumbers. Notice the expressions,

// check if ArrayList contains HashSet // return true numbers.containsAll(primeNumbers) // check if HashSet contains ArrayList // return false primeNumbers.containsAll(numbers)

Note: We can get the common elements between ArrayList and HashSet using the Java ArrayList retainAll() method.