Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

How can I find whether a collection does or does not contain Java?

Asked by Melvin Forbis Oct 11, 2022 1.2K views 1 answer
Share

About this question

 It seems to be a very common thing to have to tell whether some list or set contains at least one object matching a given condition, yet my prior searching and reading have never found a satisfactory best practice or design pattern to speak of. In the situations I am thinking of, merely using "contains" is not sufficient, as the test we want to perform is not strictly based on the equals method of the class in question.


Some things I have seen:


// for-each with break
private boolean doFoosHaveGoodQuality(List candidates) {
    boolean foundIt = false;
    for (Foo item: candidates) {
        if (<< item>>) {
            fountIt = true;
            break;
        }
    }
    return foundIt;
}   
// while-do with index counter
private boolean doFoosHaveGoodQuality(List candidates) {
    boolean foundIt = false;
    int index = 0;
    while(!fountIt && index < candidates>        Foo item = candidates.get(index++);
        if (<< item>>) {
            fountIt = true;
        }
    }
    return foundIt;
}

Both of these get the job done and quit as soon as a success is found, possibly saving some time if we are lucky enough to have the desired item early in the list. However, both require looping the entire list in order to get a false result

Another technique I've seen in some circumstances is that at the same time the original List is generated (i.e. retrieved from a database) to then simultaneously generate a Map. In this technique, FooKey is an additional (often inner) class holding only those attributes of a Foo that form the "desired quality" and overrides equals and hashcode methods based on that. When you need to determine if there exists a desired Foo, you make a new FooKey() instance with exactly that you want, then call hasKey() on the map. Extra effort up front, but quick and easy at the moment you need it.

I can't put a finger on it. Each idea works, yet each feels just a little hacky and inefficient.

Your answer

1 Answer

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.