當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java java.util.Collections.disjoint()用法及代碼示例


描述

這個disjoint(Collection<?>, Collection<?>)如果兩個指定的集合沒有共同的元素,則使用 'true' 方法。

聲明

以下是聲明java.util.Collections.disjoint()方法。

public static boolean disjoint(Collection<?> c1,Collection<?> c2)

參數

  • c1- 這是一個集合。

  • c2- 這是另一個集合。

返回值

NA

異常

NullPointerException- 如果任一集合為空,則拋出此問題。

示例

下麵的例子展示了 java.util.Collections.disjoint() 的用法

package com.tutorialspoint;

import java.util.*;

public class CollectionsDemo {
   public static void main(String args[]) {

      // create two lists    
      List<String> srclst = new ArrayList<String>(5);
      List<String> destlst = new ArrayList<String>(10);

      // populate two lists
      srclst.add("Java");
      srclst.add("is");
      srclst.add("best");

      destlst.add("C++");
      destlst.add("is not");
      destlst.add("older");      

      // check elements in both collections
      boolean iscommon = Collections.disjoint(srclst, destlst);

      System.out.println("No commom elements:"+iscommon);    
   }    
}

讓我們編譯並運行上麵的程序,這將產生以下結果。

No commom elements:true

相關用法


注:本文由純淨天空篩選整理自 java.util.Collections.disjoint() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。