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

  • SEARCH
  • Home
  • 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 8211019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:17:02+00:00 2026-06-07T10:17:02+00:00

How better to test and create a new list, if it does not exist?

  • 0

How better to test and create a new list, if it does not exist?

I only found this solution:

private List<List<Action>> actionList = Lists.newArrayList();

...

f(int index){
    Optional<List<Action>> optionalActionList = Optional.fromNullable(actionList.get(index));
    if (!optionalActionList.isPresent()) {
        actionList.add(new ArrayList<Action>());
    }
    actionList.get(index).add(index, new Action());
}

Maybe I should change the structure of storage? (The order of addition. Ability to add both the beginning and the end of the list. Are important in both lists).
Can use Google Guava if need.

  • 1 1 Answer
  • 0 Views
  • 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-06-07T10:17:03+00:00Added an answer on June 7, 2026 at 10:17 am

    It’s not clear what the Optional is really buying you here. I see the value of Optional as communicating between methods, or in storage.

    Additionally, I don’t think the code is doing what you think it is – you’re asking for the list at a particular index, but then if the value at that index is null, you’re adding a new list at the end. It sounds like you want:

    f(int index){
        List<Action> list = actionList.get(index);
        if (list == null) {
            list = new ArrayList<Action>();
            actionList.set(index, list);
        }
        // Note: no need to use index at all here... potentially
        list.add(new Action());
    }
    

    Note that I’m not using add(index, new Action()) at the end – at it was very unclear why you’d want to do so. You’ve already got to the right list using index – why would you use index again?

    The above assumes that the “outer” list is already the right size, but filled with nulls for “absent” lists. If that’s not the case, you’ll need to detect when index is beyond the bounds of the current outer list.

    Basically, if the above doesn’t help, please give more information as your question is currently unclear.

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

Sidebar

Related Questions

I have this test in my test base: public void WorksWithAreaUsers() { using (new
I have written python scripts to list repositories and commits. To create a new
Consider the following class: public class MyIntSet { private List<int> _list = new List<int>();
With DOMDocument, I can create one radio input like this: $html = new DOMDocument;
I know there is a better way to do this. I'm fairly new to
If anyone know where to download XAMLPad? Or you know better alternative to test
Is there a better way besides isset() or empty() to test for an empty
I want to test some QAbstractListModels I already implemented, is it better to use
any better way to write this ? $(this).parent().parent().find( dd ul).toggle(); update.. I am trying
Is better to use the below statement Convert.ToInt32(0 + stringValue) If not then why?

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.