Java Collections disjoint to check common element

Description

Java Collections disjoint to check common element


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
  public static void main(String[] args) {
    String[] langs = { "CSS", "SQL", "HTML", "C++" };
    List<String> list2 = new ArrayList<>();
    //  w w w . ja  va  2  s  .  co  m
    
    list2.add("Java");
    list2.add("Javascript");
    list2.add("C");

    
    List<String> list1 = new ArrayList<>(list2);
    System.out.println(list2);

    // check whether list1 and list2 have elements in common 
    boolean disjoint = Collections.disjoint(list1, list2);   

    System.out.printf("list1 and list2 %s elements in common%n",  
       (disjoint ? "do not have" : "have")); 

    
  }
}



PreviousNext

Related