开发者

Checking if Collection contains an Object inside an Object

public class Address{
 private TypeAddress typeAddress;
 private String street;
 //Getters and Setters
 ...
}

public class TypeAddress{
  private String type;
  //Getters and Setters
  ...
}

I have a Collection of Addresses

Collection<Address> addresses = db.getAddresses();

I need to check if is there any address of a type that the costumer will pick. It's just pretending a situation so you guys will understand.

What I want is, I want to get the address of just one type, that type that the customer picked up. If he picked a type that does not exist, I'll pick another addre开发者_JAVA技巧ss. So, I basically need to do 2 loops to check if is there any of its type, and if there is, I'll pick it on the next loop. If it's not, I'll pick another one on the next loop. Is there a way to use the contains method of a Collection to check if the type of the address is what I want?

Something like:

if(addresses.contains("someType")){//remind that I'm trying to check not the object Address, but the object TypeAddress
 //get this address
}else{
 //get another address
}


If you're forced to use a collection (and not a Map like suggested), why can't you just iterate once and store the most "promising" instance as some Address reference:

Address address = null;
for (Address addr : addresses) {
    if ( isBetterThanAlreadyFound(addr, address) ) {
        address = addr;
    }
}


private static boolean isBetterThanAlreadyFound(Address addr, Address alreadyFound) {
    return alreadyFound == null || (addr.isOfDesiredType() && alreadyFound.isNot());
}


even contains internally do the looping logic , use Map instead


Use Map<TypeAddress, Address> so you can verify whether the necessary address exists and get it relatively fast and easy.

Map<TypeAddress, Address> addresses = new HashMap<TypeAddress, Address>();

//put something and then
TypeAddress type = getType();
Address address = addresses.get(type);
if (address != null) {
    return address;
} else {
   //do smth else
}

Don't forget to override methods equals and hashCode of class TypeAddress in a proper way.


I think you might be looking for a way to break out of a loop, or a way to continue at the beginning of the loop. Conveniently enough, they're called break and continue

for (Address address : addresses) {
    if (!address.getType.equals("someType")) continue;
    myAddress = address;
}

OR

for (Address address : addresses) {
    if (address.getType.equals("someType")) {
        myAddress = address;
        break;
    }
}


You can use Apache CollectionUtils API :

Predicate p = new Predicate()
{
 public boolean evaluate(Object o)
 {
   return ((Adress) o).getTypeAddress().getType().equals(TYPE_INPUTED_BY_USER);
 }
}

return CollectionUtils.exists(p);


//I found an example in a book:
// e.g.
Shape shape;
if (shape instanceof Circle) {  
//check if there is any class object Circle in  
// the collection shape
// take some action
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜