Java Collection Subset sub(Collection list, int start, int end)

Here you can find the source of sub(Collection list, int start, int end)

Description

sub

License

Apache License

Declaration

public static <T> List<T> sub(Collection<T> list, int start, int end) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {

    public static <T> List<T> sub(List<T> list, int start, int end) {
        if (list == null || list.isEmpty()) {
            return null;
        }//  ww w .  j a  v  a  2  s  .  c  o m

        if (start < 0) {
            start = 0;
        }
        if (end < 0) {
            end = 0;
        }

        if (start > end) {
            int tmp = start;
            start = end;
            end = tmp;
        }

        final int size = list.size();
        if (end > size) {
            if (start >= size) {
                return null;
            }
            end = size;
        }

        return list.subList(start, end);
    }

    public static <T> List<T> sub(Collection<T> list, int start, int end) {
        if (list == null || list.isEmpty()) {
            return null;
        }

        return sub(new ArrayList<T>(list), start, end);
    }

    public static <T> boolean isEmpty(T[] array) {
        return array == null || array.length == 0;
    }

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

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

Related

  1. isSubset(Collection subset, Collection of)
  2. isSubset(Collection subSet, Collection superSet)
  3. isSubset(Collection s1, Collection s2)
  4. isSubset(Collection subset, Collection superset)
  5. isSubset(final Collection l1, final Collection l2)