In the given list, sets the element at the given index. - Java java.util

Java examples for java.util:List Element

Description

In the given list, sets the element at the given index.

Demo Code


//package com.book2s;
import java.util.ArrayList;
import java.util.Collection;

import java.util.HashSet;

import java.util.List;

import java.util.Set;

public class Main {
    /**//  w w w .ja v  a 2 s .c o  m
     * In the given list, sets the element at the given index. If the index is out of
     * the bounds of this list, it is extended up to this index
     * and the gaps are filled with the given fillElement. If the given list is null,
     * an {@link ArrayList} is created. The modified list is returned.
     */
    public static <T> List<T> setExtend(List<T> list, int index, T element,
            T fillElement) {
        if (list == null)
            list = new ArrayList<T>(index + 1);
        while (index >= list.size())
            list.add(fillElement);
        list.set(index, element);
        return list;
    }

    /**
     * Creates a new {@link Set} with the inferred type
     * using the given elements.
     */
    public static <T> Set<T> set(T... vals) {
        HashSet<T> ret = new HashSet<T>();
        for (T v : vals)
            ret.add(v);
        return ret;
    }

    /**
     * Creates a new {@link Set} with the inferred type
     * using the given elements.
     */
    public static <T> Set<T> set(Collection<T> vals) {
        return new HashSet<T>(vals);
    }
}

Related Tutorials