Java Collection Convert toggleEntry(T entry, Collection collection)

Here you can find the source of toggleEntry(T entry, Collection collection)

Description

Toggles an entry in a collection.

License

Open Source License

Parameter

Parameter Description
entry tested entry.
collection tested collection.

Return

If the entry was added.

Declaration

public static <T> boolean toggleEntry(T entry, Collection<T> collection) 

Method Source Code

//package com.java2s;
/*//w ww.  j  av  a 2  s .  c  om
 * This file is part of Bukkit Plugin Utilities.
 * 
 * Bukkit Plugin Utilities is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 * 
 * Bukkit Plugin Utilities is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with Bukkit Plugin Utilities.
 * If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Collection;

public class Main {
    /**
     * Toggles an entry in a collection. So if the entry is in the collection it
     * will be removed and if it isn't in the collection it will be added.
     * 
     * @param entry
     *            tested entry.
     * @param collection
     *            tested collection.
     * @return If the entry was added.
     * @since 1.3
     */
    public static <T> boolean toggleEntry(T entry, Collection<T> collection) {
        if (collection.remove(entry)) {
            return false;
        } else {
            collection.add(entry);
            return true;
        }
    }
}

Related

  1. toDelimitedString(Collection coll, String delim)
  2. toDelimitedString(final Collection coll, final String delim)
  3. toDoubles(Collection numbers)
  4. toEnumeration(final Collection collection)
  5. toEscapedStringWithDelimiters(Collection objects, String delim)
  6. toHashSet(Collection c)
  7. toHtml(Collection collection)
  8. toIntegerCollection(int[] ints)
  9. toJSONString(Collection collection)