Adding elements to a set on creation

Since Java 7, to instantiate a single-element, immutable Set, you can use:

Collections.singleton(thing);

Returns an immutable set containing only the specified object. The returned set is serializable.

— Javadoc reference: Collections.singleton(T)


In Java 8 you can instantiate a Set containing any number of your objects with the following, which is an adaptation of this answer:

Stream.of(thing, thingToo).collect(Collectors.toSet());

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)