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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:24:11+00:00 2026-05-25T22:24:11+00:00

I have the following method: public static List<List<String>> createObject() { List<List<String>> listOfListOfStrings = new

  • 0

I have the following method:

public static List<List<String>> createObject() {
    List<List<String>> listOfListOfStrings = new LinkedList<List<String>>();
    List<String> listOfStrings = new LinkedList<String>();
    //do some populating on the lists here
    listOfListOfStrings.add(listOfStrings);
    return listOfListOfStrings;
}

Now I want to be able to use an ArrayList/Vector/Stack instead of the LinkedList (and do different combinations if needed. I read some posts on generics regarding this issue and I found out that using a factory pattern for creating those is most suited (since I don’t want reflection, and since using the generic <T extends List<K>, K extends List<String>> would not work.). So the solution I came up with is the following:

public class Tester {
public static void main(String[] args){
    checkGenericsOfLists();
}

public static void checkGenericsOfLists(){
    List<List<String>> listOfListOfStrings = createObject(new LinkedListFactory<List<String>>(), new LinkedListFactory<String>());
    print(listOfListOfStrings);
    translate(listOfListOfStrings);
    print(listOfListOfStrings);
}

public static List<List<String>> createObject(ListFactorable mainListFactory, ListFactorable subListsFactory) {
    List<List<String>> listOfListOfStrings = mainListFactory.create();//new LinkedList<List<String>>();
    List<String> listOfStrings = subListsFactory.create();//new LinkedList<String>();
    listOfStrings.add("A");
    listOfListOfStrings.add(listOfStrings);
    return listOfListOfStrings;
}

public static void transform(List<List<String>> listOfListOfStrings){ 
            //do some abuse on the lists here.
}}

This solution gives warnings:

ListFactorable is a raw type. References to generic type ListFactorable should be parameterized

This is for the createObject method signature. And:

Type safety: The expression of type List needs unchecked conversion to conform to List of List of String>>

This is for the lines where the factory invokes the create() method.

But if I use this one:

public static List<List<String>> createObject(ListFactorable<List<List<String>>, List<String>> mainListFactory, ListFactorable<List<String>, String> subListsFactory) {
    List<List<String>> listOfListOfStrings = mainListFactory.create();//new LinkedList<List<String>>();
    List<String> list = subListsFactory.create();//new LinkedList<String>();
    list.add("A");
    listOfListOfStrings.add(list);
    return listOfListOfStrings;
}

I get compiler error:

The method createObject(ListFactorable<List<List<String>>,List<String>>, ListFactorable<List<String>,String>) in the type Tester is not applicable for the arguments (LinkedListFactory<List<String>>, LinkedListFactory<String>)

Is there any way I can make the compiler not throw warning or error and have those lists instantiated without the createObject method being aware of the List implementation used (during compile time)?!

Cheers,
Despot

EDIT:
Excuse me for not posting the rest of the classes (very silly of me:)). Here we go:

public interface ListFactorable<T extends List<K>, K> {
T create();}

public class LinkedListFactory<K> implements ListFactorable<LinkedList<K>, K> {
public LinkedList<K> create(){
    return new LinkedList<K>();
}}

EDIT2 (Eugene take on issue):

public interface EugeneListFactorable<T extends List<?>> {T create();}

public class EugeneLinkedListFactory implements EugeneListFactorable<LinkedList<?>> {
    public LinkedList<?> create(){
        return new LinkedList<List<?>>();
    }
}
public static void checkGenericsOfLists2(){
    List<List<String>> listOfListOfStrings = createObject(new EugeneLinkedListFactory(), new EugeneLinkedListFactory());
    translate(listOfListOfStrings);
}

Compiler error:

    - Type mismatch: cannot convert from LinkedList<?> to List<List<String>>
- Bound mismatch: The generic method createObject(EugeneListFactorable<N>, EugeneListFactorable<M>) of type 
 Tester is not applicable for the arguments (EugeneLinkedListFactory, EugeneLinkedListFactory). The inferred type LinkedList<?> 
 is not a valid substitute for the bounded parameter <N extends List<List<String>>>

Please do try to compile and run the example I posted and your solution. Its a test class which you can easily run on ur IDE. Thanks!

  • 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-05-25T22:24:12+00:00Added an answer on May 25, 2026 at 10:24 pm

    The followin stuff “works on my machine” without warnings or errors. What is beyond my perception is the reason for all the fuss – see the seconds codeblock.

    public class Tester {
    
        public static void main(String[] args) {
            checkGenericsOfLists();
        }
    
        public static void checkGenericsOfLists() {
            List<List<String>> listOfListOfStrings = createObject(
                    new LinkedListFactory<List<String>>(),
                    new LinkedListFactory<String>());
            transform(listOfListOfStrings);
        }
    
        public static List<List<String>> createObject(
                ListFactorable<List<List<String>>, List<String>> mainListFactory, 
                ListFactorable<List<String>, String> subListsFactory
        ) 
        {
            List<List<String>> listOfListOfStrings = mainListFactory.create();
            List<String> listOfStrings = subListsFactory.create();
            listOfStrings.add("A");
            listOfListOfStrings.add(listOfStrings);
            return listOfListOfStrings;
        }
    
        public static void transform(List<List<String>> listOfListOfStrings) {
            // do some abuse on the lists here.
            System.out.println(listOfListOfStrings);
        }
    
        public interface ListFactorable<T extends List<K>, K> {
            T create();
        }
    
        static public class LinkedListFactory<K> implements ListFactorable<List<K>, K> {
            public LinkedList<K> create() {
                return new LinkedList<K>();
            }
        }
    }
    

    This solution is a little bit cleaner leaving out some generics noise.

    public class Tester2 {
        public static void main(String[] args) {
            checkGenericsOfLists();
        }
    
        public static void checkGenericsOfLists() {
            List<List<String>> listOfListOfStrings = createObject(
                    new LinkedListFactory<List<String>>(),
                    new LinkedListFactory<String>());
            transform(listOfListOfStrings);
        }
    
        public static List<List<String>> createObject(
                ListFactory<List<String>> mainListFactory, 
                ListFactory<String> subListsFactory
        ) 
        {
            List<List<String>> listOfListOfStrings = mainListFactory.create();
            List<String> listOfStrings = subListsFactory.create();
            listOfStrings.add("A");
            listOfListOfStrings.add(listOfStrings);
            return listOfListOfStrings;
        }
    
        public static void transform(List<List<String>> listOfListOfStrings) {
            // do some abuse on the lists here.
            System.out.println(listOfListOfStrings);
        }
    
        public interface ListFactory<T> {
            List<T> create();
        }
    
        static public class LinkedListFactory<T> implements ListFactory<T> {
            public List<T> create() {
                return new LinkedList<T>();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following method: public static string UlList(this HtmlHelper helper, List<IEntity> entities, string
I have the following method public static void SerializeToXMLFile(Object obj,Type type, string fileName) {
I have the following class: public abstract class AbstractParent { static String method() {
I have the following method : public List<string> someMethod() { // populate list of
I have the following code: public static long CreateVendorsEmailJob(List<Recipient> recipients, string subject, string body)
I have the following method in an App_Code/Globals.cs file: public static XmlDataSource getXmlSourceFromOrgid(int orgid)
I have the following method: public object[] GetEventsByUser(DateTime start, DateTime end, string fullUrl) The
I have the following method: public void PutFile(string ID, Stream content) { try {
I have the following method and interface: public object ProcessRules(List<IRule> rules) { foreach(IRule rule
I have the following extension method public static class ListExtensions { public static IEnumerable<T>

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.