Languages

Java create one element List in one line - how to do it?

1 points
Asked by:
Root-ssh
6665

How can I create one element List of Strings in 1 line?

List<String> list = new ArrayList<>();
list.add("1");

Is is possible to do it in 1 line?

When I have method which would return this array, it'd take 3 lines:

public List<String> createList() {
    List<String> list = new ArrayList<>();
    list.add("1");
    return list;
}

It'd be great if it could be done in 1 line instead of 3.

0 answers
Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.