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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:28:49+00:00 2026-05-27T12:28:49+00:00

I have been wondering about a function in Java, which I hope exists. I

  • 0

I have been wondering about a function in Java, which I hope exists. I want to iterate through an Arraylist (say of class A), which contains objects of class B and C which both extend A. The idea, I only want to iterate through (for example) the objects of the B class in the ArrayList.

How is this possible, like the short example in the code below, and without the long one?

The main class:

     import java.util.*;

public class Test {

    public static void main(String[] args) {
        new Test ();
    }

    Test() {
        ArrayList<A> a = new ArrayList<A> ();
        a.add (new B ());
        a.add (new C ());
        a.add (new C ());
        a.add (new B ());
        a.add (new C ());
        for (A aObject : a) { // this works, I want it shorter
            if (aObject instanceof B) {
                B b = (B) aObject;
                System.out.println (b.hi);
            }
        }
        for (B b : a) // like this?
            System.out.println (b.hi);
    }
}

A:

public class A {

}

B:

public class B extends A {
    String hi = "hi";
}

C:

public class C extends A {

}

EDIT: Because so many people answer this: I know of the use of instanceof. I want a faster way to loop through all the objects in the array, of which the class is B.

  • 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-27T12:28:50+00:00Added an answer on May 27, 2026 at 12:28 pm

    You could make a reusable Filter class for it, similar to FileFilter. (See korifey’s answer for the use of guava.) Other than that, no, there isn’t any existing functionality to do it. Additionally, any existing functionality would have to do exactly what you’re doing anyway, as the List types don’t have any way of knowing where the different classes of objects are in the list. (It would have to iterate over them as well to find them.)

    If you require a high-performance solution around this, you could consider storing separate lists for your separate instances – either in replacement of or in addition to your current list. You could even wrap this up in a composite class that would offer both functionalities (all instances and instances by class) through a common API.

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

Sidebar

Related Questions

I have been wondering about the reload() function in python, which seems like it
I've been wondering about the following issue: assume I have a C style function
I have been wondering about temp tables in sp's and how all that can
Something I have been wondering about properties for a while. When you are using
There is a question I have been wondering about for ages and I was
I have been reading a lot about test-driven development and decided that I want
I've been wondering about JavaScript's prototypal nature, and the benefits of it, and have
I was wondering about something. Lets say I have this piece of code: if
I have been wondering how to put all the stored procedures on a SQL
I have been wondering what the best practices or ramifications are in setting up

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.