Collections static boolean disjoint(Collection c1,Collection c2) method Example Program


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

Program

package com.candidjava;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Disjoint {

	/**
	 * @author : karthikeyan.T
	 * @description : the following code shows how to use Collections.copy(List<?
	 *              super T> dest, List<? extends T> src) method.
	 * */
		public static void main(String[] args) {
			List<String> als = new ArrayList<String>();
			List<String> ald = new ArrayList<String>();
			als.add("mohan");
			als.add("karthik");
			als.add("hari");
			als.add("kamal");
			als.add("anand");
			als.add("vinodh");
			ald.add("Hai mohan");
			ald.add("Hai karthik");
			ald.add("Hai hari");
			ald.add("Hai kamal");
			ald.add("Hai anand");
			ald.add("Hai vinodh");
			System.out.println("List 1 : " + als);
			System.out.println("List 2 : " + ald);
			System.out.println();
			boolean b= Collections.disjoint(als, ald);
			System.out.println("Disjoint is : "+b);
		}
	}

Output

List 1 : [mohan, karthik, hari, kamal, anand, vinodh]
List 2 : [Hai mohan, Hai karthik, Hai hari, Hai kamal, Hai anand, Hai vinodh]

Disjoint is : true

Explanation

public static boolean 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.

Parameters:
c1 - a collection
c2 - a collection
Returns:
true if the two specified collections have no elements in common.
Throws:
NullPointerException - if either collection is null.
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)
Since:
1.5


Related Post

Comments


©candidjava.com