Makes a shallow copy of the Set provided omitting those elements identified by the Collection removed . - Java Collection Framework

Java examples for Collection Framework:Collection

Description

Makes a shallow copy of the Set provided omitting those elements identified by the Collection removed .

Demo Code

/*//  w  w w.  j a v a 2 s  .  c o  m
 * Copyright Terracotta, Inc.
 *
 * Licensed 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 com.java2s;

import java.util.Collection;

import java.util.LinkedHashMap;
import java.util.LinkedHashSet;

import java.util.Map;
import java.util.Set;

public class Main {
    /**
     * Makes a shallow copy of the {@code Set} provided omitting those elements identified by the
     * {@code Collection} {@code removed}.
     *
     * @param source the {@code Set} from which the copy is made
     * @param removed the elements to omit from the copy
     *
     * @return a new, entry-ordered {@code Set} containing the elements from {@code source} less those
     *    specified in {@code removed}
     */
    static Set<String> copyWithout(final Set<String> source,
            final Collection<String> removed) {
        final Set<String> copy = new LinkedHashSet<String>(source);
        copy.removeAll(removed);
        return copy;
    }

    /**
     * Makes a shallow copy of the {@code Map} provided omitting those entries identified by the
     * keys in the {@code Collection} {@code removed}.
     *
     * @param source the {@code Map} from which the copy is made
     * @param removed the keys of the entries to omit from the copy
     *
     * @return a new, entry-ordered {@code Map} containing the entries from {@code source} less those
     *    specified in {@code removed}
     */
    static Map<String, String> copyWithout(
            final Map<String, String> source,
            final Collection<String> removed) {
        final Map<String, String> copy = new LinkedHashMap<String, String>(
                source);
        copy.keySet().removeAll(removed);
        return copy;
    }
}

Related Tutorials