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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:54:27+00:00 2026-05-16T10:54:27+00:00

This works to return a list of ints: public List<Integer> GetIListImpl() { return new

  • 0

This works to return a list of ints:

public List<Integer> GetIListImpl() {
    return new ArrayList<Integer>();
}

But what if I want to let the caller specify the generic type? Something like this, although syntactically I’m not sure how to do it:

public List<T> GetIListImpl<T>() {
    return new ArrayList<T>();
}

The usage would be:

    List<String> = GetIListImpl<String>();
  • 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-16T10:54:27+00:00Added an answer on May 16, 2026 at 10:54 am

    On generic static factory methods for parameterized types

    It looks like you want to write convenient factory methods to instantiate generic collections.

    You can write generic methods like these:

    public static <T> List<T> newArrayList() {
        return new ArrayList<T>();
    }
    public static <K,V> Map<K,V> newHashMap() {
        return new HashMap<K,V>();
    }
    

    Then you can simply write:

    // absolutely type-safe!!! no compilation warnings at all!!!
    
    List<String> names = newArrayList();
    
    List<Integer> nums = newArrayList();
    
    Map<String, List<String>> map = newHashMap();
    

    Note that in some contexts, the above methods do not have to be static, and you may opt to leave out the implementation class names out of the methods and only use the interface names (e.g. newList, newMap).


    An endorsement from Effective Java 2nd Edition

    This kind of generic type-inferring static factory method is actually endorsed by Effective Java 2nd Edition; it had the unique privilege of being the very first item discussed in the book.

    Here are the relevant quotes from Item 1: Consider static factory methods instead of constructors:

    A fourth advantage of static factory methods is that they reduce the verbosity of creating parameterized type instances.

    When you invoke the constructor of a parameterized class, unfortunately you must specify the type parameters even if they’re obvious from context. This typically requires you to provide the type parameters twice in quick succession:

        Map<String,List<String>> m = 
            new HashMap<String,List<String>>();
    

    This redundant specification quickly becomes painful as the length and complexity of the type parameters increase. With static factories, however, the compiler can figure out the type parameters for you. This is known as type inference. For example, suppose that HashMap provided this static factory:

        public static <K,V> HashMap<K,V> newInstance() {
            return new HashMap<K,V>();
        }
    

    Then you could replace the wordy declaration above with this succinct alternative:

        Map<String,List<String>> m = HashMap.newInstance();
    

    Unfortunately the standard collection implementations such as HashMap do not have static factory methods as of release 1.6, but you can put these methods in your own utility class. More importantly you can provide such static factories in your own parameterized classes.

    The item also prescribes the common naming convention for these static factory methods:

    • getInstance – returns an instance that is described by the parameters […]
    • newInstance – Like getInstance, except it guarantees that each instance returned is distinct from all others.
    • newType – Like newInstance, but used when the factory method is in a different class. Type indicates the type of object returned by the factory method.

    On explicit type parameters

    You do not have to explicitly provide the type parameters in most cases, since the Java generics type inference system can usually figure out what you need.

    Nevertheless, to provide explicit type parameters, the syntax is to put it before the method name (not after). Here’s an example of invoking with explicit parameter the generic method <T> List<T> emptyList() from java.util.Collections:

    Collections.<String>emptyList();
    // Collections.emptyList<String>(); // DOES NOT COMPILE
    

    Note that a syntax quirk of the explicit type parameterization for generic method invocation is that you must qualify the type (if static) or the object that you’re invoking the method on, even if they can be omitted if it was not an explicit parameterization.


    References

    • Angelika Langer’s Java Generics FAQS
      • What is a generic method?
      • What is type argument inference?
      • What is explicit type argument specification?
    • JLS 8.4.4 Generic Methods

    Appendix: Collection factory methods from Guava

    It should be noted that Guava in fact already provides the static factory methods for the types in Java Collections Framework:

    From the main package com.google.common.collect:

    • Lists.newArrayList(), newLinkedList(), …
    • Sets.newHashSet(), newTreeSet(), newEnumSet(…), …
    • Maps.newHashMap(), newTreeMap(), newEnumMap(…), …

    In fact, in the spirit of Effective Java 2nd Edition recommendation, Guava’s own collections do not provide public constructors, but instead provide static create() factory methods:

    • HashMultiSet.create(), a Multiset implementation
    • TreeMultimap.create(), a Multimap implementation

    The rest of the library also provides many highly useful functionality.

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

Sidebar

Ask A Question

Stats

  • Questions 502k
  • Answers 502k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer A little google search could have helped: http://www.google.com/search?q=sqlite+concat In SQLite… May 16, 2026 at 2:33 pm
  • Editorial Team
    Editorial Team added an answer Your code hangs as grep's output may be less than… May 16, 2026 at 2:33 pm
  • Editorial Team
    Editorial Team added an answer I think the hibernateProperties are getting set incorrectly (unless Spring… May 16, 2026 at 2:33 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>
I'm trying to write a method like this: public static T Test<T>() { if
I have a list which stores a lost of integers. I don't like the
I have been using BinaryFormatter to serialise data to disk but it doesn't seem
I have a float[] and i would like to get a list with the
n00b problem- I am trying to have a list show the most recent entry
I am running WSS3.0 and have a custom list which contains versioning on a
I have an AJAX callback that returns a list of results that are written
I've got a JTable which model is custom that extends AbstractTableModel. In this the
I am learning Scala and I have a Java project to migrate to Scala.

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.