Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make use of Collections.isEmpty(Iterable) where applicable #1958

Closed
danieldietrich opened this issue Apr 23, 2017 · 0 comments
Closed

Make use of Collections.isEmpty(Iterable) where applicable #1958

danieldietrich opened this issue Apr 23, 2017 · 0 comments

Comments

@danieldietrich
Copy link
Contributor

I think there are several methods than can be optimized.

For example CharSeq containts the following method:

@Override
public CharSeq prependAll(Iterable<? extends Character> elements) {
    Objects.requireNonNull(elements, "elements is null");
    final StringBuilder sb = new StringBuilder();
    for (char element : elements) {
        sb.append(element);
    }
    sb.append(back);
    return sb.length() == 0 ? EMPTY : of(sb);
}

It can be optimized like this:

@Override
public CharSeq prependAll(Iterable<? extends Character> elements) {
    Objects.requireNonNull(elements, "elements is null");
    if (Collections.isEmpty(elements)) {
        return this; // 1
    } else if (isEmpty()) {
        return CharSeq.ofAll(elements); // 2
    } else {
        final StringBuilder sb = new StringBuilder();
        for (char element : elements) {
            sb.append(element);
        }
        sb.append(back);
        return CharSeq.of(sb);
    }
}

Notes:

  • 1: We save a StringBuilder instance, and some (empty) operations and checks
  • 2: Same benefits as in 1. Also CharSeq.ofAll does contain additional optimizations, like instanceof CharSeq-shortcut etc.
@danieldietrich danieldietrich added this to the vavr-1.0.0 milestone Apr 23, 2017
@danieldietrich danieldietrich modified the milestones: vavr-1.0.0, vavr-1.1.0 Nov 26, 2017
NataliiaPrivezentseva added a commit to NataliiaPrivezentseva/vavr that referenced this issue Jan 31, 2018
danieldietrich pushed a commit that referenced this issue Feb 17, 2018
#2208)

* Improved some methods in CharSeq, Vector, TreeSet and HashSet classes using Collections.isEmpty(Iterable) method (#1958)

* Reverted changes in addAll(Iterable) methods in TreeSet and HashSet classes

* Fixed ofAll(Iterable) method in Vector class in order to pass Euler67 and Euler18 tests
@danieldietrich danieldietrich modified the milestones: vavr-1.1.0, vavr-1.0.0 Feb 17, 2018
danieldietrich pushed a commit that referenced this issue Jan 6, 2019
#2208)

* Improved some methods in CharSeq, Vector, TreeSet and HashSet classes using Collections.isEmpty(Iterable) method (#1958)

* Reverted changes in addAll(Iterable) methods in TreeSet and HashSet classes

* Fixed ofAll(Iterable) method in Vector class in order to pass Euler67 and Euler18 tests
danieldietrich pushed a commit that referenced this issue Jan 13, 2019
#2208)

* Improved some methods in CharSeq, Vector, TreeSet and HashSet classes using Collections.isEmpty(Iterable) method (#1958)

* Reverted changes in addAll(Iterable) methods in TreeSet and HashSet classes

* Fixed ofAll(Iterable) method in Vector class in order to pass Euler67 and Euler18 tests
danieldietrich pushed a commit that referenced this issue Jan 13, 2019
#2208)

* Improved some methods in CharSeq, Vector, TreeSet and HashSet classes using Collections.isEmpty(Iterable) method (#1958)

* Reverted changes in addAll(Iterable) methods in TreeSet and HashSet classes

* Fixed ofAll(Iterable) method in Vector class in order to pass Euler67 and Euler18 tests
danieldietrich pushed a commit that referenced this issue Jan 16, 2019
#2208)

* Improved some methods in CharSeq, Vector, TreeSet and HashSet classes using Collections.isEmpty(Iterable) method (#1958)

* Reverted changes in addAll(Iterable) methods in TreeSet and HashSet classes

* Fixed ofAll(Iterable) method in Vector class in order to pass Euler67 and Euler18 tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant