Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java contains any

farenda 2015-07-16 0

Problem:

How to check that a collection in Java contains any of elements from some other collection? It’s very common problem when working on Sets, but also applicable to Java Lists and other collections.

Sometimes problems are modeled in a way that one can work on them using Set Theory. One of common operations on sets, and as a matter of fact, on different types of collections, is to find that that two Sets/Lists/Maps/Collections are completely different, so have no common elements.

Solution:

The operation returns true if two collections have no common elements. In Java contains any is implemented as Collections.disjoint(Collection<?> c1, Collection<?> c2) and works as in the following Java example:

package com.farenda.java;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class CollectionsDisjoint {

    public static void main(String[] args) {
        List<String> c1 = Arrays.asList("effective", "java");
        Set<String> c2 = new HashSet<>(Arrays.asList("java", "tutorial"));

        areDifferent(c1, c2);

        areDifferent(c1, Arrays.asList("clojure", "scala"));
    }

    private static void areDifferent(Collection<String> c1,
                                     Collection<String> c2) {
        // In other words: have _NO_ common elements:
        System.out.printf("%s and %s are completely different: %s%n",
                c1, c2, Collections.disjoint(c1, c2));
    }
}

In the code above we’ve used Java List as one collection and Java Set as the other. Collections.disjoint(Collection<?> c1, Collection<?> c2) can compare any types of collections, but they have to have elements eligible for both types of collections (simplifying – of the same type).

And the result of running the above Java example:

[effective, java] and [[java, tutorial]] are completely different: false
[effective, java] and [[clojure, scala]] are completely different: true
Share with the World!
Categories Java Tags java-collections
Previous: Java List copy
Next: Java find/count occurrences

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored