How to checkout a remote branch without knowing if it exists locally in JGit?

What you want to do is create a branch if and only if a local one is NOT present. Here's what I came up with using streams where exampleRepo is the git repo object, checkout command is the CheckoutCommand, and branchName is the branch name.:

.setCreateBranch(!exampleRepo.branchList()
                        .call()
                        .stream()
                        .map(Ref::getName)
                        .collect(Collectors.toList())
                        .contains("refs/heads/" + branchName));


One possible solution to this I have found so far is to check whether the local branch exists and is an ID in order to combine the two approaches mentioned in the question:

    boolean createBranch = !ObjectId.isId(branchOrCommitId);
    if (createBranch) {
        Ref ref = repo.getRepository().exactRef("refs/heads/" + branchOrCommitId);
        if (ref != null) {
            createBranch = false;
        }
    }
    repo.checkout()
            .setCreateBranch(createBranch)
            .setName(branchOrCommitId)
            .call();