Java List from Array asList(T... elements)

Here you can find the source of asList(T... elements)

Description

as List

License

Open Source License

Declaration

@SafeVarargs
    public static <T> List<T> asList(T... elements) 

Method Source Code

//package com.java2s;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import java.util.List;

public class Main {
    public static <T> List<T> asList(T e0, T e1) {
        ArrayList<T> result = new ArrayList<>(2);
        result.add(e0);/*from ww  w .j  a  va 2s.c om*/
        result.add(e1);
        return result;
    }

    public static <T> List<T> asList(T e0, T e1, T e2) {
        ArrayList<T> result = new ArrayList<>(3);
        result.add(e0);
        result.add(e1);
        result.add(e2);
        return result;
    }

    @SafeVarargs
    public static <T> List<T> asList(T... elements) {
        ArrayList<T> result = new ArrayList<>(elements.length);
        Collections.addAll(result, elements);
        return result;
    }

    public static <T> boolean addAll(Collection<T> collection, Collection<? extends T> toAdd) {
        int size = toAdd.size();
        boolean result = false;
        if (size > 0) {
            if (size < 10)
                for (T element : toAdd)
                    result |= collection.add(element);
            else
                result = collection.addAll(toAdd);
        }
        return result;
    }

    @SafeVarargs
    public static <T> boolean addAll(Collection<T> collection, T... toAdd) {
        int size = toAdd.length;
        boolean result = false;
        if (size > 0) {
            for (T element : toAdd)
                result |= collection.add(element);
        }
        return result;
    }
}

Related

  1. asList(T... args)
  2. asList(T... array)
  3. asList(T... array)
  4. asList(T... array)
  5. asList(T... data)
  6. asList(T... items)
  7. asList(T... values)
  8. asList(T... values)
  9. asList(T... values)