How to check is there any common element between two List of String?
UPDATED: 17 December 2014
Tags:
Collection
,
List
Collections.disjoint(Collection<?> c1, Collection<?> c2)
Returns true if the two specified collections have no elements in common.
Source Code
import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * @author javaQuery */ public class DisjointExample { public static void main(String[] args) { /* Create list of String */ List<String> listString = new ArrayList<String>(); /* Add elements in listString */ listString.add("a"); listString.add("b"); listString.add("c"); /* Create list of String */ List<String> listStringSecond = new ArrayList<String>(); /* Add elements in listString */ listStringSecond.add("x"); listStringSecond.add("y"); listStringSecond.add("z"); System.out.println("Initial List 1: " + listString); System.out.println("Initial List 2: " + listStringSecond); System.out.println("------------------------"); System.out.println("Disjoint: " + Collections.disjoint(listString, listStringSecond)); /* Add element available in listString */ listStringSecond.add("a"); System.out.println("------------------------"); System.out.println("List 2 after add(\"a\"): " + listStringSecond); System.out.println("Disjoint after add(\"a\"): " + Collections.disjoint(listString, listStringSecond)); } }
Output
Initial List 1: [a, b, c] Initial List 2: [x, y, z] ------------------------ Disjoint: true ------------------------ List 2 after add("a"): [x, y, z, a] Disjoint after add("a"): false
Tags:
Collection
,
List
0 comments :