0

I made a function that returns an array list with the people having the highest number field. But I'm pretty sure that this can be optimized

public  ArrayList<Membre> findOlder(){
    ArrayList<Membre>olderPersonnes = new ArrayList<Membre>();
    int higher = 0;
    for (Membre membre : membresDeLaFamille) {
        if (membre.getNumber() > higher) {
            higher = membre.getNumber();
        }
    }
    for (Membre membre: membresDeLaFamille) {
        if (membre.getNumber() == higher) {
            olderPersonnes.add(membre);
        }
    }
    return olderPersonnes;
}
7
  • 1
    Asking for optimization for working code is a better fit for codereview.stackexchange.com. Commented Jan 3, 2022 at 16:20
  • Stream.filter() maybe. Commented Jan 3, 2022 at 16:21
  • @JohnnyMopp while Streams have built-in functions for "max" or "filter", they incur a performance penalty and should be avoided for simple cases where performance is an issue. Commented Jan 3, 2022 at 16:24
  • @TomElias This hardly depends on the use case and only the author can give us details about the size of the membresDeLaFamille collection. For better readability and maintainability i aggree with Johnny Mopp. You can exit the method immediately, if membresDeLaFamille is empty. Max might not be the correct streaming operation depending on the fact if multiple persons can have the same number assigned. Commented Jan 3, 2022 at 16:29
  • 1
    In terms of readability it's not great, but in terms of runtime complexity you can't have better. Commented Jan 3, 2022 at 16:32

3 Answers 3

1

optimize? depends on what you're optimizing for.

complexity - this is an O(n) loop, it is already the least "complex" it can be. (findmax in an unsorted array of size n -> O(n))

running time - can be improved if instead of the second loop you just keep a reference to the "membre" that you find inside the first loop, and return that.

Sign up to request clarification or add additional context in comments.

5 Comments

"instead of the second loop you just keep a reference to the "membre" that you find inside the first loop" -> What if there's more than one?
add them to a collection like he does in the second loop. use >= instead of >
Gonna waste more time newing up and destroying collections than looping once again IMO. Perhaps with a linked list, but...
IIRC, for(each) loop uses Iterator behind the scenes, that's also a waste if we're nitpicking ;)
Just saying that I dont think you can necessarily claim that running time will be improved with the suggestion you made :P Still +1 because I think you can't reasonably optimize further.
1

You can't do better than O(1) runtime complexity (unless the list is sorted already), so this is already optimized.

In terms of readability you could do better (arguably) using declarative (and functional concepts) vs imperative programming, but it certainly wouldn't be faster.

e.g.

public static List<Member> findOlder() {
    return membresDeLaFamille.stream()
        .map(Member::getNumber)
        .max(Integer::compare)
        .map(maxAge -> membresDeLaFamille.stream().filter(m -> maxAge.equals(m.getNumber())))
        .orElseGet(Stream::empty)
        .collect(Collectors.toList());
}

Comments

0

Basically, the list of multiple family members with the maximum number may be retrieved in the one pass if the input list membresDeLaFamille is unsorted, and as soon as the new "max" is found, current list should be cleared:

public List<Membre> findOlder() {
    List<Membre> olderPersonnes = new ArrayList<>();
    int higher = 0;
    for (Membre membre : membresDeLaFamille) {
        if (membre.getNumber() >= higher) {
            if (membre.getNumber() > higher) {
                higher = membre.getNumber();
                olderPersonnes.clear();
            }
            olderPersonnes.add(membre);
        }
    }
    return olderPersonnes;
}

If the input list happens to be already sorted by the values returned by Membre::getNumber, then the max value(s) is/are surely placed at some end of the input list (depending on the ascending or descending order of sorting).

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.