CollectionUtil.java revision 645501c2ab19a559ce82a1d5a29ced159a4c30fb
1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package org.chromium.base;
6
7import java.util.ArrayList;
8import java.util.Collections;
9import java.util.HashSet;
10
11/**
12 * Functions used for easier initialization of Java collections. Inspired by
13 * functionality in com.google.common.collect in Guava but cherry-picked to
14 * bare-minimum functionality to avoid bloat. (http://crbug.com/272790 provides
15 * further details)
16 */
17public final class CollectionUtil {
18    private CollectionUtil() {}
19
20    @SafeVarargs
21    public static <E> HashSet<E> newHashSet(E... elements) {
22        HashSet<E> set = new HashSet<E>(elements.length);
23        Collections.addAll(set, elements);
24        return set;
25    }
26
27    @SafeVarargs
28    public static <E> ArrayList<E> newArrayList(E... elements) {
29        ArrayList<E> list = new ArrayList<E>(elements.length);
30        Collections.addAll(list, elements);
31        return list;
32    }
33
34    @VisibleForTesting
35    public static <E> ArrayList<E> newArrayList(Iterable<E> iterable) {
36        ArrayList<E> list = new ArrayList<E>();
37        for (E element : iterable) {
38            list.add(element);
39        }
40        return list;
41    }
42}