All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.incava.ijdk.util.Lists Maven / Gradle / Ivy

There is a newer version: 3.9.0
Show newest version
package org.incava.ijdk.util;

import java.util.ArrayList;
import java.util.List;

public class Lists extends Collections {
    /**
     * Returns the first element in the list.
     *
     * @param  the element type
     * @param list the list
     * @return the first element in the list, or null if empty
     */
    public static  T first(List list) {
        return new org.incava.ijdk.collect.Array(list).first();
    }

    /**
     * Returns the last element in the list.
     *
     * @param  the element type
     * @param list the list
     * @return the first element in the list, or null if empty
     */
    public static  T last(List list) {
        return new org.incava.ijdk.collect.Array(list).last();
    }

    /**
     * Returns the nth element in the list, where n can be negative, to index
     * from the end of the list. Returns null if out of bounds.
     *
     * @param  the element type
     * @param list the list
     * @param index the index, zero-indexed
     * @return the element at the given index, or null if not in bounds
     */
    public static  T get(List list, int index) {
        return new org.incava.ijdk.collect.Array(list).get(index);
    }

    /**
     * Removes all occurrances of element from list, returning whether any
     * were found. Returns false if list is null.
     *
     * @param  the element type
     * @param list the list to modify
     * @param element the element to seek
     * @return whether any matching elements were found
     */
    public static  boolean removeAll(List list, T element) {
        if (list != null && list.remove(element)) {
            while (list.remove(element)) {
                // nothing
            }
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * Returns a random element from the list, or null if the list is null or empty.
     *
     * @param  the element type
     * @param list the list to seek
     * @return a random element, or null if the list is empty
     */
    public static  Type getRandomElement(List list) {
        return new org.incava.ijdk.collect.Array(list).getRandomElement();
    }

    /**
     * Returns a list (an ArrayList) initialized with the given elements. The list returned is of
     * dynamic size (unlike Arrays.asList(...), which returns a fixed-size array). The
     * following two blocks are equivalent:
     *
     * 
     *     List<String> names = new ArrayList<String>(Arrays.asList("kevin", "jacob", "isaac"));
     *     names.add("henry");
     * 
*
     *     List<String> names = list("kevin", "jacob", "isaac");
     *     names.add("henry");
     * 
* * @param the element type * @param elements the elements * @return the new List */ @SafeVarargs public static List create(T ... elements) { List ary = new ArrayList(); for (T element : elements) { ary.add(element); } return ary; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy