Java Collection Remove remove(final Collection c, final Object elem)

Here you can find the source of remove(final Collection c, final Object elem)

Description

remove

License

Open Source License

Declaration

public final static boolean remove(final Collection<?> c, final Object elem) 

Method Source Code

//package com.java2s;
/**//from   w  w  w .j a  v  a2s  . c o m
 * The contents of this file are subject to the Regenstrief Public License
 * Version 1.0 (the "License"); you may not use this file except in compliance with the License.
 * Please contact Regenstrief Institute if you would like to obtain a copy of the license.
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) Regenstrief Institute.  All Rights Reserved.
 */

import java.util.*;

public class Main {
    /**
     * Removes all instances of the given character from the given String
     * 
     * @param s the String
     * @param toRemove the character to remove
     * @return the String
     **/
    public final static String remove(final String s, final char toRemove) {
        final int len = length(s);

        if (len == 0) {
            return null;
        }

        StringBuilder sb = null;
        char[] chars = null;
        int start = 0;
        for (int i = 0; i < len; i++) {
            final char c = s.charAt(i);
            if (c == toRemove) {
                if (chars == null) {
                    sb = new StringBuilder(len);
                    chars = s.toCharArray();
                }
                sb.append(chars, start, i - start);
                start = i + 1;
            }
        }

        if (sb == null) {
            return s;
        } else if (start < len) {
            sb.append(chars, start, len - start);
        }

        return sb.toString();
    }

    public final static boolean remove(final Collection<?> c, final Object elem) {
        return (c != null) && c.remove(elem);
    }

    /**
     * Removes the given key from the given Map (if the Map is not null)
     * 
     * @param map the Map
     * @param key the key
     * @return the removed value
     */
    public final static <V> V remove(final Map<?, V> map, final Object key) {
        return map == null ? null : map.remove(key);
    }

    /**
     * Returns the length of the given array
     * 
     * @param array the array for which to return the length
     * @return the length
     **/
    public final static int length(final Object array) {
        if (array == null) {
            return 0;
        } else if (array instanceof byte[]) {
            return ((byte[]) array).length;
        } else if (array instanceof short[]) {
            return ((short[]) array).length;
        } else if (array instanceof int[]) {
            return ((int[]) array).length;
        } else if (array instanceof long[]) {
            return ((long[]) array).length;
        } else if (array instanceof float[]) {
            return ((float[]) array).length;
        } else if (array instanceof double[]) {
            return ((double[]) array).length;
        } else if (array instanceof boolean[]) {
            return ((boolean[]) array).length;
        } else if (array instanceof char[]) {
            return ((char[]) array).length;
        }
        return ((Object[]) array).length;
    }

    /**
     * Returns the length of the given array
     * 
     * @param array the array for which to return the length
     * @return the length
     **/
    public final static int length(final Object[] array) {
        return array == null ? 0 : array.length;
    }

    /**
     * Returns the length of the given array
     * 
     * @param array the array for which to return the length
     * @return the length
     **/
    public final static int length(final byte[] array) {
        return array == null ? 0 : array.length;
    }

    /**
     * Returns the length of the given array
     * 
     * @param array the array for which to return the length
     * @return the length
     **/
    public final static int length(final int[] array) {
        return array == null ? 0 : array.length;
    }

    /**
     * Retrieves the length of the CharSequence
     * 
     * @param s the CharSequence
     * @return the length
     **/
    public final static int length(final CharSequence s) {
        return s == null ? 0 : s.length();
    }

    /**
     * Appends the given character to the given StringBuffer, allocating the StringBuffer if
     * necessary
     * 
     * @param sb the StringBuffer
     * @param c the char
     * @return the StringBuffer
     **/
    public final static StringBuffer append(final StringBuffer sb, final char c) {
        return (sb == null ? new StringBuffer() : sb).append(c);
    }

    /**
     * Appends the given String to the given StringBuffer, allocating the StringBuffer if necessary
     * 
     * @param sb the StringBuffer
     * @param s the String
     * @return the StringBuffer
     **/
    public final static StringBuffer append(final StringBuffer sb, final String s) {
        return s == null ? sb : (sb == null ? new StringBuffer() : sb).append(s);
    }

    /**
     * Appends part of the given CharSequence to the given StringBuffer, allocating the StringBuffer
     * if necessary
     * 
     * @param sb the StringBuffer
     * @param s the String
     * @param offset the offset of the CharSequence at which to start copying
     * @param len the number of characters to copy
     * @return the StringBuffer
     **/
    public final static StringBuffer append(StringBuffer sb, final CharSequence s, int offset, int len) {
        if (sb == null) {
            sb = new StringBuffer(len);
        }
        sb.ensureCapacity(sb.length() + len);
        for (len += offset; offset < len; offset++) {
            sb.append(s.charAt(offset));
        }

        return sb;
    }

    /**
     * Appends the given character to the given StringBuilder, allocating the StringBuilder if
     * necessary
     * 
     * @param sb the StringBuilder
     * @param c the char
     * @return the StringBuilder
     **/
    public final static StringBuilder append(final StringBuilder sb, final char c) {
        return ((sb == null) ? new StringBuilder() : sb).append(c);
    }

    /**
     * Appends the given String to the given StringBuilder, allocating the StringBuilder if necessary
     * 
     * @param sb the StringBuilder
     * @param s the String
     * @return the StringBuilder
     **/
    public final static StringBuilder append(final StringBuilder sb, final String s) {
        return (s == null) ? sb : ((sb == null) ? new StringBuilder() : sb).append(s);
    }

    /**
     * Converts an object to a string
     * 
     * @param o the Object to convert
     * @return the String
     **/
    public final static String toString(final Object o) {
        return o == null ? null : o.toString();
    }

    public final static <E> void ensureCapacity(final Collection<E> col, final int minCapacity) {
        if (col instanceof ArrayList) {
            ((ArrayList<E>) col).ensureCapacity(minCapacity);
        }
    }
}

Related

  1. remove(Collection collection, Object object)
  2. remove(Collection p_collection, int p_index, int p_numberOfObjects)
  3. remove(Collection col, T value)
  4. remove(Collection collection, final int count)
  5. remove(Collection collection, T... items)
  6. remove(final int index, final Collection collection)
  7. removeAll(Collection collection, Collection remove)
  8. removeAll(Collection source, Set remove)
  9. removeAll(Collection c, Iterable elts)