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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:10:43+00:00 2026-06-03T06:10:43+00:00

This is jdk1.7.0_04 . I was attempting to use Collections.emptyList() rather than new ing

  • 0

This is jdk1.7.0_04.

I was attempting to use Collections.emptyList() rather than newing up my own empty list in a conditional:

List<String> list = (anArray != null) ? Arrays.asList(anArray) : Collections.emptyList();

but get the following error:

error: incompatible types
        List<String> list = (anArray != null) ? Arrays.asList(anArray) : Collections.emptyList();
                                              ^
  required: List<String>
  found:    List<CAP#1>
  where CAP#1 is a fresh type-variable:
    CAP#1 extends Object from capture of ? extends Object
1 error

I was able to figure that I needed to change things to:

List<String> list = (anArray != null) ? Arrays.asList(anArray) : Collections.<String>emptyList();

But as part of working on this I encountered the bizarre (to me, anyways) situation that:

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

compiles fine, but:

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

gives the following compile error:

error: inconvertible types
        List<String> alwaysEmpty = (List<String>) Collections.emptyList();
                                                                       ^
  required: List<String>
  found:    List<Object>

What the heck??

Now I can understand that perhaps for some strange reason the use of the conditional operator is somehow blocking the type inference system from realizing that the type parameter for the emptyList() call should be String and so it needs to be explicitly specified. But why does inserting a (admittedly redundant) cast mess things up?

  • 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-03T06:10:44+00:00Added an answer on June 3, 2026 at 6:10 am

    But why does inserting a (admittedly redundant) cast mess things up?

    Because now the expression Collections.emptyList() in itself isn’t the target of any assignment – so what type argument should be chosen? It’s better just to specify the type argument:

    // Redundant here, but just as an example
    List<String> alwaysEmpty = Collections.<String>emptyList();
    

    It works the same for the conditional operator, too:

    public static void main(String[] args) {              
        List<String> list = (args != null)
            ? Arrays.asList(args) : Collections.<String>emptyList();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my path:C:\ANDROID\Java\jdk1.6.0\bin>keytool.exe -list -alias androiddebugkey -keystoreC:\ANDROID\debugkey\debug.keystore-storepass android -keypass android keytool error: java.lang.RuntimeException:
This is my sample code: SPWeb web = SPContext.Current.Web SPList list = web.Lists[TestList]; try
I did this before: CLASSPATH=.:/home/phoenies/jdk1.6.0_17/lib/tools.jar:/home/phoenies/jdk1.6.0_17/lib/dt.jar But today an article says I should do this:
I wrote a simple Linux Script this way export JAVA_HOME=/usr/local/jdk1.6.0_20 export PATH=/usr/local/jdk1.6.0_20/bin LIB_DIR=/home/praveen/lib export
I have applet which I run in command line like this: C:\Program Files (x86)\Java\jdk1.6.0_21\bin\appletviewer.exe
What puzzles me is this. Java doc of HashEntry in ConcurrentHashMap (jdk1.6.0_16) ...Because the
I created simple Java Servlet: WelcomeServlet.java. Than, I tried compile this file via: javac
I'm new to using anything other than Eclipse for compiling Java projects. I am
I'm using intelliJ IDEA version 9.0.1 build #IU 93.94 with JDK 1.6.0_17. This error
this is my first question in here, and I would like to ask if

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.