Gets a new Set that is the interleaved union of the Set s provided. - Java Collection Framework

Java examples for Collection Framework:Set

Description

Gets a new Set that is the interleaved union of the Set s provided.

Demo Code

/*/*from w  w  w. j  a va 2s .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.
 */
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Main{
    public static void main(String[] argv) throws Exception{
        List keySets = java.util.Arrays.asList("asdf","java2s.com");
        System.out.println(fanIn(keySets));
    }
    /**
     * Entries for populating {@link org.ehcache.spi.cache.Store Store} and
     * {@link org.ehcache.spi.loader.CacheLoader CacheLoader} instances in the
     * unit tests.  The entries used are generally subset using the key sets
     * {@link #KEY_SET_A}, {@link #KEY_SET_B}, {@link #KEY_SET_C}, and/or
     * {@link #KEY_SET_F}.
     * <p/>
     * Some tests are dependent on the order of the keys/entries.  In general,
     * for each key set ('xxxXn'), the keys/entries must be ordered by 'n'.
     */
    static final Map<String, String> TEST_ENTRIES;
    /**
     * Gets a new {@code Set} that is the interleaved union of the {@code Set}s provided.
     *
     * @param keySet1 the first {@code Set} of keys to appear in the result {@code Set}
     * @param keySet2 the second {@code Set} of keys to appear in the result {@code Set}
     *
     * @return a new, modifiable {@code Set} holding the designated keys
     *
     * @see #fanIn(java.util.List)
     */
    static Set<String> fanIn(final Set<String> keySet1,
            final Set<String> keySet2) {
        final List<Set<String>> keySets = new ArrayList<Set<String>>();
        keySets.add(keySet1);
        keySets.add(keySet2);
        return fanIn(keySets);
    }
    /**
     * Gets a new {@code Set} that is the interleaved union of the {@code Set}s provided.
     *
     * @param keySet1 the first {@code Set} of keys to appear in the result {@code Set}
     * @param keySet2 the second {@code Set} of keys to appear in the result {@code Set}
     * @param keySet3 the third {@code Set} of keys to appear in the result {@code Set}
     *
     * @return a new, modifiable {@code Set} holding the designated keys
     *
     * @see #fanIn(java.util.List)
     */
    static Set<String> fanIn(final Set<String> keySet1,
            final Set<String> keySet2, final Set<String> keySet3) {
        final List<Set<String>> keySets = new ArrayList<Set<String>>();
        keySets.add(keySet1);
        keySets.add(keySet2);
        keySets.add(keySet3);
        return fanIn(keySets);
    }
    /**
     * Gets a new {@code Set} that is the interleaved union of the {@code Set}s provided.
     *
     * @param keySet1 the first {@code Set} of keys to appear in the result {@code Set}
     * @param keySet2 the second {@code Set} of keys to appear in the result {@code Set}
     * @param keySet3 the third {@code Set} of keys to appear in the result {@code Set}
     * @param keySet4 the fourth {@code Set} of keys to appear in the result {@code Set}
     *
     * @return a new, modifiable {@code Set} holding the designated keys
     *
     * @see #fanIn(java.util.List)
     */
    static Set<String> fanIn(final Set<String> keySet1,
            final Set<String> keySet2, final Set<String> keySet3,
            final Set<String> keySet4) {
        final List<Set<String>> keySets = new ArrayList<Set<String>>();
        keySets.add(keySet1);
        keySets.add(keySet2);
        keySets.add(keySet3);
        keySets.add(keySet4);
        return fanIn(keySets);
    }
    /**
     * Gets a new {@code Set} that is the interleaved union of the {@code Set}s provided.  The
     * set returned from this operation is in the following order:
     * <ul>
     *   <li>keySet[0][0]</li>
     *   <li>keySet[1][0]</li>
     *   <li>...</li>
     *   <li>keySet[N][0]</li>
     *   <li>keySet[0][1]</li>
     *   <li>keySet[1][1]</li>
     *   <li>...</li>
     *   <li>keySet[N][1]</li>
     *   <li>keySet[0][2]</li>
     *   <li>keySet[1][2]</li>
     *   <li>...</li>
     *   <li>keySet[N][2]</li>
     *   <li>...</li>
     * </ul>
     * where {@code keySet[0][0]} is the first item returned from the {@code Iterator} obtained from
     * {@code {@code keySets.get(0).iterator()}, {@code keySet[1][0]} is the first item obtained from
     * {@code {@code keySets.get(1).iterator()}, etc.

     *
     * @param keySets the {@code Set}s of keys to appear in the result {@code Set}
     *
     * @return a new, modifiable {@code Set} holding the designated keys
     */
    private static Set<String> fanIn(final List<Set<String>> keySets) {
        final Set<String> union = new LinkedHashSet<String>();

        /*
         * Collect the keys from the sets provided in the iteration order of the main
         * test entry map.
         */
        for (final String key : TEST_ENTRIES.keySet()) {
            for (final Set<String> keySet : keySets) {
                if (keySet.contains(key)) {
                    union.add(key);
                    break;
                }
            }
        }
        return union;
    }
}

Related Tutorials