Java Collection Subtract subCollection(final Collection collection, final int offset, final int limit)

Here you can find the source of subCollection(final Collection collection, final int offset, final int limit)

Description

sub Collection

License

Apache License

Declaration

@SuppressWarnings("unchecked")
    public static <T> Collection<T> subCollection(final Collection<T> collection, final int offset,
            final int limit) 

Method Source Code


//package com.java2s;
/*/*w  w w  .  j  a  va2 s .  c o  m*/
 * Copyright 2002-2009 the original author or authors.
 * 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.Map;

public class Main {
    @SuppressWarnings("unchecked")
    public static <T> Collection<T> subCollection(final Collection<T> collection, final int offset,
            final int limit) {
        if (collection == null || collection.isEmpty()) {
            return collection;
        }

        int start = offset;
        if (start > collection.size()) {
            start = collection.size();
        }

        int end = start + limit;
        if (end > collection.size()) {
            end = collection.size();
        }
        final Collection<T> returnCollection = new ArrayList<>();
        final T[] srcArray = (T[]) collection.toArray();

        for (int i = start; i < end; i++) {
            returnCollection.add(srcArray[i]);
        }
        return returnCollection;
    }

    public static boolean isEmpty(final Collection<?> collection) {
        return collection == null || collection.isEmpty();
    }

    public static boolean isEmpty(final Map<?, ?> map) {
        return map == null || map.isEmpty();
    }
}

Related

  1. sub(Collection s, T item)
  2. subCollection(Collection col, int amount)
  3. subMap(Map map, Collection keys)
  4. substractBackground( final Map> concentrationToSampleReadings, final Map> concentrationToBackgroundReadings)
  5. subtract(Collection a, Collection b)
  6. subtract(Collection c1, Collection c2)