1

I am attempting to write code to traverse a collection of type InstallationComponentSetup:

java.util.Collection<InstallationComponentSetup> components= context.getInstallationComponents();
Iterator it = components.iterator();
while (it.hasNext())
{
    if (((InstallationComponentSetup)it).getName() == "ACQ")
    {
         return true;   
    }
}

The cast in the if-statement fails, but I don't really know why (I am a C++ programmer!).

If someone could give me some pointers as to what I am doing wrong I would be grateful.

arshajii
  • 127,459
  • 24
  • 238
  • 287
user1414413
  • 403
  • 5
  • 14

8 Answers8

4

it is an Iterator, whereas it.next() is an InstallationComponentSetup.

The error results from the fact that an Iterator cannot be cast as an InstallationComponentSetup.

Also, you shouldn't even need to cast if you parametrize the Iterator appropriately:

Iterator<InstallationComponentSetup> it = components.iterator();

Finally, don't compare strings with something like a == b, instead use a.equals(b). See "How do I compare strings in Java" for further details.


You might also want to look into the for-each loop if all you want to do is iterate over the collection. Your code can be rewritten as:

for (InstallationComponentSetup component : components)
    if (component.getName().equals("ACQ"))
        return true;
Community
  • 1
  • 1
arshajii
  • 127,459
  • 24
  • 238
  • 287
2

If you are comparing String , use equals() method . Even your casting is wrong.You have to invoke next() on the iterator to get the next element . Hence it.next() gives you the next element which will be an object of InstallationComponentSetup, it is not of type InstallationComponentSetup hence the cast will fail.

Here you are casting the Iterator to your class type which will fail.

if (((InstallationComponentSetup)it).getName() == "ACQ")
{
     return true;   
}

I believe there is no need of cast here as you have defined the Collection to hold the specific type of element and also if you declare the Iterator of a specific type. You can simply do :

// define Iterator of InstallationComponentSetup
Iterator<InstallationComponentSetup> it = components.iterator();
if("ACQ".equals(it.next().getName())) {
   return true;
}

You can also consider using the enhanced for loop in Java , if your purpose is only to read the elements .

 for(InstallationComponentSetup component: components) {
      if("ACQ".equals(component.getName())) {
       return true;
   }
 }
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
1

You have to retrieve the next element in the iteration before you compare:

InstallationComponentSetup next = it.next();
        if (next.getName() == "ACQ")
        {
             return true;   
        }
Manish Doshi
  • 1,205
  • 1
  • 9
  • 17
  • `Iterator` in OP is not generic, so `it.next()` will return an `Object` type. Secondly, it's still doing String comparison using `==`. – Rohit Jain Jul 08 '13 at 14:25
1

Try to use the following code. It is more concise and easier to understand.

Collection<InstallationComponentSetup> components= context.getInstallationComponents();
for(InstallationComponentSetup comp : components){
    if("ACQ".equals(comp.getName()){
        return;
    }
}

I think you had two problems in you code.

  1. Cast the iterator to an object doesn't work like that. You need to use it.next() to get the object and move the iterator.
  2. like already mentioned you need equals to compare Strings. == compares "memory locations" (in C++ terms).
ssindelar
  • 2,833
  • 1
  • 17
  • 36
0

Use it.next() to get the next element.

Also, use the .equals() method to compare strings in Java. Otherwise, the references are compared.

Finally, the cast should be unnecessary with a type-parameterized Iterator.

while (it.hasNext())
{
    if ( it.next().getName().equals("ACQ") ) {
       ...
    }
}
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
0

You have to retrieve the next element in the iteration before you compare:

java.util.Collection<InstallationComponentSetup> components= context.getInstallationComponents();
Iterator<InstallationComponentSetup> it = components.iterator();
while (it.hasNext()) {
    if ("ACQ".equals(it.next().getName())) {
         return true;   
    }
}
M. Abbas
  • 6,409
  • 4
  • 33
  • 46
0

It would be easier to use foreach loop, make use of generic type, use equals for String and change string comparison order to be null secure.

Collection<InstallationComponentSetup> components= context.getInstallationComponents();
for (InstallationComponentSetup setup : components)
{
    if ("ACQ".equals(setup.getName()))
    {
        return true;   
    }
}
Grzegorz Żur
  • 47,257
  • 14
  • 109
  • 105
0

The install4j API is still for Java 1.4, so there are no generics yet. This will work:

    for (Object o : context.getInstallationComponents()) {
        InstallationComponentSetup component = (InstallationComponentSetup)o;
        if (component.getName().equals("ACQ")) {
            return true;
        }
    }
Ingo Kegel
  • 46,523
  • 10
  • 71
  • 102