Java9 Collections工厂函数特性说明


JEP 269: Convenience Factory Methods for Collections

Makes it easier to create instances of collections and maps with small numbers of elements. New static factory methods on the List, Set, and Map interfaces make it simpler to create immutable instances of those collections.

For example: Set alphabet = Set.of("a", "b", "c");

See Creating Immutable Lists, Sets, and Maps in Java Platform, Standard Edition Java Core Libraries Developer’s Guide. For API documentation, see Immutable Set Static Factory Methods, Immutable Map Static Factory Methods, and Immutable List Static Factory Methods.


Set a = Set.of(1, 2, 3);
List b = List.of(1, 1, 2);
Map c = Map.of(1, 1, 2, 2, 3, 3);


static <E> Set<E> of(E e1, E e2, E e3) {
    return new ImmutableCollections.SetN<>(e1, e2, e3);


abstract static class AbstractImmutableSet<E> extends AbstractSet<E> implements Serializable {
    @Override public boolean add(E e) { throw uoe(); }
    @Override public boolean addAll(Collection<? extends E> c) { throw uoe(); }
    @Override public void    clear() { throw uoe(); }
    @Override public boolean remove(Object o) { throw uoe(); }
    @Override public boolean removeAll(Collection<?> c) { throw uoe(); }
    @Override public boolean removeIf(Predicate<? super E> filter) { throw uoe(); }
    @Override public boolean retainAll(Collection<?> c) { throw uoe(); }


static <E> Set<E> of()
static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) 
static <E> Set<E> of(E... elements) 



Exercise care when using the varargs facility in performance-critical situations. Every invocation of a varargs method causes an array allocation and initialization. If you have determined empirically that you can’t afford this cost but you need the flexibility of varargs, there is a pattern that lets you have your cake and eat it too. Suppose you’ve determined that 95 percent of the calls to a method have three or fewer parameters. Then declare five overloadings of the method, one each with zero through three ordinary parameters, and a single varargs method for use when the number of arguments exceeds three。


public static void main(String[] args) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) {
            appendStr("a", "b");
        long end = System.currentTimeMillis();
        System.out.println("Fixed cost time: " + (end - start));
        long vStart = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) {
            appendStrVargs("a", "b");
        long vend = System.currentTimeMillis();
        System.out.println("Vargs cost time: " + (vend - vStart));

    private static List<String> appendStr(String a, String b) {
        return ofString(a, b);
    private static List<String> appendStrVargs(String... args) {
        return ofString(args);
    private static List<String> ofString(String... strs) {
        List<String> result = new ArrayList();
        for (String s : strs) {
        return result;


Fixed cost time: 333  
Vargs cost time: 720


Thanks a lot.