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 8464121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:38:42+00:00 2026-06-10T14:38:42+00:00

while developing I was trying to return an empty List . public Collection<?> getElements()

  • 0

while developing I was trying to return an empty List.

public Collection<?> getElements() {
    // return elements
}

I searched for an easy way, my first idea was to create for example an ArrayList without any elements and return it. Like the following example:

public Collection<?> getElements() {
    return new ArrayList<?>();
}

For me it is too much overhead for an empty list.

  • 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-10T14:38:44+00:00Added an answer on June 10, 2026 at 2:38 pm

    There is a really simple solution for the above described “problem”:

    public Collection<?> getElements() {
        return Collections.EMPTY_LIST;
    }
    

    That returns an empty list.

    Notice:
    It returns an immutable object! You can use it only, if you need an object, which isn’t editable.

    Type-safety
    In the case you want to get a type-safe list you should use the following example [1]:

    List<String> s = Collections.emptyList();
    

    Three kinds of interfaces are supported:

    • List:

      List l = Collections.EMPTY_LIST;
      List<String> s = Collections.emptyList();
      
    • Map:

      Map m = Collections.EMPTY_MAP;
      Map<String> ms = Collections.emptyMap();
      
    • Set:

      Set s = Collections.EMPTY_SET;
      Set<String> ss = Collections.emptySet();
      

    Notice:

    Implementations of this method need not create a separate XXX object
    for each call. Using this method is likely to have comparable cost to
    using the like-named field. (Unlike this method, the field does not
    provide type safety.)

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

Sidebar

Related Questions

I have just found something very weird while developing a website. While trying to
While developing an application, I had the following problem. I wanted to return an
I'm trying to understand the Repository Pattern , while developing an ASP.NET MVC application
I am trying to achieve Model-view separation. My airplane is a class. While developing
I have been developing GWT application for a while and I'm trying to port
I've been experiencing very inconsistent results while developing an iPhone app and trying to
I have been researching around trying to find the best way to begin developing
I am trying to implement the Strategy design pattern using interfaces. However, while developing
While developing an application using gwt in ecliplse crashed. Now the server is running
While developing an application this is what i did : 1.Kept 320*480 as my

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.