Copies entries from the Set provided until reaching a barrier item. - Java Collection Framework

Java examples for Collection Framework:Set

Description

Copies entries from the Set provided until reaching a barrier item.

Demo Code

/*//from  ww w .  jav  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.LinkedHashSet;

import java.util.Set;

public class Main {
    /**
     * Copies entries from the {@code Set} provided until reaching a barrier item.
     * The items are copied in iterator order and the result does not include the barrier.
     *
     * @param source the {@code Set} from which the items are copied
     * @param barrier the barrier indicating the end of the copy
     *
     * @return a new, entry-ordered {@code Set} containing the subset of {@code source}
     */
    static Set<String> copyUntil(final Set<String> source,
            final String barrier) {
        final Set<String> subset = new LinkedHashSet<String>();
        for (final String sourceItem : source) {
            if (barrier.equals(sourceItem)) {
                break;
            }
            subset.add(sourceItem);
        }
        return subset;
    }
}

Related Tutorials