6

I have an array list that looks like:

[new Class1(), new Class2(), new Class1(), new Class1()]

I want to know the most efficient way to extract the number of instances of Class1 in the array list.

For the above example, I want the answer 3.

I am using Java version "1.7.0_79".

0

4 Answers 4

7

You could iterate through the ArrayList and check using the instanceof operator.

for (Object e : lst)
{
  if (e instanceof Car)
  {
    carCount++;
  }
}
Sign up to request clarification or add additional context in comments.

8 Comments

thanks for the answer - hoping for something a bit cleverer OR more efficient but your answer is much appreciated
There really isn't a more efficient way, unless you keep a running count while you're creating the list. By the way, instanceof will be true for objects of classes that extend Car, as well as Car itself. If that isn't what you want, then make it if (e.getClass() == Car.class).
@danday74: keeping tally may be just unnecessary complication (read: source of errors, increased cost of maintenance). How big is your list? How long can it take to count things?
Collections.frequency isn't any more efficient than if you or I wrote it. grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/…
In fact, it has to call the equals methods, which is surely slower than an instanceof operation.
|
2

if the arraylist is like below,

newClass1 = new Class1();

[newClass1, new Class2(), newClass1, newClass1]

then you can check the frequency like below,

Collections.frequency(arrayList, newClass1);

If you are always adding new instance of Class1 then below will be the solution

[new Class1(), new Class2(), new Class1(), new Class1()]

override equals method in Class1 like below,

 @Override
public boolean equals(Object o){

    if(!(o instanceof Class1 )){
        return false;
    }else{

            return true;
    }
}

then in your test class,

System.out.println(Collections.frequency(array, new Class1()));

6 Comments

you're welcome! Please check my edited answer, i got solution for your new Class1 instances arraylist.
you should override equals() method in all your Class1, Class2... classes
It seems like a very bad idea to override equals to make all instances of the class to be equal.
If you want to find instance of Class1 then override equals only in Class1
accepting this answer - both this answer and top voted answer work but this answer is extremely original and well explained - many thanks to all - PS the 2 approaches detailed in this answer work - i however implemented [newClass1, new Class2(), newClass1, newClass1] using singletons in a scenario where it made sense to use singletons. Thanks
|
2

You can need to check the class at any given position in the array by using the keyword instanceof

Example:

public static void main(String[] args) {
    Number[] myN = new Number[5];
    
    //populate... ignore this if  want
    for (int i = 0; i < myN.length; i++) {
        if (i%2==0) {
            myN[i]= new Integer(i);
        }else{
            myN[i]= new Double(i);
        }
    }
    int classACounter=0;
    int classBCounter=0;
    //check
    for (int i = 0; i < myN.length; i++) {
        if (myN[i] instanceof Integer){
            System.out.println(" is an int");
            classACounter++;
        }
        if (myN[i] instanceof Double){
            System.out.println(" is a double");
            classBCounter++;
        }
    }
    
    System.out.println("There are "+classACounter+" elements of the class A");
    System.out.println("There are "+classBCounter+" elements of the class B");
}

Comments

2

Well, You can easily filter them by

result = Iterables.filter(collection, YourClass.java);

then you can apply .size() on the result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.