Use of Java's Collections.singletonList()?

前端 未结 6 863
無奈伤痛
無奈伤痛 2021-01-29 18:58

What is the use of Collections.singletonList() in Java? I understand that it returns a list with one element. Why would I want to have a separate method to do that?

6条回答
  •  隐瞒了意图╮
    2021-01-29 19:35

    Here's one view on the singleton methods:

    I have found these various "singleton" methods to be useful for passing a single value to an API that requires a collection of that value. Of course, this works best when the code processing the passed-in value does not need to add to the collection.

提交回复
热议问题