All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.elasticsearch.common.util.set.Sets Maven / Gradle / Ivy

There is a newer version: 8.14.1
Show newest version
/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.common.util.set;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

public final class Sets {
    private Sets() {
    }

    public static  HashSet newHashSet(Iterator iterator) {
        Objects.requireNonNull(iterator);
        HashSet set = new HashSet<>();
        while (iterator.hasNext()) {
            set.add(iterator.next());
        }
        return set;
    }

    public static  HashSet newHashSet(Iterable iterable) {
        Objects.requireNonNull(iterable);
        return iterable instanceof Collection ? new HashSet<>((Collection)iterable) : newHashSet(iterable.iterator());
    }

    public static  HashSet newHashSet(T... elements) {
        Objects.requireNonNull(elements);
        HashSet set = new HashSet<>(elements.length);
        Collections.addAll(set, elements);
        return set;
    }

    public static  Set newConcurrentHashSet() {
        return Collections.newSetFromMap(new ConcurrentHashMap<>());
    }

    public static  boolean haveEmptyIntersection(Set left, Set right) {
        Objects.requireNonNull(left);
        Objects.requireNonNull(right);
        return !left.stream().anyMatch(k -> right.contains(k));
    }

    public static  Set difference(Set left, Set right) {
        Objects.requireNonNull(left);
        Objects.requireNonNull(right);
        return left.stream().filter(k -> !right.contains(k)).collect(Collectors.toSet());
    }

    public static  Set union(Set left, Set right) {
        Objects.requireNonNull(left);
        Objects.requireNonNull(right);
        Set union = new HashSet<>(left);
        union.addAll(right);
        return union;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy