Utility method for wrapping an object in a collection

前端 未结 5 669
抹茶落季
抹茶落季 2021-02-06 22:44

I\'m looking for a static method in the Java core libraries or some other commonly used dependency — preferably one of Apache — that does the following:

public s         


        
5条回答
  •  -上瘾入骨i
    2021-02-06 22:49

    java.util.Collections.singleton(object) will give you an immutable Set. singletonList is also available.

    Less efficiently java.util.Arrays.asList(object) will give you a mutable (can use list.set(0, x);), but non-structurally changeable (can't add or remove) List. It is a bit more expensive as there is an extra array that is created client-side.

提交回复
热议问题