How to Efficiently Convert List<SubClass> to List<BaseClass> in Java?

Question

What is the most efficient way to convert a List<SubClass> to List<BaseClass> in Java without creating a new list?

List<BaseClass> convertedList = new ArrayList<BaseClass>(listOfSubClass);

Answer

In Java, casting a List of subclass instances to a List of base class references is not straightforward due to type safety. This article explains why and offers techniques to minimize overhead while maintaining references to original objects.

// Optimal method using bounded wildcard
public void processList(List<? extends BaseClass> list) {
    // Process the list, can reference as BaseClass
    for (BaseClass obj : list) {
        // Do something with obj
    }
}

Causes

  • Java's type system prohibits direct casting of collections due to type safety.
  • Generics in Java are invariant, meaning List<SubClass> cannot be treated as List<BaseClass> even though SubClass is a subclass of BaseClass.

Solutions

  • Use a parameterized method that accepts List<SubClass> and processes it as List<BaseClass>.
  • Leverage bounded wildcards such as List<? extends BaseClass> to refer to List<SubClass> as a list of BaseClass without needing to cast.

Common Mistakes

Mistake: Attempting to directly cast List<SubClass> to List<BaseClass> causes compile-time errors.

Solution: Replace direct casting with a wildcard approach, using List<? extends BaseClass>.

Mistake: Creating a new list and copying objects unnecessarily involves performance overhead.

Solution: Utilize bounded wildcards or generics in method parameters to operate on the original list.

Helpers

  • convert List to BaseClass
  • Java List casting
  • List subclass to superclass
  • generics in Java
  • bounded wildcards Java

Related Questions

⦿Can an Interface Extend Multiple Interfaces in Java?

Learn if interfaces in Java can extend multiple interfaces exceptions to multiple inheritance rules and examples to clarify.

⦿How to Resize an Image to Full Width and Fixed Height Using Picasso

Learn how to resize images to fit the full width and a fixed height using Picasso library in Android. Clear steps and code examples included.

⦿How to Fix NoClassDefFoundError for MenuBuilder in Android AppCompat v7 on Samsung Devices?

Resolve NoClassDefFoundError android.support.v7.internal.view.menu.MenuBuilder issue on Samsung devices running Android 4.2 with expert solutions.

⦿How Can I Make a Private Field Immutable in a Java Class?

Learn how to prevent modification of private fields in Java classes and enforce immutability for better encapsulation.

⦿How Can I Exclude Artifacts Inherited from a Parent POM in Maven?

Learn how to exclude inherited artifacts from a parent POM in Maven with a detailed explanation and practical solutions.

⦿What is the Maximum Length of a String in Java?

Discover the maximum number of characters a String can hold in Java and explore solutions for handling large strings efficiently.

⦿How to Locate the Java JDK Source Code for Development?

Learn how to find the Java JDK source code for API methods including where to download the src.zip file for your IDE.

⦿How to Replace Text with a New Line in IntelliJ IDEA

Learn how to efficiently replace text with a new line in IntelliJ IDEA using the replace function. Stepbystep guide with examples.

⦿Why is the `getView` Method of a Custom ListView Adapter Called Multiple Times in Android?

Explore why the getView method in your custom ListView adapter is invoked multiple times and learn how to resolve potential issues with layout and view recycling.

⦿How to Retrieve Inherited Attribute Names and Values Using Java Reflection?

Learn how to access inherited attributes of a Java object using reflection including field names and values. A stepbystep guide.

© Copyright 2025 - CodingTechRoom.com

close