Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6100637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:24:34+00:00 2026-05-23T13:24:34+00:00

I am searching for the shortest way (in code) to initialize list of strings

  • 0

I am searching for the shortest way (in code) to initialize list of strings and array of strings, i.e. list/array containing
“s1”, “s2”, “s3” string elements.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:24:35+00:00Added an answer on May 23, 2026 at 1:24 pm

    There are various options. Personally I like using Guava:

    List<String> strings = Lists.newArrayList("s1", "s2", "s3");
    

    (Guava’s a library worth having anyway, of course 🙂

    Using just the JDK, you could use:

    List<String> strings = Arrays.asList("s1", "s2", "s3");
    

    Note that this will return an ArrayList, but that’s not the normal java.util.ArrayList – it’s an internal one which is mutable but fixed-size.

    Personally I prefer the Guava version as it makes it clear what’s going on (the list implementation which will be returned). It’s also still clear what’s going on if you statically import the method:

    // import static com.google.common.collect.Lists.newArrayList;
    List<String> strings = newArrayList("s1", "s2", "s3");
    

    … whereas if you statically import asList it looks a little odder.

    Another Guava option, if you don’t want a modifiable-in-any-way list:

    ImmutableList<String> strings = ImmutableList.of("s1", "s2", "s3");
    

    I typically want to either have a completely mutable list (in which case Lists.newArrayList is best) or a completely immutable list (in which case ImmutableList.of is best). It’s rare that I really want a mutable-but-fixed-size list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Searching for a way to add this code, after the <head> (or some <link
searching around trying to find a way to add index on suffix of a
Could you recommend any java library which implements k-shortest algorithm -> searching for alternative
Searching for some sample code for converting a point in WGS84 coordinate system to
Searching for a string within a string is extremely well supported in .NET but
Searching on Google reveals x2 code snippets. The first result is to this code
After searching for a long time for a simple way of changing the text
I'm searching through a list of images and I want to find the best
Searching for a way to keep a registry of views and named adapters in
Searching for a way to extract Shell Thumbnails for a windows application, I ran

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.